springの面に向かって実現する二つの方式

5715 ワード

面に向かって:主にログ記録に応用します.業務とログ記録分離開発を実現する.
spring面向けには2つの実施形態があります.1、注釈2、xml配置です.
1、注釈は以下の通り実現される.
(1)構成は以下の通りである.
<?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" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
                            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                            http://www.springframework.org/schema/context 
                            http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
   <context:annotation-config/>
   <aop:aspectj-autoproxy/>  
   <bean id="Psv" class="com.test.spring.AOP.Personservice"/>
   <bean id="testAop" class="com.test.spring.AOP.testAop"/>
   </beans>
注意:赤い部分の配置
(2)インプリメントされたコードは以下の通りです.
@Aspect
public class testAop {

//           
@Pointcut("execution(* com.test.spring.AOP.Personservice.*(..))")
    public void pointCut(){}
    
    @After("pointCut()")
    public void after(){
        System.out.println("after");
    }
    
    @Before("pointCut()")
    public void before(){
        //JoinPoint joinPoint
        System.out.println("before");
    }
    
    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinpoint){
//joinpoint.getArgs()            
Object valu
= null try { System.out.println("aaaaaaaa"); valu = joinpoint.proceed(); // ( )
System.out.println(valu); }
catch (Throwable e) { e.printStackTrace(); } return valu; } @AfterReturning("pointCut()") public void afteReturning(){ System.out.println("afteReturning"); } @AfterThrowing("pointCut()") public void afterThrowing(){ System.out.println("afterThrowing"); } }
2、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" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
                            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
                            http://www.springframework.org/schema/aop 
                            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                            http://www.springframework.org/schema/context 
                            http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
   <context:annotation-config/>
  
   <bean id="Psv" class="com.test.spring.AOP.Personservice"/>
   <bean id="testAop" class="com.test.spring.AOP.testAop"/>
<aop:config>
        <aop:aspect id="Pservice" ref="testAop">
            <!--           -->
            <aop:pointcut id="point_cut" expression="execution(* execution(* frame.com.Action.login.*(..)))" />
            <!--    add       
            <aop:pointcut id="except_add" expression="execution(* com.test.spring.AOP.*.addPerson(..))" />
             -->
            <!--      -->
            <aop:before method="before" pointcut-ref="point_cut" />
            <!--      returning       -->
            <aop:after-returning method="after" pointcut-ref="point_cut" returning="result" />
            <!--      -->

        <aop:around method="around" pointcut-ref="point_cut"/> <aop:after-throwing method="doThrow" pointcut-ref="point_cut" throwing="e"/> </aop:aspect> </aop:config> </beans>