How it works...

The @Bean annotation is equivalent to <bean/> in the XML-based ApplicationContext definition. It is method-level and must be attached to functions inside @Configuration classes. Without it, the method will be considered as a typical function and might give some container errors.

The recipe advices developers to use the name attribute of @Bean in order to monitor and specify the bean id of the containers without compromising the Java coding standard for the function signature. Without this attribute, the container will consider the method name as the bean id just as shown in the following example:

@Bean 
public Employee empRec4(){ 
    Employee empRec4 = new Employee("Diego","Silang", 
    new Date(65,11, 15), 55, 85000.00, "guitarist", dept4()); 
    return empRec4; 
} 
@Bean 
public Department dept4(){ 
    Department dept4 = new Department(11223, "Music Department"); 
     return dept4; 
} 

With effect from Spring 3.0, the ApplicationContext can be instantiated through AnnotationConfigApplicationContext when the JavaConfig approach is used. It is XML-free since it only accepts classes that are annotated with @Configuration, @Component, and JSR-330 annotations such as @Inject, @Named, and @Singleton. The class is very versatile in that it also recognizes DI metadata such as @Autowired, @Resource, or @Inject and considers them as part of the ApplicationContext definition.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.111.179