ApplicationContextAware
2016-03-31 00:00
288 查看
ContextUtil.java
AspectJStudent.java
BootMain.java
spring-test.xml
output
通过实现ApplicationContextAware接口,我们可以动态获取被spring管理的bean.
记得实现ApplicationContextAware的setApplicationContext方法,spring会在初始化ContextUtil类的时候帮我们注入applicationContext.
ContextUtil再提供相应的getbean方法,就能动态获取spring管理的bean了.
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * Created by wangrui04 on 2016/3/31. */ public class ContextUtil implements ApplicationContextAware { private ApplicationContext applicationContext; public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } public Object getBean(String beanName){ return applicationContext.getBean(beanName); } public Object getBean(String beanName,Class cls){ return applicationContext.getBean(beanName,cls); } }
AspectJStudent.java
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; /** * Created by wangrui04 on 2016/3/31. */ @Aspect public class AspectJStudent { @Pointcut("execution (* Student.say(..))") public void pointcut(){} @Around("pointcut()") public void around (ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("AspectJ around before"); joinPoint.proceed(); System.out.println("AspectJ around after"); } @Before("pointcut()") public void before(JoinPoint joinPoint){ System.out.println("AspectJ before"); } @After("pointcut()") public void after(JoinPoint joinPoint){ System.out.println("AspectJ after"); } @AfterReturning("pointcut()") public void afterReturning(JoinPoint joinPoint){ System.out.println("AspectJ after returning"); } }
BootMain.java
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; /** * Created by wangrui04 on 2016/3/31. */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:spring-test.xml"}) public class BootMain { @Autowired private ContextUtil contextUtil; @Test public void testContextUtil(){ Person student = (Person)contextUtil.getBean("student"); student.say(); } }
spring-test.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <aop:aspectj-autoproxy/> <bean id="student" class="Student"/> <bean id="aspectJStudent" class="AspectJStudent"/> <bean class="ContextUtil"/> </beans>
output
AspectJ around before AspectJ before hello world! AspectJ around after AspectJ after AspectJ after returning
通过实现ApplicationContextAware接口,我们可以动态获取被spring管理的bean.
记得实现ApplicationContextAware的setApplicationContext方法,spring会在初始化ContextUtil类的时候帮我们注入applicationContext.
ContextUtil再提供相应的getbean方法,就能动态获取spring管理的bean了.
相关文章推荐
- 获取Spring容器中的Bean
- spring中的event listener模式和解耦
- Spring中ApplicationContextAware使用说明
- 启动tomcat的同时,加载类的方法:采用Spring中的ApplicationContextAware接口
- The method replace(int, Fragment, String) in the type FragmentTransaction is not applicable...
- 【2】JAVA---地址App小软件(ListPanel.class)(表现层)
- ios 第三方框架
- iOS中的摇一摇功能
- 小程序 - 链表检测环/链表是否交叉 等
- android Application实例 无法强转 自定义的类
- android Application实例 无法强转 自定义的类
- Android 工程运行报错 Conversion to Dalvik format failed: Unable to execute dex: java.nio.BufferOv
- Android 工程运行报错 Conversion to Dalvik format failed: Unable to execute dex: java.nio.BufferOv
- android studio 配置dragger
- android studio 配置dragger
- android listview onItemClick不执行
- android listview onItemClick不执行
- android高效读取大图片 防止OOM
- android高效读取大图片 防止OOM
- ScrollView内嵌套 WebView WebView 拦截滚动事件问题