在 spring boot 中,当需要在一个单例(singleton)bean 中注入一个非单例(如 prototype)bean 时,直接使用 @autowired 只能获取到同一个实例。要解决这个问题,可以使用 @lookup 方法注入。具体做法是在单例 bean 中定义一个抽象方法或具体方法,并使用 @lookup 注解标记它;spring 容器会自动为该 bean 生成一个子类,重写该方法以返回一个新的目标 bean 实例。

@Lookup 方法注入的核心原理是什么
@Lookup 方法注入的核心原理是利用 CGLIB 动态代理生成当前 Bean 的子类。当调用被 @Lookup 注解的方法时,实际执行的是子类中重写的逻辑,该逻辑会从 Spring 容器中请求一个新的目标 Bean 实例。这解决了单例 Bean 无法直接获取多例 Bean 新实例的问题,因为普通的依赖注入只在初始化时进行一次。

// SingletonService.java
package com.example.service;
import org.springframework.beans.factory.annotation.Lookup;
import org.springframework.stereotype.Service;
@Service
public class SingletonService {
// 声明一个方法,Spring 会重写它以返回新的 PrototypeService 实例
@Lookup
public PrototypeService getPrototypeInstance() {
return null; // 方法体可以是空的,或者抛出异常,因为会被重写
}
public void doSomething() {
// 每次调用都会获得一个新的 PrototypeService 实例
PrototypeService instance = getPrototypeInstance();
System.out.println("Got instance: " + instance);
}
}
// PrototypeService.java
package com.example.service;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
@Service
@Scope("prototype")
public class PrototypeService {
public PrototypeService() {
System.out.println("Creating new PrototypeService instance");
}
}
运行上述代码,每次调用 doSomething() 都会打印出不同的对象哈希码,证明每次获取的都是新实例。如果去掉 @Lookup 而改用 @Autowired 注入 PrototypeService,则只会创建一个实例。
使用 @Lookup 有哪些限制和替代方案
使用 @Lookup 的主要限制是它要求当前 Bean 必须是 Spring 管理的 Bean,且该方法不能是 final、static 或 private 的。此外,由于使用了 CGLIB 代理,Bean 必须是非 final 的。替代方案包括使用 ObjectProvider(原 ObjectFactory)进行手动查找,或者使用 ApplicationContext 的 getBean() 方法。其中 ObjectProvider 是更推荐的方式,因为它不依赖 CGLIB 代理,性能更好且更灵活。

// AlternativeService.java
package com.example.service;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.stereotype.Service;
@Service
public class AlternativeService {
private final ObjectProvider<PrototypeService> prototypeServiceProvider;
// 通过构造函数注入 ObjectProvider
public AlternativeService(ObjectProvider<PrototypeService> prototypeServiceProvider) {
this.prototypeServiceProvider = prototypeServiceProvider;
}
public void doSomethingAlternative() {
// 每次调用 getObject() 都会获取一个新的 PrototypeService 实例
PrototypeService instance = prototypeServiceProvider.getObject();
System.out.println("Got alternative instance: " + instance);
}
}
对比两种方案,@Lookup 语法简洁但引入了代理开销,而 ObjectProvider 更加透明且无额外代理成本。在实际开发中,除非有特殊需求,否则优先推荐使用 ObjectProvider。
在 @Lookup 方法中如何处理参数传递
@Lookup 方法可以接受参数,这些参数会被传递给目标 Bean 的构造器或工厂方法。例如,如果 PrototypeService 有一个带参数的构造器,可以在 @Lookup 方法中定义相应的参数,Spring 会将这些参数传递给新创建的实例。这使得 @Lookup 不仅限于无参注入,还能支持有参注入场景。
// PrototypeServiceWithParam.java
package com.example.service;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
@Service
@Scope("prototype")
public class PrototypeServiceWithParam {
private final String name;
public PrototypeServiceWithParam(String name) {
this.name = name;
System.out.println("Creating PrototypeServiceWithParam with name: " + name);
}
public String getName() {
return name;
}
}
// SingletonServiceWithParam.java
package com.example.service;
import org.springframework.beans.factory.annotation.Lookup;
import org.springframework.stereotype.Service;
@Service
public class SingletonServiceWithParam {
@Lookup
public PrototypeServiceWithParam getPrototypeInstanceWithParam(String paramName) {
return null; // 会被重写,paramName 会传递给构造器
}
public void testParamInjection() {
PrototypeServiceWithParam instance = getPrototypeInstanceWithParam("testName");
System.out.println("Instance name: " + instance.getName());
}
}
在上述代码中,调用 testParamInjection() 会创建一个新的 PrototypeServiceWithParam 实例,并传入参数 "testName"。这验证了 @Lookup 方法支持参数传递,且参数值会在每次调用时动态确定。


















