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加载

    评论

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题