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

相关文章
|
18天前
|
Java API 数据安全/隐私保护
掌握Spring Boot中的@Validated注解
【4月更文挑战第23天】在 Spring Boot 开发中,@Validated 注解是用于开启和利用 Spring 的验证框架的一种方式,特别是在处理控制层的输入验证时。本篇技术博客将详细介绍 @Validated 注解的概念和使用方法,并通过实际的应用示例来展示如何在项目中实现有效的数据验证
26 3
|
19天前
|
前端开发 Java 开发者
深入理解Spring Boot中的@Service注解
【4月更文挑战第22天】在 Spring Boot 应用开发中,@Service 注解扮演着特定的角色,主要用于标识服务层组件。本篇技术博客将全面探讨 @Service 注解的概念,并提供实际的应用示例,帮助开发者理解如何有效地使用这一注解来优化应用的服务层架构
76 1
|
19天前
|
Java 开发者 Spring
深入理解Spring Boot的@ComponentScan注解
【4月更文挑战第22天】在构建 Spring Boot 应用时,@ComponentScan 是一个不可或缺的工具,它使得组件发现变得自动化和高效。这篇博客将详细介绍 @ComponentScan 的基本概念、关键属性及其在实际开发中的应用。
30 4
|
20天前
|
Java 开发者 Spring
深入理解 Spring Boot 中的 @EnableAutoConfiguration 注解:概念与实践
【4月更文挑战第21天】在 Spring Boot 项目中,@EnableAutoConfiguration 注解是实现自动配置的核心,它可以根据项目的依赖和配置,自动地配置 Spring 应用程序中的 Bean
33 3
|
21天前
|
Java API 网络架构
深入理解 Spring Boot 中的 @RestController 注解:概念与实践
【4月更文挑战第20天】在现代Web开发中,创建RESTful服务已成为常态。Spring Boot通过提供@RestController注解,极大简化了REST API的开发过程。本篇博客旨在详细介绍@RestController的概念、优势以及在Spring Boot项目中的具体应用方法。
31 8
|
21天前
|
Java 开发者 Spring
Spring Framework 中的 @Autowired 注解:概念与使用方法
【4月更文挑战第20天】在Spring Framework中,@Autowired 注解是实现依赖注入(Dependency Injection, DI)的一种非常强大的工具。通过使用 @Autowired,开发者可以减少代码中的引用绑定,提高模块间的解耦能力
33 6
|
21天前
|
XML Java 数据库
探索 Spring Boot 中的 @Configuration 注解:核心概念与应用
【4月更文挑战第20天】在 Spring Boot 项目中,@Configuration 注解扮演了一个关键角色,它标识一个类作为配置源,这些配置用于定义和管理 Spring 应用程序中的 Bean
42 7
|
16天前
|
缓存 Java Sentinel
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
|
5天前
|
运维 Java 程序员
Spring5深入浅出篇:基于注解实现的AOP
# Spring5 AOP 深入理解:注解实现 本文介绍了基于注解的AOP编程步骤,包括原始对象、额外功能、切点和组装切面。步骤1-3旨在构建切面,与传统AOP相似。示例代码展示了如何使用`@Around`定义切面和执行逻辑。配置中,通过`@Aspect`和`@Around`注解定义切点,并在Spring配置中启用AOP自动代理。 进一步讨论了切点复用,避免重复代码以提高代码维护性。通过`@Pointcut`定义通用切点表达式,然后在多个通知中引用。此外,解释了AOP底层实现的两种动态代理方式:JDK动态代理和Cglib字节码增强,默认使用JDK,可通过配置切换到Cglib
|
1天前
|
JSON 前端开发 Java
【JAVA进阶篇教学】第七篇:Spring中常用注解
【JAVA进阶篇教学】第七篇:Spring中常用注解