以注解的方式装配 Bean 到 Spring IoC 容器中
使用一个基于注解的方式的
IoC容器:AnnotationConfigApplicationContext
实例
首先创建一个 Java 对象(POJO)
User.java
@Data
public class User {
private Integer id;
private String name;
private String age;
}
然后再定义一个 Java 配置文件 AppConfig.java
package com.lostsheep.java.design.mode.springbean;
import com.lostsheep.java.design.mode.builder.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* <b><code>Appconfig</code></b>
* <p/>
* Description
* <p/>
* <b>Creation Time:</b> 2020/5/17 1:38.
*
* @author lostsheep
* @since java-design-mode 0.1.0
*/
@Configuration
public class Appconfig {
@Bean(name = "user")
public User initUser() {
return User.builder()
.id(1)
.age("20")
.name("lostsheep")
.build();
}
}
-
@Configuration注解代表该类是一个Java配置文件,Spring 的容器会根据它来生成IoC容器去装配Bean -
@Bean注解代表将 initUser() 方法返回的 POJO 对象装配到IoC容器中,而属性name="user"定义了这个Bean的名称,如果没有配置 name 属性,则会将方法名称initUser作为Bean名称保存到Spring IoC容器中
使用 AnnotationConfigApplicationContext 来构建自己的 IoC 容器
代码片段:
package com.lostsheep.java.design.mode.springbean;
import com.lostsheep.java.design.mode.builder.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* <b><code>Appconfig</code></b>
* <p/>
* Description
* <p/>
* <b>Creation Time:</b> 2020/5/17 1:38.
*
* @author lostsheep
* @since java-design-mode 0.1.0
*/
@Configuration
@Slf4j
public class AppConfig {
@Bean(name = "user")
public User initUser() {
return User.builder()
.id(1)
.age("20")
.name("lostsheep")
.build();
}
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext =
new AnnotationConfigApplicationContext(AppConfig.class);
User userBean = applicationContext.getBean(User.class);
log.info(userBean.toString());
}
}
- 代码中将 Java 配置文件
AppConfig反射对象传递给AnnotationConfigApplicationContext的构造方法中,这样它就可以读取配置了 - 然后将配置类中的
Bean装配到IoC容器中,并使用 getBean() 方法获取对应Bean,启动该 main() 方法可以看到控制台打印的Bean信息
D:\Tools\jdk1.8.0_241\bin\java.exe
01:56:18.930 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7b1d7fff
01:56:18.943 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
01:56:19.037 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
01:56:19.038 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
01:56:19.039 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
01:56:19.041 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
01:56:19.049 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'appConfig'
01:56:19.054 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'user'
01:56:19.079 [main] INFO com.lostsheep.java.design.mode.springbean.AppConfig - User{id=1, name='lostsheep', age='20'}
- 已经可以明显地看到配置到配置文件中的名称为 user 的
Bean已经被装配到IoC容器中,并且可以通过 getBean() 方法获取对应的Bean,并在日志中输出了该Bean的信息 - 当然这只是很简单的方法,而注解
@Bean也不是唯一创建Bean的方法,还有其他的方法可以让IoC容器装配Bean,而且Bean之间还有依赖的关系需要进一步处理。