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