Spring @Lookup

@Lookup注解允许Spring在运行时动态注入bean实例,主要用于单例bean访问原型bean。文章通过示例展示了如何使用@Lookup从单例bean中获取新的原型bean实例,并对比了与Provider的区别。同时,指出了@Lookup的限制,如不能用于抽象类或被@Bean管理的类,并提出在这些情况下可以考虑使用Provider作为替代方案。
摘要由CSDN通过智能技术生成

A method annotated with @Lookup tells Spring to return an instance of the method’s return type when we invoke it. Essentially, Spring will override our annotated method and use our method’s return type and parameters as arguments to BeanFactory#getBean(AAA.class).


How will our singleton Spring beans access prototype Spring beans?

Provider is certainly one way, though @Lookup is more versatile in some respects.

@Component
@Scope("prototype")
public class SchoolNotification {
    // ... prototype-scoped state
}

@Component
public class StudentServices {
    // ... member variables, etc.
    @Lookup
    public SchoolNotification getNotification() {
        return null;
    }
    // ... getters and setters
}

@Test
public void whenLookupMethodCalled_thenNewInstanceReturned() {
    // ... initialize context
    StudentServices first = this.context.getBean(StudentServices.class);
    StudentServices second = this.context.getBean(StudentServices.class);
       
    assertEquals(first, second); 
    assertNotEquals(first.getNotification(), second.getNotification()); 
}

Injecting Dependencies Procedurally, something that we cannot do with Provider.

@Component
@Scope("prototype")
public class SchoolNotification {
    private String name;
    private Collection<Integer> marks;

    public SchoolNotification(String name) {
        // ... set fields
    }

    // ... getters and setters

    public String addMark(Integer mark) {
        this.marks.add(mark);
    }
}

public abstract class StudentServices {
    private Map<String, SchoolNotification> notes = new HashMap<>();
 
    @Lookup
    protected abstract SchoolNotification getNotification(String name);

    public String appendMark(String name, Integer mark) {
        SchoolNotification notification = notes.computeIfAbsent(name, exists -> getNotification(name)));
        return notification.addMark(mark);
    }
}

First, We treat SchoolNotification a bit more like a Spring-aware method. It does this by implementing getSchoolNotification with a call to beanFactory.getBean(SchoolNotification.class, name).

Second, we can sometimes make the @Lookup annotated method abstract, like the above example.
Using abstract is a bit nicer-looking than a stub, but we can only use it when we don’t component-scan or @Bean-manage the surrounding bean:

@Test
public void whenAbstractGetterMethodInjects_thenNewInstanceReturned() {
    // ... initialize context

    StudentServices services = context.getBean(StudentServices.class);    
    assertEquals("PASS", services.appendMark("Alex", 89));
    assertEquals("FAIL", services.appendMark("Bethany", 78));
    assertEquals("PASS", services.appendMark("Claire", 96));
}

Limitations

Despite @Lookup‘s versatility, there are a few notable limitations:

1、@Lookup-annotated methods, like getNotification, must be concrete when the surrounding class is component-scanned. This is because component scanning skips abstract beans.
2、@Lookup-annotated methods won’t work at all when the surrounding class is @Bean-managed.

In those circumstances, if we need to inject a prototype bean into a singleton, we can look to Provider as an alternative.


参考:@Lookup Annotation in Spring

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值