ApplicationContextAware

ContextUtil.java

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了.

你可能感兴趣的:(ApplicationContextAware)