-2

Following is my employee class. If I write a @Bean annotation to Line 1, Line 2 or Line 3 it throws an error.

It allows the @Bean annotation to method names only. Why?

    import org.springframework.context.annotation.Bean;

    //line 1
    public class Employee {

        int id;
        String name;

        // line 2
        public Employee(int id, String name) {
            this.id = id;
            this.name = name;
        }

        //line 3
        public Employee() {
        }

        @Bean
        public void showCurrentEmployee() {
            System.out.println(this.id + " " + this.name);
        }

    }

and As the Spring world says; Bean scope is a singleton. Where does that apply here? How the @Bean method would hold the singleton instance and of what?

If @Bean can not be given to the class name then how the following thing is valid ?

<bean name="emp" class="com.myProj.Employee"> 
<property name="id"> 
      <value>20</value> 
</property> 
<property name="name"> 
      <value>John</value> 
</property> 
</bean> 
user2746466
  • 361
  • 5
  • 23
  • 2
    https://docs.spring.io/spring-javaconfig/docs/1.0.0.m3/reference/html/creating-bean-definitions.html look at this document. it will help you – Oğuzhan Aygün Jun 25 '19 at 12:07
  • 2
    Please don't link to documentation that isn't relevant anymore. The Spring Java Config project has been dead for several years now. Parts of it (and lessons learned) have been incorporated into Spring itself. – M. Deinum Jun 25 '19 at 12:13
  • Bean scope is singleton by default in Spring. That means that exists only one instance of this class. Spring creates it the first time it will be used. It will be managen from Spring and will be injected on every reuse. – Elio Jun 25 '19 at 12:41
  • @Elio, but my Employee class can have multiple instances. – user2746466 Jun 25 '19 at 12:50
  • I am not sure if what you need is a `@Bean` or maybe an `@Entity`? What is the Empoyee class in your case, is it an entry in database or is it a service? See this two links about the scope of `@Bean` in Spring. They maybe help to better understand what you need in you case: https://www.baeldung.com/spring-bean-scopes OR https://www.tutorialspoint.com/spring/spring_bean_scopes.htm – Elio Jun 25 '19 at 14:14

1 Answers1

1

@Bean annotation could be used only in Spring configuration classes. This classes should be annotated with @Configuration annotation

With properties injection the @Value annotation could helps.

@Configuration
public class ConfigClass {
   @Bean
   public BeanClassOne beanOne(@Value("20") Integer intValue) {
      return new BeanClassOne(intValue);
   }

   @Bean
   public BeanClassTwo beanTwo() {
      return new BeanClassTwo();
   }

   @Bean
   public BeanClassThree beanThree() {
      return new BeanClassThree();
   }
}

Another way to make Spring bean from the class to annotate it with @Service or @Component annotation

@Service
public class BeanClass {

    @Value("String value")
    privat String strField;
    // your bean methods
}

More info here https://www.tutorialspoint.com/spring/spring_java_based_configuration.htm

Alexey Usharovski
  • 1,404
  • 13
  • 31