用@aspect注解的方式实现切面,但是无发效果,代码如下,求大神解答
业务类
package com.aop_spring;
public class Business implements IBusiness, IBusiness2 {
@Override
public boolean doSomeThing() {
System.out.println("执行业务逻辑");
return true;
}
@Override
public void doSomeThing2() {
System.out.println("执行业务逻辑2");
}
}
切面代码:
package com.aop_spring;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class SpringAspect {
@Before("execution(* * com.aop_spring.Business.*(..))")
public void before(){
System.out.println("before");
}
}
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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="com.*" annotation-config="true"/>
<aop:aspectj-autoproxy/>
<bean id="business" class="com.aop_spring.Business">
</bean>
</beans>
测试代码:
package com.aop_spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args){
ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");
((Business) appCtx.getBean("business")).doSomeThing();
}
}
包路径:
运行结果:
执行业务逻辑