关于注解自动配置以及xml的手动配置AOP动态代理

版权声明:有一种生活不去经历不知其中艰辛,有一种艰辛不去体会,不会知道其中快乐,有一种快乐,没有拥有不知其中纯粹 https://blog.csdn.net/wwwzydcom/article/details/82959377

定义了一个切面

package com.sxt.proxy;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 定义AOP切面类
 * @Aspect标注当前类为切面类
 */
@Component
@Aspect
public class LogCut {

    /**
     * 定义切入点
     * 匹配service包以及子包下所有类的所有方法
     */
    @Pointcut("execution(* com.sxt.service..*.*(..))")
    public void cut(){ }

    /**
     * 前置通知
     */
    @Before(value = "cut()")
    public void before(){
        System.out.println("前置通知执行的方法");
    }
    @AfterReturning(value = "cut()")
    public void afterReturn(){
        System.out.println("方法正常结束后的返回通知");
    }
    @After(value = "cut()")
    public void after(){
        System.out.println("最终通知:无论方法是否抛出异常,都会执行的通知!");
    }
    @AfterThrowing(value = "cut()",throwing = "e")
    public void afterThrow(Exception e){
        System.out.println("异常通知:异常方法抛出异常会执行该通知"+e);
    }


    /**
     * 环绕通知:重点
     *
     */
    /*
    @Around( value = "cut()")
    public Object around(ProceedingJoinPoint pjp)throws Throwable{
        //执行目标对象的方法
        Object result = null;
        System.out.println("环绕通知开始...");
        System.out.println(pjp.getSignature());
        System.out.println(pjp.getTarget());

        Object[] params = pjp.getArgs();

        for (Object obj:params){
            System.out.println(obj);
        }
        System.out.println("----------");
        System.out.println(pjp.getKind());
        System.out.println(pjp.getSourceLocation());
        //调用目标方法
        result = pjp.proceed();
        System.out.println("通知结束!");
        return result;
    }*/
}

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: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/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <context:component-scan base-package="com.sxt"/>
    <!--
       开启aop 代理环境
    -->
    <aop:aspectj-autoproxy/>
</beans>

BUG:

警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'userService' is expected to be of type 'com.sxt.service.IUserService' but was actually of type 'com.sxt.service.UserService$$EnhancerBySpringCGLIB$$3159cbe7'

显示是类型注入失败,一脸懵逼,这种的bug不知道怎么调试

手动配置
定义切面

package com.sxt.proxy;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 定义AOP切面类
 * @Aspect标注当前类为切面类
 */
@Component
@Aspect
public class LogCut02 {

    public  void before(){
        System.out.println("前置通知:目标方法执行前执行该通知...");
    }

    public  void afterReturn(){
        System.out.println("返回通知:方法正常结束后执行该通知...");
    }


    public  Object around(ProceedingJoinPoint pjp) throws Throwable {
        // 执行目标对象的方法
        Object result=null;
        System.out.println("环绕通知开始...");
        System.out.println(pjp.getSignature());
        //Signature signature=pjp.getSignature();
        System.out.println(pjp.getTarget());
        Object[] params=pjp.getArgs();
        for(Object obj:params){
            System.out.println(obj);
        }
        System.out.println("---------");
        System.out.println(pjp.getKind());
        System.out.println(pjp.getSourceLocation());
        result= pjp.proceed();
        System.out.println("通知结束...");


        return result;
    }

}

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: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/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <context:component-scan base-package="com.sxt"/>
    <!--
       开启aop 代理环境
    -->
    <aop:aspectj-autoproxy/>
    <aop:config>
        <!--配置切面-->
        <aop:aspect ref="logCut02">
            <!--配置切入点-->
            <aop:pointcut id="cut" expression="execution(* com.sxt.service..*.*(..))">
            </aop:pointcut>
            <aop:around method="around" pointcut-ref="cut">
            </aop:around>
        </aop:aspect>
    </aop:config>
</beans>
 @Test
    public void shouldAnswerWithTrue02(){
        BeanFactory factory = new ClassPathXmlApplicationContext("Info02.xml");
        UserController userController = factory.getBean(UserController.class);
        ResultInfo resultInfo = userController.userLogin("admin","123456");
        System.out.println(resultInfo);

    }

BUG

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 22 in XML document from class path resource [Info02.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 22; columnNumber: 28; cvc-complex-type.2.1: 元素 'aop:pointcut' 必须不含字符或元素信息项 [子级], 因为该类型的内容类型为空。

猜你喜欢

转载自blog.csdn.net/wwwzydcom/article/details/82959377
今日推荐