weixin_40568405 2018-01-12 06:11 采纳率: 0%
浏览 1507
已结题

SpringAoP无法切入问题,请大神指导

在做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的注解,难道和这个有关... 执行不报错,只是没有切入!
  • 写回答

5条回答 默认 最新

  • 不够具体 2018-01-12 06:36
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料