1. 程式人生 > >經典三層框架初識(二)---Spring 4 Aop

經典三層框架初識(二)---Spring 4 Aop

  • aop程式設計:
    • 1.匯入jar包
      • 4+1
      • spring-aop.jar  spring的aop框架
      • aspects.jar  aspectj規範
      • aopalliance-1.0.jar aop聯盟(規範通知)
      • aspectjweaver-1.8.5.jar  實現織入的
      • 如果要使用spring整合Junit來測試的話,我們還要匯入spring-test-4.1.6.RELEASE.jar
    • 2.目標類物件UserServiceImpl.java
      • src下建立service包,裡面建立UserService介面
      • package service;
        
        public interface UserService {
        	void addUser();
        	void deleteUser();
        }
        

        service包下建立impl子包,裡面建立UserService的實現類UserServiceImpl

      • package service.impl;
        
        import service.UserService;
        
        public class UserServiceImpl implements UserService {
        	/*
        	 *	這裡正常應該是呼叫dao的方法,這裡我們就用輸出語句代替了 
        	 */
        	@Override
        	public void addUser() {
        		System.out.println("新增使用者");
        	}
        
        	@Override
        	public void deleteUser() {
        		System.out.println("刪除使用者");
        	}
        
        }
        
    • 3.增強程式碼  MyAspect.java

      • src下建立aspect包,在裡面建立MyAspect這個增強程式碼類

      • package aspect;
        
        
        public class MyAspect {
        
        	public void before(){
        		System.out.println("開啟事務");
        	}
        	public void after(){
        		System.out.println("提交事務");
        	}
        }
        
    • 4.需要建立目標類物件,建立切面類物件,然後織入就可以了.這些都需要在我們的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.xsd
                http://www.springframework.org/schema/aop 
                http://www.springframework.org/schema/aop/spring-aop.xsd
                
            	<!-- 1.建立目標類物件 -->
            	<bean id="userservice" class="service.impl.UserServiceImpl"></bean>
            
           		<!-- 2.建立增強程式碼類物件 -->
            	<bean id="myaspect" class="aspect.MyAspect"></bean>
            
           	    <!-- 3.織入增強程式碼 
           	    	織入需要我們使用aop容器.所以這裡要匯入aop的名稱空間來對它進行配置
           	    	aop:config中有一個expose-proxy="false"屬性,預設是false,指代預設使用jdk代理,咱們這裡一般不寫
           	    -->
                <aop:config >
                	<!-- 	
              			 aop:pointcut 配置切入點 :往哪個地方織入增強程式碼
              			 	屬性expression:指明哪些才是切入點
              			 		execution(返回值 包名.類名.方法名(引數列表))
              			 		如果不管返回值的話可以用 *:萬用字元
              			 	下面的寫法是service包下所有的類的所有方法
                    -->
                	<aop:pointcut expression="execution(* service..*(..))" id="myPointcut"/>
                	<!-- 
                		配置切面:這裡可以理解為就是找到切入點之後,告訴它怎麼去新增
                		ref:使用哪一個增強,值就是增強程式碼類的id
                		
                	 -->
                	<aop:aspect ref="myaspect">
                		<!-- 
                			前置增強 :切入點的方法執行之前需要執行的增強程式碼
                			method:增強程式碼類裡面的方法名稱
                			pointcut-ref:前面配置的切入點
                			整體來說就是 將myaspect這個增強程式碼類的before方法新增到切入點(service包下所有的類的所有方法)裡的前面去
                		-->
                		<aop:before method="before" pointcut-ref="myPointcut"/>
                		<!-- 後置增強 -->
                		<aop:after-returning method="after" pointcut-ref="myPointcut"/>
                	</aop:aspect>
                </aop:config> 
        
                
                
        </beans>

        接下來我們去寫個測試類,測試一下

      • package test;
        
        import org.junit.runner.RunWith;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.test.context.ContextConfiguration;
        import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
        
        import service.UserService;
        
        
        @RunWith(SpringJUnit4ClassRunner.class)
        @ContextConfiguration(locations="classpath:applicationContext.xml")
        public class Test {
        	
        	@Autowired
        	UserService service;
        	
        	@org.junit.Test    
        	public void test(){
        		service.addUser();
        		service.deleteUser();
        	}
        }
        

        結果如下: