详解Spring中Lookup注解的使用
我们知道在spring容器中单独的一个抽象类是不能成为一个bean的,那么有没有办法呢?这个时候我们可以使用Lookup注解,我们可以看下spring的扫描bean部分逻辑。我们知道在spring中要想成为一个bean,必须先生成BeanDefinition对象,如果一个抽象类中没有含有Lookup注解的方法,在spring扫描时就会被排除掉。
/**
* 1、判断是不是独立的类,非静态内部类则无法生成bean,
* 2、判断是不是接口或者抽象类(有一种特殊情况),是则无法生成
* 3、判断如果是抽象类,但是里面有某个方法上面加油@lookup注解,则也可以生成bean
* Determine whether the given bean definition qualifies as candidate.
* <p>The default implementation checks whether the class is not an interface
* and not dependent on an enclosing class.
* <p>Can be overridden in subclasses.
* @param beanDefinition the bean definition to check
* @return whether the bean definition qualifies as a candidate component
*/
protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
AnnotationMetadata metadata = beanDefinition.getMetadata();
return (metadata.isIndependent() && (metadata.isConcrete() ||
(metadata.isAbstract() && metadata.hasAnnotatedMethods(Lookup.class.getName()))));
}
下面我们就验证下,不使用Lookup注解的情况
@Component
public abstract class C1 {
}
运行结果报错

在抽象类中随便写个方法,然后方法上面增加Lookup注解
@Component
public abstract class C1 {
@Lookup
public void a(){}
}
运行结果,正常输出,通过cglib代理生成新的类

但是一般很少这样用,另外一种场景可能会用到。在某个单例bean中使用另外一个bean对象,但是每次又想返回的对象不同。但是spring在容器中注入bean的时候,scope默认的是单例模式,也就是说在整个应用中只能创建一个实例。当scope为PROTOTYPE类型的时候,在每次注入的时候会自动创建一个新的bean实例。但是当一个单例模式的bean去引用PROTOTYPE类型的bean的时候,PROTOTYPE类型的bean也会变成单例。
@Component
public class D3 {
@Autowired
private E4 e4;
public void a(){
System.out.println(this.e4);
}
}
@Component
public class E4 {
}
输出结果,可以看到每次打印出来的对象是同一个

使用Lookup注解
@Component
public class D3 {
public void a(){
System.out.println(this.a1());
}
@Lookup
public E4 a1(){
return null;
}
}
运行输出结果,每次输出的结果已经不相同了,已经达到了我们的需求

这是什么原因导致的呢?还有就是我们a1方法返回的是空,但是输出的结果为啥也有值呢?
因为spring在遇到这种标有Lookup注解的方法时,会重写该方法,然后返回结果,所以我们自己定义的方法不管有没有返回值已经没关系了。
到此这篇关于详解Spring中Lookup注解的使用的文章就介绍到这了,更多相关Spring Lookup注解的使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
一文了解Java读写锁ReentrantReadWriteLock的使用
ReentrantReadWriteLock称为读写锁,它提供一个读锁,支持多个线程共享同一把锁。这篇文章主要讲解一下ReentrantReadWriteLock的使用和应用场景,感兴趣的可以了解一下2022-10-10
Java concurrency集合之ConcurrentLinkedQueue_动力节点Java学院整理
这篇文章主要介绍了Java concurrency集合之ConcurrentLinkedQueue,需要的朋友可以参考下2017-06-06
SpringBoot使用ExceptionHandler做异常处理
这篇文章主要介绍了SpringBoot使用ExceptionHandler做异常处理,这篇文章通过多种方法案例来介绍该项技术的使用,需要的朋友可以参考下2021-06-06
Spring Security permitAll()不允许匿名访问的操作
这篇文章主要介绍了Spring Security permitAll()不允许匿名访问的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-06-06


最新评论