Spring AOP XML配置及註解配置

一.XML配置切面類

1.日誌切面類

//切面類註解配置加兩個註解
@Aspect
@Component
public class LoggerAspect {
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("start log:" + joinPoint.getSignature().getName());
        Object object = joinPoint.proceed();
        System.out.println("end log:" + joinPoint.getSignature().getName());
        return object;
    }
}

2.業務類

//業務類
@Component
public class ProductService {
    public void doSomeService(){
        System.out.println("doSomeService");
    }
}

3.spring.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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    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
   http://www.springframework.org/schema/tx 
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context      
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    <!-- 註解方式aop-->
    <component-scan base="ServiceClass"/>
    <component-scan base="LoggAspect"/>
    <aop:aspectj-autoproxy/>
    <!--xml配置aop-->
    <bean id="productService" class="service"/>
    <bean id="loggerAspect" class="LoggerAspect">
    <aop:config>
        <aop:pointcut id="pointCut" expression="execution(* package.class.*(..))"
        <aop:aspect id="logAspect" ref="loggerAspect">
            <aop:round method="log" ref="pointcut">
        </aop:aspect>
    </aop:config>
 
</beans>

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章