<?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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="sr" class="com.springmodel.Shiren" />
<bean id="qs" class="com.springmodel.Qishi">
</bean>
<aop:config>
<aop:aspect ref="sr" >
<aop:pointcut id="qeMethod" expression="execution(* com.springmodel.Qishi.tanXian(*))" />
<aop:before method="singBefore" pointcut-ref="qeMethod"/>
<aop:after method="singAlter" pointcut-ref="qeMethod"/>
</aop:aspect>
</aop:config>
</beans>
package com.springmodel;public class Shiren {
public void singBefore(){
System.out.println("探险之前sing");
}
public void singAlter(){
System.out.println("探险之后sing");
}
}
程序能够运行成功过,但是这2条语句 <aop:before method="singBefore" pointcut-ref="qeMethod"/>
<aop:after method="singAlter" pointcut-ref="qeMethod"/>运行没有效果
<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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="sr" class="com.springmodel.Shiren" />
<bean id="qs" class="com.springmodel.Qishi">
</bean>
<aop:config>
<aop:aspect ref="sr" >
<aop:pointcut id="qeMethod" expression="execution(* com.springmodel.Qishi.tanXian(*))" />
<aop:before method="singBefore" pointcut-ref="qeMethod"/>
<aop:after method="singAlter" pointcut-ref="qeMethod"/>
</aop:aspect>
</aop:config>
</beans>
package com.springmodel;public class Shiren {
public void singBefore(){
System.out.println("探险之前sing");
}
public void singAlter(){
System.out.println("探险之后sing");
}
}
程序能够运行成功过,但是这2条语句 <aop:before method="singBefore" pointcut-ref="qeMethod"/>
<aop:after method="singAlter" pointcut-ref="qeMethod"/>运行没有效果
有点不是很懂,但是我Qishi里面是实现了1个接口
tanXian()里面只能有*,用..会抛异常
代码没报错,反而运行成功,只不过这2条语句没长生任何效果
<aop:before method="singBefore" pointcut-ref="qeMethod"/>
<aop:after method="singAlter" pointcut-ref="qeMethod"/>
如果直接造型成class会报错:
Exception in thread "main" java.lang.ClassCastException:
public static void main(String[] args) throws Exception {
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
// 这样写会报错
// LogTestServiceImpl s = (LogTestServiceImpl)act.getBean("LogTestService");
LogTestService s = (LogTestService) act.getBean("LogTestService");
s.say();
}
没作用,现在关键的是这2条语句配置了和没配置一样
而且我把它改成TanXian tx=(TanXian)apc.getBean("qs");
也没左右 TanXian(为接口) Qishi实现了它的方法
<aop:before method="singBefore" pointcut-ref="qeMethod"/>
<aop:after method="singAlter" pointcut-ref="qeMethod"/>
<aop:pointcut id="qeMethod" expression="execution(* com.springmodel.Qishi.tanXian(*))" />
这句造成的,用*号就会这样,测试过了。换成..或者什么都不要试试吧
<aop:pointcut id="qeMethod" expression="execution(* com.springmodel.Qishi.tanXian(*))" />
这个导致的,改成
<aop:pointcut id="qeMethod" expression="execution(* com.springmodel.Qishi.tanXian(..))" />或者
<aop:pointcut id="qeMethod" expression="execution(* com.springmodel.Qishi.tanXian())" />试试吧