013、简单了解application context

本文围绕Spring的ApplicationContext展开,介绍其由BeanFactory派生,扩展了更多功能。通过aop和application context展示父子context差异,包括上下文实例化、aspect声明等内容,调试发现aop增强仅对声明上下文中的bean起作用,还列举了常用接口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、spring-application-context

ApplicationContext接口由BeanFactory接口派生而来,提供了更多面向实际的功能。

ApplicationContext继承了HierachicalBeanFactory和ListableBeanFactory接口,在此基础上,还通过其他接口扩展了BeanFactory的功能。

 

下面通过aop和application context展示父子context不同之处

1. 上下文实例化

# 父context使用注解配置上下文
AnnotationConfigApplicationContext parentContext = new AnnotationConfigApplicationContext(TestConfig.class);
# 子context使用xml配置上下文		
ClassPathXmlApplicationContext childContext = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"}, parentContext);

 

2. aspect声明

@Aspect
@Slf4j
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TestAspect {
	
	private String context;

	@AfterReturning("bean(testBean*)")
	public void afterReturning() {

		log.info("aspect {}: after hello!", context);
		
	}

}

 

3. 被增强的类

@Data
@NoArgsConstructor
@AllArgsConstructor
@Slf4j
public class TestBean {
	
	private String context;
	
	public void hello() {
		log.info("hello, {}", context);
	}

}

 

4. 注解实例化bean对象

@Configuration
@EnableAspectJAutoProxy
public class TestConfig {
	
	@Bean
	public TestBean testBeanX() {
		return new TestBean("testBeanX AnnotationConfigApplicationContext");
	}
	
	@Bean
	public TestBean testBeanY() {
		return new TestBean("testBeanY AnnotationConfigApplicationContext");
	}
	
	
	@Bean
    public TestAspect testAspect() {
        return new TestAspect("TestAspect AnnotationConfigApplicationContext");
    }
	
}

 

5. xml配置实例化bean对象

	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

	<bean id="testBeanX" class="com.example.springcontext.TestBean">
		<constructor-arg name="context" value="testBeanX ClassPathXmlApplicationContext" />
	</bean>

	<bean id="testAspect" class="com.example.springcontext.TestAspect">
		<constructor-arg name="context" value="TestAspect ClassPathXmlApplicationContext" />
	</bean>

 

6. 测试

		TestBean bean = parentContext.getBean("testBeanX", TestBean.class);
		
		bean.hello();
		
		log.info("-------------------");
		
		bean = childContext.getBean("testBeanX", TestBean.class);
		
		bean.hello();
		log.info("-------------------");
		
		bean = childContext.getBean("testBeanY", TestBean.class);
		bean.hello();

 

调试不同配置,可得 aop 增强只会对在其所声明的上下文中的bean起作用。

源码

Fork me on Gitee

 

二、关于上下⽂常⽤的接⼝

BeanFactory

  • DefaultListableBeanFactory

ApplicationContext

  • ClassPathXmlApplicationContext
  • FileSystemXmlApplicationContext
  • AnnotationConfigApplicationContext

WebApplicationContext

 

web上下文层次

ee5a53ae4b0cb834e523f3fd44eac9af93e.jpg

web配置方式

1d72aac7ebdc379377e149d35d92d73ca66.jpg

servlet3.0非配置方式

3f873b56d629e8a6f76e50da995aa85a6ed.jpg

转载于:https://my.oschina.net/tita/blog/3065133

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值