@Lookup
介绍
@Lookup
是 Spring 框架中的一个注解,用于在 Spring 管理的 Bean 中实现方法级别的依赖注入。它通常用于解决单例 Bean 需要依赖原型(Prototype)Bean 的情况。
@Lookup
注解允许你在单例 Bean 中注入原型 Bean。通过在方法上使用 @Lookup
注解,Spring 会在运行时动态地生成一个代理方法,该方法会在每次调用时返回一个新的原型 Bean 实例。
源代码
package org.springframework.beans.factory.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Lookup {
/**
* 此注释属性可能建议查找的目标Bean名称。
* 如果未指定,将根据以下内容解析目标 bean:
* 标注方法的返回类型声明。
*/
String value() default "";
}
示例代码
DemoPrototypeBean
定义一个原型 Bean
package com.yang.SpringTest.annotation.lookupLearn;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
/**
* <p>【spring】@Lookup注解学习</p>
* 定义一个原型 Bean
*
* @author By: chengxuyuanshitang <br>
* @Package: com.yang.SpringTest.annotation.lookupLearn <br>
* @CreateTime: 2024-11-20 13:58 <br>
*/
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class DemoPrototypeBean {
public void doSomething() {
System.out.println("DemoPrototypeBean 已经开始工作... 。toString方法输出:" + this.toString());
}
}
DemoSingletonBean
定义一个单例 Bean,并在其中使用 @Lookup 注解
package com.yang.SpringTest.annotation.lookupLearn;
import org.springframework.beans.factory.annotation.Lookup;
import org.springframework.stereotype.Component;
/**
* <p>【spring】@Lookup注解学习</p>
* 定义一个单例 Bean,并在其中使用 @Lookup 注解
*
* @author By: chengxuyuanshitang <br>
* @Package: com.yang.SpringTest.annotation.lookupLearn <br>
* @CreateTime: 2024-11-20 13:59 <br>
*/
@Component
public class DemoSingletonBean {
/**
* 方法会被 Spring代理,每次调用时返回一个新的 DemoPrototypeBean 实例
*
* @return DemoPrototypeBean
*/
@Lookup
public DemoPrototypeBean getDemoPrototypeBean() {
// 返回值会被忽略
return null;
}
public void useDemoPrototypeBean() {
DemoPrototypeBean prototypeBean = getDemoPrototypeBean();
prototypeBean.doSomething();
}
}
LookupAppConfig
Spring 上下文配置
package com.yang.SpringTest.annotation.lookupLearn;
/**
* <p>【spring】@Lookp注解学习</p>
* Spring 上下文配置
*
* @author By: chengxuyuanshitang <br>
* @Package: com.yang.SpringTest.annotation.lookupLearn <br>
* @CreateTime: 2024-11-20 14:01 <br>
*/
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.yang.SpringTest.annotation.lookupLearn")
public class LookupAppConfig {
}
LookupTest
测试 SingletonBean 是否每次都能获取到新的 PrototypeBean 实例
package com.yang.SpringTest.annotation.lookupLearn;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.util.Arrays;
/**
* <p>【spring】@Lookup注解学习</p>
* 测试 SingletonBean 是否每次都能获取到新的 PrototypeBean 实例
*
* @author By: chengxuyuanshitang <br>
* @Package: com.yang.SpringTest.annotation.lookupLearn <br>
* @CreateTime: 2024-11-20 14:02 <br>
*/
@Slf4j
public class LookupTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(LookupAppConfig.class);
log.info("**************** IOC容器启动完成....");
String[] definitionNames = context.getBeanDefinitionNames();
Arrays.stream(definitionNames).forEach((definitionName) -> System.out.println("=====" + definitionName));
DemoSingletonBean demoSingletonBean = context.getBean(DemoSingletonBean.class);
demoSingletonBean.useDemoPrototypeBean();
demoSingletonBean.useDemoPrototypeBean();
context.close();
}
}