前言
在日常开发中,项目中大量的Bean的装配。今天就来聊聊如何使用注解装配Bean。这里与其说是SpringBoot装配Bean还是不如说是Spring注解来装配Bean
Spring boot bean 默认创建的bean 为singleton模式
- 通过Java配置文件@Bean的方式定义Bean
- 通过注解扫描的方式@Component/@ComponentScan
一、本项目装载bean
1.1、@Component方式
@Component("componentBean")
public class ComponentBean {
private String type = "@Component实例化bean";
public String getName(String name) {
return name + " ___ " + type;
}
}
注解@Component 表明这个类将被Spring IoC容器扫描装配,bean的名称为componentBean。 如果不配置这个值 ,那IoC 容器就会把类名第一个字母作为小写,其他的不变作为 Bean 名称放入到 IoC 容器中
1.2、@Bean方式
pojo类
public class ConfigBean {
private String type = "Configuration注解生成bean实体";
public String getName(String name) {
return name + " ___ " + type;
}
}
BeanConfig类
@Configuration
public class BeanConfig {
@Bean
public ConfigBean configBean() {
return new ConfigBean();
}
}
@Configuration代表是一个 Java 配置文件 , Spring会根据它来生成 IoC 容器去装配 Bean
@Bean 代表将 configBean方法返回的 POJO 装配到 IoC 容器中, name为Bean 的名称,如果没有配置它,则会将方法名称作为 Bean 的名称保存到 Spring IoC 容器中
测试
@Autowired
private ConfigBean configBean;
/**
* 2、通过@Component方式注入bean 这里通过构造方法引入方式(也可和同上通过@Autowired注入)
*/
@Autowired
private ComponentBean componentBean;
@GetMapping(path = "/bean")
public String bean(String name) {
Map<String, String> map = new HashMap(16);
map.put("ComponentBean", componentBean.getName(name));
map.put("ConfigBean", configBean.getName(name));
return JSON.toJSONString(map);
}
二、装载第三方bean
2.1、@Component方式
@Component
public class ThirdComponentBean {
private String type = "第三方ThirdComponent注解生成bean实体";
public String getName(String name) {
return name + " ___ " + type;
}
}
如果是@Component方式,那么第三方jar包依然无法实例化当前Bean的,除非,在上面的BeanConfig将@ComponentScan注解打开,同时满足@ComponentScan注解能够扫描到ThirdComponentBean实体,那么久可以实例化该Bean
2.1、通过@Bean方式
POJO实体
public class ThirdConfigBean {
private String type = "第三方 ThirdConfigBean注解生成bean实体";
public String getName(String name) {
return name + " ___ " + type;
}
}
BeanConfig
@Configuration
//@ComponentScan("com.jincou.third") 当前项目包名
public class BeanConfig {
@Bean
public ThirdConfigBean thirdConfigBean() {
return new ThirdConfigBean();
}
}
这个时候在将配置放在指定的文件中即可,使用者会自动加载,从而避免的代码的侵入
- 在资源目录下新建目录 META-INF
- 在 META-INF 目录下新建文件 spring.factories
- 在文件中添加
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.jincou.third.config.BeanConfig
测试
/**
* 1、通过@Component方式注入bean
*/
@Autowired
private ThirdComponentBean thirdComponentBean;
/**
* 2、经典的注解引入方式 就是在@Configuration注解下生存bean
*/
@Autowired
private ThirdConfigBean thirdConfigBean;
@GetMapping(path = "/third-bean")
public String third_bean(String name) {
Map<String, String> map = new HashMap(16);
map.put("ThirdComponentBean", thirdComponentBean.getName(name));
map.put("ThirdConfigBean", thirdConfigBean.getName(name));
return JSON.toJSONString(map);
}
三、spring中获取bean
工具类
@Component
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
//获取applicationContext
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
//通过name获取 Bean.
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
//通过class获取Bean.
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
//通过name,以及Clazz返回指定的Bean
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
}
bean使用上面的案例
测试
@GetMapping(path = "/getBean")
public String getBean() {
ComponentBean s1 = SpringContextUtil.getBean(ComponentBean.class);
ConfigBean s2 = SpringContextUtil.getBean(ConfigBean.class);
System.out.println(s1);
System.out.println(s2);
return s1 +"----"+ s2;
}