用@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();
}
}
包路徑:
運行結果:
執行業務邏輯
@Aspect上面注解一個@Component試試