• 面向切面编程的例子


    随意的测试结构

    //Aspect1.java
    package entity;
    
    public class Aspect1 {
    		public void before(){
    			System.out.println("业务执行前,before方法被执行");
    		}
    		
    		public void after(){
    			System.out.println("业务执行后,after方法被执行");
    		}
    }
    
    
    //Service1.java
    
    package service;
    
    public class Service1 {
    
    	public  void doSomething1(){
    		System.out.println("业务服务1");
    	}
    	
    	public  void doSomething2(){
    		System.out.println("业务服务2");
    	}
    
    	public Service1() {
    
    	}
    	
    	
    }
    
    
    
    //TestDemo .java
    
    package test;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import service.Service1;
    
    public class TestDemo {
    	
    	@Test
    	public void test(){
    		ApplicationContext ac= new ClassPathXmlApplicationContext("applicationContext.xml");
    		Service1 s=ac.getBean("s1",Service1.class);
    		s.doSomething1();
    		System.out.println();
    		s.doSomething2();
    	}
    
    }
    
    

    一:基于xml配置##

    ①applicationContext.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:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"  
    	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    	xmlns:aop="http://www.springframework.org/schema/aop" 
    	xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
    	xsi:schemaLocation="
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
    		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
    		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    
    <bean id="s1" class="service.Service1"></bean>
    <bean id="as1" class="entity.Aspect1"></bean>
    <aop:config>
    	<aop:aspect  ref="as1"><!--  设置切面类 ref表示切面的id -->
    	    <aop:pointcut expression="execution(* service.*.*(..))" id="dd"/>    <!--  设置执行某个执行点时运行切面    service.*.*(..)表示service包中的所有类的所有方法执行的时候都出发这个切面执行 -->
    	    <aop:after method="after"  pointcut-ref="dd"/> <!--  执行点之后运行切面类中的after方法 -->
    	    <aop:before method="before"  pointcut-ref="dd"/>	 <!--  执行点之前运行切面类中的before方法 -->
    	</aop:aspect>
    </aop:config>
    
    </beans>
    

    ②Junit运行TestDemo中的test方法得到结果

    二:基于注解的配置##

    ①applicationContext.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:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"  
    	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    	xmlns:aop="http://www.springframework.org/schema/aop" 
    	xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
    	xsi:schemaLocation="
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
    		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
    		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    		
    <bean id="s1" class="service.Service1"></bean>
    <bean id="as1" class="entity.Aspect1"></bean>
    <aop:aspectj-autoproxy  proxy-target-class="true"></aop:aspectj-autoproxy>
    </beans>
    

    ②:Aspect.java的代码,其他两个类不变

    package entity;
    
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.springframework.stereotype.Component;
    
    @Aspect
    public class Aspect1 {
    		
    		@Before("execution(* service.*.*(..))")
    		public void before(){
    			System.out.println("业务执行前,before方法被执行");
    		}
    		@After("execution(* service.*.*(..))")
    		public void after(){
    			System.out.println("业务执行后,after方法被执行");
    		}
    }
    
    

    ③:Junit运行TestDemo中的test方法得到结果

  • 相关阅读:
    二叉查找树
    Hash算法原理理解
    算法的时间复杂度
    解决Ubuntu14.04下Clementine音乐播放器不能播放wma文件的问题
    Ubuntu 14.04 开启启动器图标最小化功能
    Ubuntu14.04建立WiFi热点
    C语言运算符优先级
    老鸟的Python入门教程
    MD5算法步骤详解
    MD5算法
  • 原文地址:https://www.cnblogs.com/hts-technology/p/7244180.html
Copyright © 2020-2023  润新知