In the past, Java frameworks basically used XML as configuration files, but now Java frameworks also support "zero configuration" based on Annotation instead of XML configuration files. Struts2, Hibernate, and Spring have all started to use Annotation instead of XML configuration files; and Spring3.x provides three options, namely: XML-based configuration, annotation-based configuration and Java-based configuration.
The following are the three configuration methods; first define a JavaBean for example.
package com.chinalife.dao public class LoginUserDao {... // Used to set the initialization method public void myInit() {}// Used to set the destruction method public void myDestroy() {}}1. XML-based configuration
<bean id="loginUserDao" class="com.chinalife.dao.impl.LoginUserDaoImpl" lazy-init="true" init-method="myInit" destroy-method="myDestroy" scope="prototype"> ... </bean>
In XML configuration, the bean is defined through <bean></bean>, and the bean's name is defined through the id or name attribute. If the id and name attributes are not specified, Spring will automatically use the fully qualified class name as the name of the bean. Inject value into the Bean through the dynamic attribute of the <property> child element or the p namespace. You can also specify the method name of the Bean implementation class through the init-method and destory-method properties of <bean> to set the life process method (specify at most one initialization method and a destroy method). Specify the scope of the bean through the scope of <bean>. I have heard of the lazy-init attribute of <bean> specifies whether to delay initialization.
When the implementation class of the Bean comes from third-party class libraries, such as DataSource, HibernateTemplate, etc., it cannot annotate information in the class, and can only be configured through XML; and the configuration of namespaces, such as aop, context, etc., can only use XML-based configuration.
2. Annotation-based configuration
@Scope("prototype") @Lazy(true) @Component("loginUserDao") public class LoginUserDao { ... // Used to set the initialization method @PostConstruct public void myInit() { } // Used to set the destruction method @PreDestroy public void myDestroy() { } }In the Bean implementation class, some Annotations are used to annotate the Bean class:
・@Component: Annotate a normal SpringBean class (the bean name can be specified, and the default is the class name starting with lowercase letters when not specified)
・@Controller: Annotate a controller class
・@Service: Annotate a business logic class
・@Repository: Annotate a DAO class
By labeling @Autowired by type matching injection at member variables or method parameters, you can also use @Qualifier to configure injection by name. The initialization method and destroy method specified by annotating the @PostConstrut and PreDestroy annotation (as many can be defined). Specify the scope of the bean's action through @Scope("prototype"). Specify the lazy loading of the bean by labeling @Lazy(true) at the class definition.
When the implementation class of the Bean is developed by the current project, you can directly use annotation-based configuration in the Java class, and the configuration is relatively simple.
3. Java-based configuration
@Configuration public class Conf { @Scope("prototype") @Bean("loginUserDao") public LoginUserDao loginUserDao() { return new LoginUserDao(); } }In the java class marked @Configuration, define a Bean by annotating @Bean in the class method. Methods must provide instantiation logic for beans. The name of the bean can be defined through the @Bean name attribute. The default name is the method name if it is not specified. At the method, use @Autowired to bind the method to the parameter, and then inject it through code in the method; you can also call the @Bean method of the configuration class for injection. Specify an initialization or destroy method through @Bean's initMethod or destroyMethod. The scope of action of the bean is specified by the Bean method definition. Specify the delay initialization of the bean by labeling @Lazy at the Bean method definition.
When the logic of instantiating a bean is more complicated, it is more suitable for Java class configuration.
Summarize
The above is all the content in this article about the three bean configuration methods in Spring 3.x. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!