2spring注解:@Lazy,@Conditional,@import,FactoryBean接口
大致的工程内容也来源于上一篇博文!
https://www.cnblogs.com/Mrchengs/p/10108603.html
1.@Lazy懒加载
懒加载:针对单实例的
单实例bean,默认在容器启动的时候创建对象
懒加载就是:容器启动的时候不创建对象,在第一次获取bean的时候创建对象,并且初始化、
Config2.class
@Configuration
public class Config2 {
@Bean("per")
public Person person(){
System.out.println("person对象创建完成");
return new Person("MrChengs",);
}
}
测试
@Test
public void test2(){
ApplicationContext app = new AnnotationConfigApplicationContext(Config2.class);
String [] names = app.getBeanDefinitionNames();
System.out.println("容器创建完成...");
}
此时并没有去获取Person对象!
此时容器创建之后就会实例化对象
person对象创建完成
容器创建完成...
使用懒加载:
@Configuration
public class Config2 {
@Lazy
@Bean("per")
public Person person(){
System.out.println("person对象创建完成");
return new Person("MrChengs",);
}
}
测试:
容器创建完成...
2..@Conditional
假设在windows和Linux操作系统获取自动的注册信息
@Configuration
public class Config2 {
@Lazy
@Bean("per")
public Person person(){
System.out.println("person对象创建完成");
return new Person("MrChengs",);
} //按照一定的条件判断,满足条件给容器注册
@Conditional({LinuxCondition.class})
@Bean("person1")
public Person person1(){
return new Person("Mr",);
} @Conditional({WindowsCondition.class})
@Bean("person2")
public Person person2(){
return new Person("Mx",);
}
}
public class LinuxCondition implements Condition{
//ConditionContext:判条件能使用的上下文
//AnnotatedTypeMetadata:注释信息
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
//能获取到IOC容器使用的beanfactory
ConfigurableListableBeanFactory c = context.getBeanFactory();
//获取加载器
ClassLoader classLoader = context.getClassLoader();
//获取当前的环境信息
//Environment environment = (Environment) context.getEnvironment();
//获取bean定义注册的类
BeanDefinitionRegistry resistroy = context.getRegistry();
//获取当前的操作系统
String name = context.getEnvironment().getProperty("os.name");
if(name.contains("Linux")){
return true;
}
return false;
}
}
public class WindowsCondition implements Condition{
//ConditionContext:判条件能使用的上下文
//AnnotatedTypeMetadata:注释信息
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
//能获取到IOC容器使用的beanfactory
ConfigurableListableBeanFactory c = context.getBeanFactory();
//获取加载器
ClassLoader classLoader = context.getClassLoader();
//获取当前的环境信息
//Environment environment = (Environment) context.getEnvironment();
//获取bean定义注册的类
BeanDefinitionRegistry resistroy = context.getRegistry();
String name = context.getEnvironment().getProperty("os.name");
if(name.contains("Win")){
return true;
}
return false;
}
}
@Test
public void test3(){
ApplicationContext app = new AnnotationConfigApplicationContext(Config2.class);
String [] names = app.getBeanNamesForType(Person.class);
for(String name : names){
System.out.println(name);
}
System.out.println("容器创建完成...");
}
per
person2
容器创建完成...
3.@import
@Configuration
@Import(Person.class)
public class Config2 { }
@Test
public void test3(){
ApplicationContext app = new AnnotationConfigApplicationContext(Config2.class);
String [] names = app.getBeanNamesForType(Person.class);
for(String name : names){
System.out.println(name);
}
System.out.println("容器创建完成...");
}
coom.MrChengs.bean.Person
容器创建完成...
源码:
public @interface Import {
/**
* {@link Configuration}, {@link ImportSelector}, {@link ImportBeanDefinitionRegistrar}
* or regular component classes to import.
*/
Class<?>[] value();
}
可以同时传入多个
@Import({Person.class,xxxxx})
直接注入到容器中
ImportSelector
是一个接口
public class MyImportSelector implements ImportSelector{
//返回值就是需要导入到容器中的全类名
//AnnotationMetadata : 就是获取标注@Import()注解类的所有信息
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{"coom.MrChengs.bean.Person"};
}
}
@Configuration
@Import({MyImportSelector.class})
public class Config2 {
}
@Test
public void test3(){
ApplicationContext app = new AnnotationConfigApplicationContext(Config2.class);
String [] names = app.getBeanDefinitionNames();
for(String name : names){
System.out.println(name);
}
System.out.println("容器创建完成...");
}
config2
coom.MrChengs.bean.Person
容器创建完成...
ImportBeanDefinitionRegistrar接口
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
//AnnotationMetadata:当前类的注解信息
//BeanDefinitionRegistry:注册类
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
//coom.MrChengs.bean.Person
//判断当前类是否有下面的这个bean
boolean a = registry.containsBeanDefinition("coom.MrChengs.bean.Person");
//如果没有我们进行注册
if(!a){
RootBeanDefinition beanDefinition = new RootBeanDefinition(Person.class);
//指定bean的名字
//注册一个bean
registry.registerBeanDefinition("peson", beanDefinition );
}
}
}
@Configuration
@Import({MyImportBeanDefinitionRegistrar.class})
public class Config2 {
}
@Test
public void test3(){
ApplicationContext app = new AnnotationConfigApplicationContext(Config2.class);
String [] names = app.getBeanDefinitionNames();
for(String name : names){
System.out.println(name);
}
System.out.println("容器创建完成...");
}
config2
peson
容器创建完成...
4.FactoryBean接口
public class PersonFactoryBean implements FactoryBean<Person>{
//返回一个person对象,这个对象会添加到容器中
public Person getObject() throws Exception {
// TODO Auto-generated method stub
return new Person();
}
//返回类型
public Class<?> getObjectType() {
// TODO Auto-generated method stub
return Person.class;
}
//是单例吗?
public boolean isSingleton() {
// TODO Auto-generated method stub
return false;
}
}
@Configuration
public class Config2 { @Bean
public PersonFactoryBean personFactoryBean(){
return new PersonFactoryBean();
}
}
@Test
public void test3(){
ApplicationContext app = new AnnotationConfigApplicationContext(Config2.class);
String [] names = app.getBeanDefinitionNames();
for(String name : names){
System.out.println(name);
}
Object name = app.getBean("personFactoryBean").getClass();
System.out.println(name);
System.out.println("容器创建完成...");
}
config2
personFactoryBean
class coom.MrChengs.bean.Person -> Object name = app.getBean("personFactoryBean").getClass();
容器创建完成...
得到的是Person对象
2spring注解:@Lazy,@Conditional,@import,FactoryBean接口的更多相关文章
- 二、Spring注解之@Conditional
Spring注解之@Conditional [1]@Conditional介绍 @Conditional是Spring4新提供的注解,它的作用是按照一定的条件进行判断,满足条件给容器注册bean. ...
- Springboot + redis + 注解 + 拦截器来实现接口幂等性校验
Springboot + redis + 注解 + 拦截器来实现接口幂等性校验 1. SpringBoot 整合篇 2. 手写一套迷你版HTTP服务器 3. 记住:永远不要在MySQL中使用UTF ...
- 使用AOP+自定义注解完成spring boot的接口权限校验
记使用AOP+自定义注解完成接口的权限校验,代码如下: pom文件添加所需依赖: 1 <dependency> 2 <groupId>org.aspectj</group ...
- Spring之BeanFactory和FactoryBean接口的区别
目录 一.BeanFactory接口 二.FactoryBean接口 1.简单实现 2.增强实现 3.FactoryBean的实际使用案例 三.总结 @ Spring框架中的BeanFactory ...
- java反射之获取所有方法及其注解(包括实现的接口上的注解),获取各种标识符备忘
java反射之获取类或接口上的所有方法及其注解(包括实现的接口上的注解) /** * 获取类或接口上的所有方法及方法上的注解(包括方法实现上的注解以及接口上的注解),最完整的工具类,没有现成的工具类 ...
- Spring5源码深度分析(二)之理解@Conditional,@Import注解
代码地址: 1.源码分析二主要分析的内容 1.使用@Condition多条件注册bean对象2.@Import注解快速注入第三方bean对象3.@EnableXXXX 开启原理4.基于ImportBe ...
- java反射注解妙用-获取所有接口说明
转载请注明出处:https://www.cnblogs.com/wenjunwei/p/10293490.html 前言 最近在做项目权限,使用shiro实现restful接口权限管理,对整个项目都进 ...
- WebService之CXF注解之二(Service接口)
ITeacherService.java: /** * @Title:ITeacherService.java * @Package:com.you.service * @Description:教师 ...
- spring注解第05课 FactoryBean
1.工厂bean调用 @Configuration public class MainConfig2 {/** * 使用Spring提供的 FactoryBean(工厂Bean); * 1).默认获取 ...
随机推荐
- .net EF框架 MySql实现实例
1.nuget中添加包EF和MySql.Data.Entity 2.config文件添加如下配置 1.配置entitframework节点(一般安装EF时自动添加) <entityFramewo ...
- Windows Server: 将虚拟机迁移到 Azure (以阿里云为例)
Azure 虚拟机能很容易地导出 vhd 并迁移到各种环境中,包含本地及云端环境,或者迁移至其他区域.这为开发.测试.扩展带来了极大的便利.本文以阿里云为例,阐述如何将Windows Server 的 ...
- [Java反射基础一]Class类的使用
任何一个类都是Class类的实例对象,这个实例对象有三种表示方式 第一种表示方式(任何一个类都有一个隐含的静态成员变量class): Class c1 = Foo.class; 第二种表示方式(已知该 ...
- 《Java并发编程实战》读书笔记(一)----- 简介
简史 早期的计算机中不包含操作系统,从头至尾都只执行一个程序,并且这个程序能访问计算机所有资源.随着计算机发展,操作系统的出现,使得计算机可以同时运行多个程序,并且每程序都在单独的进程内运行.为什么要 ...
- side Effect
副作用 side Effect 副作用是在计算结果的过程中,系统状态的一种变化,或者与外部世界进行的可观察的交互. 副作用可能包含,但不限于: 1.更改文件系统 2.往数据库里插入数据 3.发送一个h ...
- laravel后台注册登入
1.只需要在新安装的 Laravel 应用下运行 php artisan make:auth 和 php artisan migrate,这两个命令会生成用户登录注册所需要的所有东西 2.你会发现 h ...
- Python 爬虫的集中简单方式
- JVM jmap
需求:经常会因为OOM而导致系统挂掉,很多服务无法连接,所以准备了解一下. 参考:http://www.open-open.com/lib/view/open1390916852007.html 一. ...
- Yii2.0 新建项目通用准备工作
1.设置 cookieValidationKey 在 config/web.php 中 config 里有 components项中request有个cookieValidationKey需要配置参数 ...
- Android MediaPlayer播放音乐并实现进度条
提前工作,往sd卡里放音乐文件 1.布局文件main.xml <?xml version="1.0" encoding="utf-8"?> < ...