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. 1.2(Spring学习笔记)Spring中的Bean

    一.<Bean>的属性及子元素 在1.1中我们对<Bean>有了初步的认识,了解了一些基本用法. 现在我们进一步理解<Bean>属性及子元素. 我们先来看下< ...

  2. spring扩展点之二:spring中关于bean初始化、销毁等使用汇总,ApplicationContextAware将ApplicationContext注入

    <spring扩展点之二:spring中关于bean初始化.销毁等使用汇总,ApplicationContextAware将ApplicationContext注入> <spring ...

  3. 传统javabean与spring中的bean的区别

    javabean已经没人用了 springbean可以说是javabean的发展, 但已经完全不是一回事儿了 用处不同:传统javabean更多地作为值传递参数,而spring中的bean用处几乎无处 ...

  4. 第2章 Spring中的Bean

    2.1 Bean的配置 Bean本质是Java中的类.Spring可以被看做一个大型工厂,这个工厂的作用就是生产和管理Spring容器zho中的Bean.想在项目中使用这个工厂,就需要对Spring的 ...

  5. Spring 中的bean 是线程安全的吗?

    结论: 不是线程安全的 Spring容器中的Bean是否线程安全,容器本身并没有提供Bean的线程安全策略,因此可以说Spring容器中的Bean本身不具备线程安全的特性,但是具体还是要结合具体sco ...

  6. spring(四):spring中给bean的属性赋值

    spring中给bean的属性赋值 xml文件properties标签设置 <bean id="student" class="com.enjoy.study.ca ...

  7. 【Spring】Spring中的Bean - 5、Bean的装配方式(XML、注解(Annotation)、自动装配)

    Bean的装配方式 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 文章目录 Bean的装配方式 基于XML的装配 基于注解 ...

  8. 【Spring】Spring中的Bean - 4、Bean的生命周期

    Bean的生命周期 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 了解Spring中Bean的生命周期有何意义? 了解Sp ...

  9. 【Spring】Spring中的Bean - 1、Baen配置

    Bean配置 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 什么是Spring中的Bean? Spring可以被看作是一个 ...

随机推荐

  1. Ubuntu12.04(64bit)下安装Qt4总结

    本文主要介绍linux系统Ubuntu12.04(64bit)下Qt4.8.5的安装,其中还涉及Fedora9下Qt4的安装. 1.下载软件:去Qt的官网下载Qt4.8.5和Qt Creator软件, ...

  2. 品味性能之道<十一>:JAVA中switch和if性能比较

    通常而言大家普遍的认知里switch case的效率高于if else.根据我的理解而言switch的查找类似于二叉树,if则是线性查找.按照此逻辑推理对于对比条件数目大于3时switch更优,并且对 ...

  3. [Jmeter] Concurrency Thread Group

    Concurrency Thread Group : https://jmeter-plugins.org/wiki/ConcurrencyThreadGroup/ 参数介绍: Target Conc ...

  4. swift NSdata 转换 nsstring

    result = NSString(data: data, encoding: NSUTF8StringEncoding) 做HTTP 请求时 遇到 打印结果看  所以~~~

  5. c++智能指针(1)

    根据muduo开源库作者陈硕的一些文章.对于多线程下C++编程提出了一些观点.主要是多线程下对象的销毁比较困难,但是由于多线程下,mutext是无法保护析构的.而后提出了智能指针的方案并对使用该指针会 ...

  6. Maximum Average Subarray I LT643

    Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...

  7. 关于Spring父容器和SpringMvc子容器

    在SSM项目中,会有SpringMvc容器(子容器)和Spring容器(父容器) 一共2个容器 基本规则: 子容器可以访问父容器的bean,父容器不能访问子容器的bean. 当<context: ...

  8. Delegate,Action,Func,匿名方法,匿名委托,事件 (转载)

    Delegate,Action,Func,匿名方法,匿名委托,事件 (转载) 一.委托Delegate 一般的方法(Method)中,我们的参数总是string,int,DateTime...这些基本 ...

  9. Django 文章标签功能

    使用第三方框架django-taggit为模型添加标签功能,此模块是一个可复用的应用 首先安装 https://github.com/alex/django-taggit 这是项目主页 pip ins ...

  10. Prism-超轻量的开源框架

    http://msdn.microsoft.com/en-us/library/ff648465.aspx prism 是微软模式与实践小组开发的一个进行MVVM模式开发,其中使用依赖注入等一些方法将 ...