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>