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. (O)JS高阶函数应用——函数节流

    在一些函数需被频繁调用的场景,如:window.onresize.mousemove.scroll滚动事件.上传进度等等,操作频繁导致性能消耗过高,而造成浏览器卡顿现象,我们可以通过函数节流的方式解决 ...

  2. xcode资源管理

    1. 在根目录放图片. UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ok.pn ...

  3. base64编码是什么1

    首先明确一点base64 是一种编码格式.就想UNICODE一样,能在电脑上表示所有字符,或者换句话说通过编码能让电脑理解你想要表示的字符(因为电脑只知道0和1 ) 就像ascII 中 0100 00 ...

  4. Maven中maven-source-plugin,maven-javadoc-plugin插件的使用

    摘要:今天领导说要把项目通过maven生产源码包和文档包并发布到自己的私服上,经过查看mavne官网发现有两个maven插件可以做到这些工作,一个是maven-source-plugin,另一个是ma ...

  5. gunicorn配置文件

    最近使用gunicorn部署,感觉用命令参数方式启动比较繁琐,而且有时候就忘了以前怎么设置的了.一笑... 上stackoverflow查了查,找到了一个官方示例,在这里. 官方解释在这里. 记在这里 ...

  6. Globalization and accessibility for tile and toast notifications (Windows Store apps)

    http://msdn.microsoft.com/en-us/library/windows/apps/hh831183.aspx 做 HighContrast时,采用以下分目录方式: /Proje ...

  7. CMDB 配置管理数据库

  8. Ubuntu下 QT添加外部链接库(.so文件)示例

    参考:https://blog.csdn.net/KKALL1314/article/details/81915354 https://forum.qt.io/topic/80301/file-not ...

  9. Keepalived+Nginx高可用架构配置

    1.yum install -y libnfnetlink-devel2.yum -y install libnl libnl-devel 3.yum -y install openssl-devel ...

  10. 微信分享接口 略缩图 php

    php插件下载地址:  https://files.cnblogs.com/files/fan-bk/jssdk_php.rar 提示:如果插件里面的jssdk.php函数 file_get_cont ...