向Spring容器中注册组件的方法汇总小结
1、通过xml定义
<bean class="">
<property name="" value=""></property>
</bean>
2、通过注解
这种方式比较常见,通常用@Controller、@Component、@Service等等
3、通过@Bean注解
比如下面的代码往容器中注册一个Person对象
@Bean
public Person person(){
return new Person("张三", 20);
}
默认情况下,使用方法【person()】名person作为Person对象的注册id
也可以通过修改方法名或者使用@Bean(“customBeanName”)
4、通过实现FactoryBean接口
public interface FactoryBean<T> {
T getObject() throws Exception;
Class<?> getObjectType();
boolean isSingleton();
}
// Sample
public class PersonFactoryBean implements FactoryBean<Person> {
....
}
实现上述接口的3个方法,并把PersonFactoryBean注册到容器中,就可以把Person也注册到容器中。
具体创建Person过程的源码可以看FactoryBeanRegistrySupport#getObjectFromFactoryBean方法。
// 如下代码拿到的是Person对象
applicationContext.getBean("personFactoryBean")
// 如果想要拿到PersonFactoryBean对象,可以再前面加&
applicationContext.getBean("&personFactoryBean")
5、通过@Import注解
先来看看源码:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {
/**
* {@link Configuration}, {@link ImportSelector}, {@link ImportBeanDefinitionRegistrar}
* or regular component classes to import.
*/
Class<?>[] value();
}
源码注释写的也很清楚,可以引入 配置类、ImportSelector、ImportBeanDefinitionRegistrar,甚至是普通class。 通过@Import,我们可以使用如下方式注册组件:
@Import({Person.class, MyImportSelector.class,MyImportBeanDefinitionRegistrar.class})
其中:
MyImportSelector实现了ImportSelector接口,selectImports方法返回类全名的String[]都会被注册到容器中
MyImportBeanDefinitionRegistrar实现了ImportBeanDefinitionRegistrar接口
// Sample
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
// 指定Bean定义信息
RootBeanDefinition beanDefinition = new RootBeanDefinition(Person.class);
// 注册一个Bean,指定bean名
registry.registerBeanDefinition("person", beanDefinition);
}
}
```
这是一个非常重要的注解,在Spring源码中,哪哪都能看到他的身影。
如 @EnableAspectJAutoProxy注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AspectJAutoProxyRegistrar.class)
public @interface EnableAspectJAutoProxy {
...
}
EnableAspectJAutoProxy通过@Import引入了AspectJAutoProxyRegistrar类[实现了 ImportBeanDefinitionRegistrar],这个Registrar里面又会向Spring容器中注册AnnotationAwareAspectJAutoProxyCreator(Spring aop注解实现的功臣)。
如 @EnableWebMvc注解。通过Import引入的是一个配置类
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
}
如 @EnableAsync注解。通过Import引入的是AsyncConfigurationSelector[实现了ImportSelector接口]
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AsyncConfigurationSelector.class)
public @interface EnableAsync {
...
}
向Spring容器中注册组件的方法汇总小结的更多相关文章
- Spring注解驱动开发04(给容器中注册组件的方式)
给容器中注册组件的方式 1. 组件注解标注 + 包扫描(适用于自己写的类) //控制层组件 @Controller public class PersonController { } //业务逻辑层组 ...
- 2、组件注册-@Configuration&@Bean给容器中注册组件
2.组件注册-@Configuration&@Bean给容器中注册组件 2.1 创建maven项目 spring-annotation pom.xml文件添加 spring-context 依 ...
- 【String注解驱动开发】如何按照条件向Spring容器中注册bean?这次我懂了!!
写在前面 当bean是单实例,并且没有设置懒加载时,Spring容器启动时,就会实例化bean,并将bean注册到IOC容器中,以后每次从IOC容器中获取bean时,直接返回IOC容器中的bean,不 ...
- 【String注解驱动开发】面试官让我说说:如何使用FactoryBean向Spring容器中注册bean?
写在前面 在前面的文章中,我们知道可以通过多种方式向Spring容器中注册bean.可以使用@Configuration结合@Bean向Spring容器中注册bean:可以按照条件向Spring容器中 ...
- spring注解开发:容器中注册组件方式
1.包扫描+组件标注注解 使用到的注解如下,主要针对自己写的类 @Controller @Service @Repository @Component @ComponentScan 参考 spring ...
- 【Spring注解开发】组件注册-使用@Configuration和@Bean给容器中注册组件
写在前面 在之前的Spring版本中,我们只能通过写XML配置文件来定义我们的Bean,XML配置不仅繁琐,而且很容易出错,稍有不慎就会导致编写的应用程序各种报错,排查半天,发现是XML文件配置不对! ...
- 一、Spring之组件注册-@Configuration&@Bean给容器中注册组件
xml配置方式 首先我们创建一个实体类Person public class Person { private String name; private Integer age; private St ...
- spring 给容器中注册组件的几种方式
1.@Bean 导入第三方的类或包的组件 2.包扫描+组件的标注注解(@ComponentScan: @Controller,@service,@Reponsitory,@Componet), 自己写 ...
- Servlet自动注入Spring容器中的Bean解决方法
很多情况在进行Web开发的时候需要自己手写Servlet来完成某些功能,而servlet有需要注入Spring容器中的某些bean,这是每次都要手动获取比较麻烦,这里有一个解决方案,只需要写一个ser ...
随机推荐
- delphi 窗体最大化 最小化
procedure TForm1.SpeedButton2Click(Sender: TObject); begin sendmessage(form1.WindowHandle,WM_SYSCOMM ...
- OO第一次博客总结
虽然早在开学之前就已耳闻过OO这门课的威力,也在寒假自学了一些java的语法,但在真正面对OO这样的工程训练时才发现寒假所学的那点语法简直不值一提,也深刻的感受到在这个过程中自己的提升确实很快,毕竟d ...
- Python 每日随笔
使用python已经有3个月了,带总体来说,还是python菜鸟,今天发现了一个好玩的东西,记录下来,有时间深入研究一下. 关于Metaclass 不得不说python 的元类很有意思,可以做很多有趣 ...
- 「总结」 MLEAutoMaton的各种板子总结
太菜了,所以咕了,欢迎评论留言... 网络流(不全) 自适应Simpson
- wordpress升级版本时出现错误“Maximum execution time of 30 seconds exceeded”
wordpress版本是4.9,之前升级5.0时就提示这个错误了,但因为我用的第三方主题,所以也没想去解决,也担心升级了wp版本后主题出问题. 现在wp版本已经到了5.2了,我闲着无聊就又点了升级,结 ...
- SpringAop实操之记录关键业务请求数据
AOP,中文名称,切面.在不影响业务代码情况下,实现想要的功能,是个真炫酷的事. aop是个好东西,记录日志是必须的. 记录数据也一样的,那么也是可以用aop来实现的,这里借助注解一起解决问题吧. 因 ...
- 产品经理聊产品--mac book pro 2018 初体验
工作前几年,使用电脑,基本上都是微软的操作系统,自从从大厂出来之后,才逐渐熟悉使用linux,到现在基本上都是基本上一个月windows平台基本不需要开机就可以,可以说基本上被ubuntu的简洁和实用 ...
- Emmet/Zen Coding 快速入门说明
快速参考 以下是支持的特性: ele creates an HTML element tag 展开一个HTML元素标签 # creates an id attribute 作用于元素标签,展开一个id ...
- 告诉你,Spring Boot 真是个牛逼货!
现在 Spring Boot 非常火,各种技术文章,各种付费教程,多如牛毛,可能还有些不知道 Spring Boot 的,那它到底是什么呢?有什么用?今天给大家详细介绍一下. Spring Boot ...
- [Leetcode]895.最大频率栈
Problem 实现 FreqStack,模拟类似栈的数据结构的操作的一个类. FreqStack 有两个函数: push(int x),将整数 x 推入栈中. pop(),它移除并返回栈中出现最频繁 ...