谢同学谢童鞋
2016-03-06 20:26
配置文件如下:
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<bean id="moocAspect" class="com.imooc.aop.schema.advice.MoocAspect"></bean>
<bean id="aspectBiz" class="com.imooc.aop.schema.advice.AspectBiz"></bean>
<aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
<aop:pointcut expression="execution(* com.imooc.aop.schema.advice.biz.*Biz.*(..))" id="moocPoint"/>
<aop:before method="before" pointcut-ref="moocPoint"/>
</aop:aspect>
</aop:config>
</beans>
输出结果:
三月 06, 2016 8:42:08 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5bc22f22: startup date [Sun Mar 06 20:42:08 CST 2016]; root of context hierarchy
三月 06, 2016 8:42:08 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from URL [file:/D:/Program%20Files%20(x86)/eclipse-jee-luna-SR2-win32-x86_64/eclipse/D/workspace/spring/bin/spring-aop-schema-aspect.xml]
AspectBiz
三月 06, 2016 8:42:08 下午 org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@5bc22f22: startup date [Sun Mar 06 20:42:08 CST 2016]; root of context hierarchy
试一下在execution()里面的*后空一格,在写包那些方法
package com.imooc.aop.schema.advice;
public class AspectBiz {
public void biz(){
System.out.println("AspectBiz");
//throw new RuntimeException();
}
}
package com.imooc.aop.schema.advice;
import org.aspectj.lang.ProceedingJoinPoint;
public class MoocAspect {
public void before(){
System.out.println("MoocAspect before");
}
public void afterReturning(){
System.out.println("MoocAspect after");
}
public void afterThrowing(){
System.out.println("MoocAspect throwing");
}
public void after(){
System.out.println("MoocAspect after");
}
public Object around(ProceedingJoinPoint pjp){
Object obj = null;
try {
System.out.println("MoocAspect around 1");
obj = pjp.proceed();
System.out.println("MoocAspect around 2");
} catch (Throwable e) {
e.printStackTrace();
}
return obj;
}
}
package com.imooc.aop.schema.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import com.imooc.aop.schema.advice.AspectBiz;
import com.imooc.test.UnitTestBase;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestAOPSchemaAdvice extends UnitTestBase{
public TestAOPSchemaAdvice(){
super("classpath*:spring-aop-schema-aspect.xml");
}
@Test
public void testBiz(){
AspectBiz biz = super.getBean("aspectBiz");
biz.biz();
}
}
要不要把java文件也贴上来,这样更容易确定问题
Spring入门篇
268786 学习 · 963 问题
相似问题