Spring中Lookup注解的使用

您所在的位置:网站首页 spring注解注入抽象类 Spring中Lookup注解的使用

Spring中Lookup注解的使用

2024-07-03 07:31| 来源: 网络整理| 查看: 265

我们知道在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. *

The default implementation checks whether the class is not an interface * and not dependent on an enclosing class. *

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注解的方法时,会生成代理类,重写该方法,然后返回结果,所以我们自己定义的方法不管有没有返回值已经没关系了。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3