您的位置:首页 > 编程语言 > Java开发

Spring AOP Demo

2016-03-10 14:26 253 查看
/**
 * Created by ZhangZhengyi on 2016/3/10.
 * 目标类
 */
public class HelloWorldService  {
    public void sayHello() {
        System.out.println("这是目标类");
    }
}
/**
 * Created by ZhangZhengyi on 2016/3/10.
 *切面
 */
public class HelloWorldAspect {
    //前置通知
    public void beforeAdvice() {
        System.out.println("===========这是前置通知");
    }
    //后置最终通知
    public void afterFinallyAdvice() {
        System.out.println("===========这是后置通知");
    }
}/**
 * Created by ZhangZhengyi on 2016/3/10.
*/
public class AopTest {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml");
        HelloWorldService helloworldService = ctx.getBean("helloWorldService", HelloWorldService.class);
        helloworldService.sayHello();
    }
}
spring配置:
<?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-3.0.xsd            http://www.springframework.org/schema/aop            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">     <bean id="helloWorldService" class="foo.bar.Spring.AOP.HelloWorldService"/>    <bean id="aspect" class="foo.bar.Spring.AOP.HelloWorldAspect"/>    <!--<aop:aspectj-autoproxy proxy-target-class="true"/>-->    <aop:config>        <aop:pointcut id="pointcut" expression="execution(* foo.bar.Spring.AOP.*.*(..))"/>        <aop:aspect ref="aspect">            <aop:before method="beforeAdvice" pointcut="execution(* foo..*.*(..))"/>            <aop:after method="afterFinallyAdvice" pointcut="execution(* foo..*.*(..))"/>        </aop:aspect>    </aop:config>
文件结构:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: