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. 在 Ubuntu 上使用微信客户端

    原文地址: http://www.myzaker.com/article/5979115d1bc8e08c30000071/ 在这个快速信息交互时代,无论是工作还是生活,都需要频繁的网络社交,而在中国 ...

  2. APICloud开发

    2018-06-16 今天在看房角石APPIOS版本闪退的问题,后来定位到了 elements.find("video").attr("preload", &q ...

  3. 【搜索】C - Catch That Cow

    #include<stdio.h> #include<string.h> struct A{ int state; int step; }queue[]; // 结构体数组用来 ...

  4. Alpha 冲刺 (2/10)

    队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 协助前端界面的开发 搭建测试用服务器的环境 完成 ...

  5. 20155312 2006-2007-2 《Java程序设计》第六周学习总结

    20155312 2006-2007-2 <Java程序设计>第六周学习总结 课堂笔记 学习进程 周一看视频-2h 周二以代码为中心看书-3h 课后选择题-5h 教材指导 应试 Linux ...

  6. Django框架之验证码生成示例

    一.生成随机颜色的图片 1)前端代码展示 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  7. c++中的log函数

    引入#include<cmath> 以e为底:log(exp(n)) 以10为底:log10(n) 以m为底:log(n)/log(m)

  8. Codeforces Round #542(Div. 2) CDE 思维场

    C https://codeforces.com/contest/1130/problem/C 题意 给你一个\(n*m\)(n,m<=50)的矩阵,每个格子代表海或者陆地,给出在陆地上的起点终 ...

  9. PC Access的使用

    需要copy xxx.dll  到windows/syswow64 目录下 运行com注册 启动电脑后,自动锁定(在启动目录下架锁定程序) using System; using System.Col ...

  10. c++中typedef、define、const、inline之间的区别

    1.typedef和#define的区别 typedef int* pInt; , b = ; const pInt p1 = &a; //p1是常量指针 pInt const p2 = &a ...