Spring中注入bean学习的总结
1.在类上直接加注解@Component,那么这个类就直接注入到Spring容器中了 ,像@Contrloller,@Service这些本质上都是@Component,
2.@Configuration(或者@SpringBootConfiguration)放到类上,然后在类中的方法上加注解@Bean
@SpringBootConfiguration
public class UseConditionalOnBean {
@Bean
public User createBean(){
return new User();
}
}
当然也可以直接在类上加@Bean,这种情况就和1比较类似了
3.@ComponentScan (在主类中,比如springboot的App.java),然后在加上@Import(Bean.class)或者@Import(BeanConfig.class)
4.@ComponentScan (在主类中,比如springboot的App.java),然后在@EnableXXX 这个注解里也是包含有@Import,本质上还是和3一样
再说,Bean注入Spring容器的一些回调:
1.DefinitionRegistryPostProcessor:
/**
* 通过BeanDefinitionRegistryPostProcessor动态的注入bean
* 这里的例子是一次性注入10个person的bean,并为这10个person的bean的name属性赋值了
* BeanDefinitionRegistryPostProcessor可以拿到ConfigurableListableBeanFactory和BeanDefinitionRegistry两个对象
* @author Joy
*
*/
@Component
public class MyBeanDefinitionRegistryPostProcessor implements
BeanDefinitionRegistryPostProcessor {
public void postProcessBeanFactory(
ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
public void postProcessBeanDefinitionRegistry(
BeanDefinitionRegistry registry) throws BeansException {
for(int i=1;i<=10;i++){
BeanDefinitionBuilder bdb=BeanDefinitionBuilder.rootBeanDefinition(Person.class);//这个是构造beanDefinition的
bdb.addPropertyValue("name", "admin"+i);
registry.registerBeanDefinition("person"+i, bdb.getBeanDefinition());
}
}
}
2.BeanFactoryPostProcessor
/**
* 容器初始化的回调方法
* BeanFactoryPostProcessor在spring容器初始化之后触发,而且只会触发一次
* 触发的时机比BeanPostProcessor早
* @author Joy
*
*/
@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
throws BeansException {
System.out.println("=========postProcessBeanFactory========"+ beanFactory.getBeanPostProcessorCount());
}
}
3.BeanPostProcessor:
/**
* Bean初始化的回调方法
* @author Joy
*
*/
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
public Object postProcessAfterInitialization(Object bean, String name)
throws BeansException {
System.out.println("++++++++before+++++"+bean.getClass());
return bean;
}
public Object postProcessBeforeInitialization(Object bean, String name)
throws BeansException {
System.out.println("----------after----"+bean.getClass());
return bean;
}
}
Spring中注入bean学习的总结的更多相关文章
- 1.2(Spring学习笔记)Spring中的Bean
一.<Bean>的属性及子元素 在1.1中我们对<Bean>有了初步的认识,了解了一些基本用法. 现在我们进一步理解<Bean>属性及子元素. 我们先来看下< ...
- spring扩展点之二:spring中关于bean初始化、销毁等使用汇总,ApplicationContextAware将ApplicationContext注入
<spring扩展点之二:spring中关于bean初始化.销毁等使用汇总,ApplicationContextAware将ApplicationContext注入> <spring ...
- 传统javabean与spring中的bean的区别
javabean已经没人用了 springbean可以说是javabean的发展, 但已经完全不是一回事儿了 用处不同:传统javabean更多地作为值传递参数,而spring中的bean用处几乎无处 ...
- 第2章 Spring中的Bean
2.1 Bean的配置 Bean本质是Java中的类.Spring可以被看做一个大型工厂,这个工厂的作用就是生产和管理Spring容器zho中的Bean.想在项目中使用这个工厂,就需要对Spring的 ...
- Spring 中的bean 是线程安全的吗?
结论: 不是线程安全的 Spring容器中的Bean是否线程安全,容器本身并没有提供Bean的线程安全策略,因此可以说Spring容器中的Bean本身不具备线程安全的特性,但是具体还是要结合具体sco ...
- spring(四):spring中给bean的属性赋值
spring中给bean的属性赋值 xml文件properties标签设置 <bean id="student" class="com.enjoy.study.ca ...
- 【Spring】Spring中的Bean - 5、Bean的装配方式(XML、注解(Annotation)、自动装配)
Bean的装配方式 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 文章目录 Bean的装配方式 基于XML的装配 基于注解 ...
- 【Spring】Spring中的Bean - 4、Bean的生命周期
Bean的生命周期 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 了解Spring中Bean的生命周期有何意义? 了解Sp ...
- 【Spring】Spring中的Bean - 1、Baen配置
Bean配置 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 什么是Spring中的Bean? Spring可以被看作是一个 ...
随机推荐
- node.js 在使用child_process 模块时候,调试端口占用的问题解决方案(EADDRINUSE)
在fork的时候,带参数{ execArgv: ['--debug=' + (process.debugPort + 1)] }
- 在nodejs里面是用类似配置文件的方法
1.a.js exports.MYSQLIP = '127.0.0.1'; exports.MYSQLPORT = 1336; 2.b.js const C = require('./config/c ...
- 一种基于URL数据源的WEB报表插件
完全支持所见所得的报表设计, 支持 PHP ,Java 等所有支持JSON格式的后端. 立即下载测试版本 需要正式版本?请QQ联系:1565498246 或者留言
- springmvc+mybatis 构建cms+UC浏览器文章功能
最近公司在模拟UC浏览器做一个简单的cms系统,主要针对于企业内部的文章浏览需求,这边考虑用户大多用mobile浏览文章内容,故使用原生的ios和android进行开发,后面也会集成html5. 1. ...
- C++点和箭头操作符用法区别
变量是对象的时候用“.”访问 变量是对象指针的时候用“->”访问 例: #inlclude <iostream> using namespace std; class A { pub ...
- [Robot Framework] 动态等待,提供默认的等待时间,等待时间可传可不传
默认10s
- python常用命令
安装sudo easy_install pip 列出已安装的包pip freeze or pip list 导出requirements.txtpip freeze > <目录>/r ...
- 【UI测试】--快捷键组合
- 【C++】c++中栈 队列 的应用
C++中提供了STL模板statck 在使用的时候更为方便 除了一般的队列外 还有STL更有双向队列可以使用 #include<deque> 声明:deque <type > ...
- 前端之前端介绍或html的常用标签1
一 web服务器的本质 由浏览器发送一个请求.服务器接收到,然后在回应一个响应. 由于浏览器的不同,web服务器响应的内容不一定被浏览器接收. HTTP/1.1 201 OK\r\n\r\n需要发送的 ...