Spring Lookup注解的使用 详解Spring中Lookup注解的使用

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Spring Lookup注解的使用 详解Spring中Lookup注解的使用

小鲁班-JAVA开发   2021-10-26 我要评论
想了解详解Spring中Lookup注解的使用的相关内容吗,小鲁班-JAVA开发在本文为您仔细讲解Spring Lookup注解的使用的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Spring,Lookup注解的使用,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注解的方法时,会重写该方法,然后返回结果,所以我们自己定义的方法不管有没有返回值已经没关系了。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们