1, an overview of the

JavaConfig was originally a subproject of Spring that provides Bean definition information in the form of Java classes. In the Spring4 release, JavaConfig has officially become a core feature of Spring4.

Java-based container configuration

2.1 Basic Concepts @bean and @Configuration

2.3.1 Bind Java and XML configuration

package com.zhl; import com.alibaba.druid.pool.DruidDataSource; import com.zhl.entity.User; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @configuration /* is equivalent to creating an XML file <bean></bean>*/ @componentScan ("com.zhl")/* equivalent to <context:component-scan Base-package ="com.zhl"></context:component-scan>*/ @propertysource ("classpath:db.properties")/* Import external files, */ Public class JavaConfig {@value ("${mysql.username}") private String name; @Value("${mysql.password}") private String password; @Value("${mysql.url}") private String url; @Value("${mysql.driverClassName}") private String driverName; Public DruidDataSource dataSource(){DruidDataSource dataSource=new DruidDataSource(); dataSource.setName(name); dataSource.setPassword(password); dataSource.setUrl(url); dataSource.setDriverClassName(driverName); return dataSource; } //init‐method="initByConfig" destroy‐method="destroyByConfig" @bean (initMethod ="initByConfig",destroyMethod = "destroyByConfig") public User userinfo(){ return new User(); }}Copy the code
package com.zhl.entity; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.stereotype.Controller; import java.util.List; import java.util.Map; /** * lifecycle callback * 1. Use the interface to implement: * 1. The initialization callback implements InitializingBean to override afterPropertiesSet * 2. DisposableBean override destroy * 2. */ @controller public class User {private Integer ID; private String username; private String realname; private Role role; private List<String> hobbies; private Map<Integer,String> course;; Public User() {} public void initByConfig() throws Exception{system.out.println (" instantiate User"); } public void destroyByConfig() throws Exception {system.out.println (" destroy User"); }}Copy the code

2.2, the use of AnnotationConfigApplicationContext initializes the Spring container simple structure

public class IocTest { @Test public void test0(){ ApplicationContext ioc=new AnnotationConfigApplicationContext(JavaConfig.class); UserController bean = ioc.getBean(UserController.class); bean.getUser(); }}Copy the code

2.3. @bean annotations

Declare a Bean

The @bean is a method-level annotation similar to an element in XML. Annotations support some of the attributes provided, such as init-method, destroy-method, Autowiring, and Name. Developers can use @Bean annotations in the @Configuration or @Component classes

package com.zhl; import com.zhl.entity.User; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean public User user(){ return new User(); }}Copy the code

The previous configuration is exactly equivalent to the following Spring XML:

<bean id="user" class="com.zhl.entity.User"></bean>
Copy the code

2.3.2 Dependencies between beans

Dependencies can be implemented using method parameters, as shown in the following example:

package com.zhl; import com.zhl.entity.Role; import com.zhl.entity.User; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean public User user(Role role){ return new User(role); } @Bean public Role role(){ return new Role(); }}Copy the code

2.3.3 Accept life cycle callback

@Bean(initMethod = "initByConfig",destroyMethod = "destroyByConfig")
public User userinfo(){
    return new User();
}
Copy the code

2.3.4. Specify the scope of the Bean

@Scope("prototype")
public User user(){
    return new User();
}
Copy the code

2.3.5. Customize the Bean name

@Configuration Public class AppConfig {/* By default, the Configuration class uses the name of the @bean method as the name of the resulting Bean. However, you can override this functionality using the Name attribute, as shown in the following example: */ @Bean(name = {"user2","user3","user4"}) @Scope("prototype") public User user(){ return new User(); }}Copy the code

2.4. @Configuration Annotation

2.4.1 inject internal Bean dependencies

@Configuration public class AppConfig {/* When beans are dependent on each other, indicating the dependency is as simple as calling another Bean method. */ @bean public User User(){return new User(role()); } @Bean public Role role(){ return new Role(); }}Copy the code

2.5. Constitute java-based configuration

@ Import annotations

The @import annotation is used to Import configuration classes or classes that need to be pre-loaded.

@Configuration public class AppConfig { @Bean public User user(){ return new User(); }}Copy the code
@Configuration @Import(AppConfig.class) public class AppConfig2 { @Bean public Role role(){ return new Role(); }}Copy the code
public class IocTest { @Test public void test0(){ ApplicationContext ioc=new AnnotationConfigApplicationContext(AppConfig2.class); User bean = ioc.getBean(User.class); System.out.println(bean.toString()); //User{id=null, username='null', realname='null', role=null} } }Copy the code