2 weixin 40568405 weixin_40568405 于 2018.01.12 14:11 提问

SpringAoP无法切入问题,请大神指导 10C
在做AoP练习时,AoP总是无法切入,请各位大大赐教  

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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.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">
    <!-- 自动扫描 -->
    <context:component-scan base-package="com.gwc.shop" />
    <!--自动代理-->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <!-- 引入配置文件 -->
    <context:property-placeholder location="classpath:properties/jdbc.properties"/>  
    ```  
### 代理类  

@Component
@Aspect
public class AoPUtils {
@Pointcut("execution(* com.gwc.shop.entity.Goods.*(..))")
public void aspect(){ }

@Before("aspect()")
public void before(JoinPoint joinPoint){
    System.out.println("before");
    System.out.println(joinPoint.getArgs().toString());
    System.out.println(joinPoint.getKind());
    System.out.println(joinPoint.getSignature());
    System.out.println(joinPoint.getTarget());
}

@After("aspect()")
public void after(JoinPoint joinPoint){
    System.out.println("after");
}

@Around("aspect()")
public void around(JoinPoint joinPoint){
    try {
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            System.out.println(arg.toString());
        }
        Object proceed = ((ProceedingJoinPoint) joinPoint).proceed();
        if (proceed!=null)
            System.out.println(proceed.toString());
    } catch (Throwable throwable) {
        throwable.printStackTrace();
    }
}
### 被代理类如下  

@Component(value = "goods")
public class Goods {
private Long id;
private String name;
private Long count;
//assortment的id
private Long belongs;


### 测试类如下:  

@Test
public void test(){
ApplicationContext context = new AnnotationConfigApplicationContext("com.gwc.shop.entity");
Goods goods = ((Goods) context.getBean("goods"));
goods.setBelongs(123L);
goods.getBelongs();
String name = "zhangsan";
name = name.concat("123");
System.out.println(name);
}

我的测试类没有添加Spring的注解,难道和这个有关... 执行不报错,只是没有切入!

4个回答

tgioer
tgioer   2018.01.12 14:36

xml 配置文件没有被加载,
xml文件 使用ClassPathXmlApplicationContext加载
@Configuration 使用 AnnotationConfigApplicationContext加载

weixin_40568405
weixin_40568405 回复ljx960409: 我是在test 中调用的
5 天之前 回复
ljx960409
ljx960409 Service中如此调用并非调用的是代理类中的方法,是不会被切进去的。换言之,必须要调用代理类才会被切进去。
5 天之前 回复
weixin_40568405
weixin_40568405 如何加载,可以详细说一下吗 感谢
5 天之前 回复
z3jjlzt
z3jjlzt   2018.01.12 16:40

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath* :*.xml")

weixin_40568405
weixin_40568405 不是这个问题 之前是过了
5 天之前 回复
u014794644
u014794644   2018.01.12 18:29

图片说明
图片说明

qq_36431127
qq_36431127   2018.01.15 17:15

@Pointcut("execution(* com.gwc.shop.entity.Goods.*(..))") 切点看下是在哪里

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!