Spring 常用注解之@Lookup

简介: Spring 常用注解之@Lookup

正文


01 @Lookup的作用


在平时写代码的过程中,经常会出现多例的模式。例如,可以指定Spring配置文件@Scope("prototype")的多例模式,如下。


@Component
@Scope("prototype")
public class PrototypeBean {
...
}

但是假若如果在一个单例对象中使用这个实例,就会出现不是想要的结果如下:


@Component
@Scope("singleton")
public class SingletonBean {
    @Resource
    private PrototypeBean prototypeBean;
    public PrototypeBean getPrototypeBean() {
        return prototypeBean;
    }
}


SingletonBean对象中每次获取PrototypeBean对象,发现都是一个确定的值,如下测试:


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:beans.xml"})
public class SpringTest {
    @Resource
    private SingletonBean singletonBean;
    @Test
    public void test01() {
        System.out.println(singletonBean.getPrototypeBean());
        System.out.println(singletonBean.getPrototypeBean());
        System.out.println(singletonBean.getPrototypeBean());
    }
}


测试结果如下,可以得到PrototypeBean对象每一次都是相同的,那我们如何可以获取不同的呢?实际上,这个注解就是简化每一位程序员的操作,可以顺利地达到每一次都是不同对象的目的。


...
vip.breakpoint.bean.PrototypeBean@3c419631
vip.breakpoint.bean.PrototypeBean@3c419631
vip.breakpoint.bean.PrototypeBean@3c419631


02 采用ApplicationContextAware实现


修改SingletonBean对象代码如下:


@Component
@Scope("singleton")
public class SingletonBean implements ApplicationContextAware {
    private ApplicationContext applicationContext;
    public PrototypeBean getPrototypeBean() {
        return this.applicationContext.getBean(PrototypeBean.class);
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}


测试代码保持不变,测试结果如下:


七月 14, 2022 9:29:17 下午 org.springframework.test.context.support.AbstractTestContextBootstrapper getDefaultTestExecutionListenerClassNames
信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
七月 14, 2022 9:29:17 下午 org.springframework.test.context.support.AbstractTestContextBootstrapper getTestExecutionListeners
信息: Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@224edc67, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@14acaea5, org.springframework.test.context.support.DirtiesContextTestExecutionListener@46d56d67, org.springframework.test.context.event.EventPublishingTestExecutionListener@d8355a8]
vip.breakpoint.bean.PrototypeBean@3e92efc3
vip.breakpoint.bean.PrototypeBean@1622f1b
vip.breakpoint.bean.PrototypeBean@72a7c7e0
Process finished with exit code 0


可以看出,使用ApplicationContextAware方式成功了,并且每一次获取的PrototypeBean对象都是不同的。


03 使用@Lookup注解实现


修改后SingletonBean对象的代码如下:


@Component
@Scope("singleton")
public class SingletonBean {
    @Lookup
    public PrototypeBean getPrototypeBean() {
        return null;
    }
}


测试代码不变,一起看一下结果吧!


七月 14, 2022 9:30:58 下午 org.springframework.test.context.support.AbstractTestContextBootstrapper getDefaultTestExecutionListenerClassNames
信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
七月 14, 2022 9:30:58 下午 org.springframework.test.context.support.AbstractTestContextBootstrapper getTestExecutionListeners
信息: Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@475530b9, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@1d057a39, org.springframework.test.context.support.DirtiesContextTestExecutionListener@26be92ad, org.springframework.test.context.event.EventPublishingTestExecutionListener@4c70fda8]
vip.breakpoint.bean.PrototypeBean@7b2bbc3
vip.breakpoint.bean.PrototypeBean@a1153bc
vip.breakpoint.bean.PrototypeBean@1aafa419
Process finished with exit code 0


可以从测试的结果看出,采用@Lookup注解实现了获取不同的对象,达到了最开始的目的。


03 后记


@Lookup注解简化了程序员的操作,它的主要的实现的原理是,当遇到这个注解的时候,会从上下文中根据方法的返回类型进行获取注册在IOC容器中的实例对象,也就是调用getbean的方法。


27.png

目录
打赏
0
0
0
0
4
分享
相关文章
SpringBoot入门(8) - 开发中还有哪些常用注解
SpringBoot入门(8) - 开发中还有哪些常用注解
75 0
Spring IOC—基于注解配置和管理Bean 万字详解(通俗易懂)
Spring 第三节 IOC——基于注解配置和管理Bean 万字详解!
124 26
SpringBoot缓存注解使用
Spring Boot 提供了一套方便的缓存注解,用于简化缓存管理。通过 `@Cacheable`、`@CachePut`、`@CacheEvict` 和 `@Caching` 等注解,开发者可以轻松地实现方法级别的缓存操作,从而提升应用的性能和响应速度。合理使用这些注解可以大大减少数据库的访问频率,优化系统性能。
194 89
【Spring】方法注解@Bean,配置类扫描路径
@Bean方法注解,如何在同一个类下面定义多个Bean对象,配置扫描路径
185 73
|
23天前
|
SpringBoot:SpringBoot通过注解监测Controller接口
本文详细介绍了如何通过Spring Boot注解监测Controller接口,包括自定义注解、AOP切面的创建和使用以及具体的示例代码。通过这种方式,可以方便地在Controller方法执行前后添加日志记录、性能监控和异常处理逻辑,而无需修改方法本身的代码。这种方法不仅提高了代码的可维护性,还增强了系统的监控能力。希望本文能帮助您更好地理解和应用Spring Boot中的注解监测技术。
58 16
【SpringFramework】Spring IoC-基于注解的实现
本文主要记录基于Spring注解实现IoC容器和DI相关知识。
68 21
【Spring】获取Bean对象需要哪些注解
@Conntroller,@Service,@Repository,@Component,@Configuration,关于Bean对象的五个常用注解
【Spring配置】idea编码格式导致注解汉字无法保存
问题一:对于同一个项目,我们在使用idea的过程中,使用汉字注解完后,再打开该项目,汉字变成乱码问题二:本来a项目中,汉字注解调试好了,没有乱码了,但是创建出来的新的项目,写的注解又成乱码了。
|
4月前
|
SpringBoot必须掌握的常用注解!
SpringBoot必须掌握的常用注解!
191 4
SpringBoot必须掌握的常用注解!
Spring MVC核心:深入理解@RequestMapping注解
在Spring MVC框架中,`@RequestMapping`注解是实现请求映射的核心,它将HTTP请求映射到控制器的处理方法上。本文将深入探讨`@RequestMapping`注解的各个方面,包括其注解的使用方法、如何与Spring MVC的其他组件协同工作,以及在实际开发中的应用案例。
81 4

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等