第19讲 --使用Spring配置文件实现AOP

 

上面我们是使用注解的方式实现spring AOP的,下面我们使用Spring的配置文件实现AOP

 

 

步骤:

 

1.首先写切面类,不在需要任何 注解 了

package cn.com.xinli.service;

import org.aspectj.lang.ProceedingJoinPoint;
/**
 * 切面
 *
 */
public class MyInterceptor {	
	public void doAccessCheck() {
		System.out.println("前置通知");
	}

	public void doAfterReturning() {
		System.out.println("后置通知");
	}
	
	public void doAfter() {
		System.out.println("最终通知");
	}
	
	public void doAfterThrowing() {
		System.out.println("例外通知");
	}
	
	public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("进入方法");
		Object result = pjp.proceed();
		System.out.println("退出方法");
		return result;
	}
	
}

 

(2) 在beans.xml 中配置 切面,定义业务bean

 

<?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:aop="http://www.springframework.org/schema/aop"      
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  
			
           <aop:aspectj-autoproxy proxy-target-class="true"/>
           <!--切面和业务bean都需要交给容器管理--> 
            <bean id="myInterceptor" class="cn.com.xinli.service.MyInterceptor"></bean>
        	<bean id="personService" class="cn.com.xinli.service.impl.PersionServiceBean"></bean>
        	 <aop:config>
        	 <!-- 定义一个切面 -->
        	 <aop:aspect id="asp" ref="myInterceptor">
        	 <!-- 定义切入点,定义拦截表达式,前置通知,后置通知,等.. -->
        		<aop:pointcut id="mycut" expression="execution(* cn.com.xinli.service..*.*(..))"/>
        		<aop:before pointcut-ref="mycut" method="doAccessCheck"/>
        		<aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
			  	<aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>
			  	<aop:after pointcut-ref="mycut" method="doAfter"/>
			  	<aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
        	</aop:aspect>
        </aop:config>
      
       
                                                        
		
		           
       

        
     <!-- 扫描 cn.com.xinli包下以及子包种有 @Service @Controller @Repository @Component
          注解的类,一旦发现,则将其纳入到spring容器中管理
     -->
	
	<!--       
	<bean id="persionDao" class="cn.com.xinli.dao.impl.PersionDaoBean"></bean>                                                               
	<bean id="persionServiceBean" class="cn.com.xinli.service.impl.PersionServiceBean" init-method="init" destroy-method="destory">
		 -->  
		<!--  
		<constructor-arg index="0" type="cn.com.xinli.dao.PersionDao" ref="persionDao"></constructor-arg>
		<constructor-arg index="1" value="huxl"></constructor-arg>
		-->
	
	
	
	<!-- 
    <bean id="persionServiceBean2" class="cn.com.xinli.service.impl.PersionServiceBeanFactory" factory-method="createPersionServiceBean"/>
    
    <bean id="PersionServiceBeanFactory" class="cn.com.xinli.service.impl.PersionServiceBeanFactory"></bean>
    <bean id="persionServiceBean3" factory-bean="PersionServiceBeanFactory" factory-method="createPersionServiceBean2"></bean>
     -->
</beans>

 

 

测试::

 

ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		PersionSevice ps=(PersionSevice)ctx.getBean("personService");
		
		//ps.save("huxl");
		ps.getPersonName(1);

 

结果: 

前置通知:
进入方法
我是getPersonName()方法
后置通知
最终通知
退出方法

 

 

 

你可能感兴趣的:(DAO,spring,AOP,bean,配置管理)