抱歉打扰大家,最近在看spring in action ,看到aop 的一个小例子,怎么弄也是有问题。请大家帮忙,谢了。
类package lambert.springInaction.chapter1.aop;/**
* @author zhixinl
*
*/
public interface Knight {
public void exequest();
}package lambert.springInaction.chapter1.aop;public class TableRoundKnight implements Knight {
private String name;
public TableRoundKnight(String name) {
super();
this.name = name;
}
public TableRoundKnight() {
super();
// TODO Auto-generated constructor stub
}
public void exequest(){
System.out.println("TableRoundKnight method exequest called !");
}
}/**
*
*/
package lambert.springInaction.chapter1.aop;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
/**
* @author zhixinl
*
*/
public class Minstrel { public Minstrel() {
super();
// TODO Auto-generated constructor stub
} private static final Logger SONG = Logger.getLogger(Minstrel.class);
public void singBefore(JoinPoint joint){
SONG.info("Fa lala ,Sir " +" is so brave!"+ joint.getSignature());
}
public void singAfter(JoinPoint joint){
SONG.info("Theee ,Sir "+" is not so brave!" + joint.getSignature());
}
}
aop.xml
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="minstrel"
class="lambert.springInaction.chapter1.aop.Minstrel" />
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<bean id="knight"
class="lambert.springInaction.chapter1.aop.TableRoundKnight" />
<aop:config>
<aop:aspect id="myaspect" ref="minstrel">
<aop:pointcut id="questPointCut"
expression="execution(* *.springInaction.chapter1.aop.Knight.*(..))" />
<aop:before method="singBefore"
pointcut-ref="questPointCut" />
<aop:after-returning method="singAfter"
pointcut-ref="questPointCut" />
</aop:aspect>
</aop:config>
</beans>测试类:package lambert.springInaction.chapter1.aop;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Exec { private static final String PATH1 = "lambert/springInaction/chapter1/aop/aop.xml"; public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext(
new String[] { PATH1 });
Knight knight = (Knight)ctx.getBean("knight");
knight.exequest();
}
}
类package lambert.springInaction.chapter1.aop;/**
* @author zhixinl
*
*/
public interface Knight {
public void exequest();
}package lambert.springInaction.chapter1.aop;public class TableRoundKnight implements Knight {
private String name;
public TableRoundKnight(String name) {
super();
this.name = name;
}
public TableRoundKnight() {
super();
// TODO Auto-generated constructor stub
}
public void exequest(){
System.out.println("TableRoundKnight method exequest called !");
}
}/**
*
*/
package lambert.springInaction.chapter1.aop;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
/**
* @author zhixinl
*
*/
public class Minstrel { public Minstrel() {
super();
// TODO Auto-generated constructor stub
} private static final Logger SONG = Logger.getLogger(Minstrel.class);
public void singBefore(JoinPoint joint){
SONG.info("Fa lala ,Sir " +" is so brave!"+ joint.getSignature());
}
public void singAfter(JoinPoint joint){
SONG.info("Theee ,Sir "+" is not so brave!" + joint.getSignature());
}
}
aop.xml
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="minstrel"
class="lambert.springInaction.chapter1.aop.Minstrel" />
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<bean id="knight"
class="lambert.springInaction.chapter1.aop.TableRoundKnight" />
<aop:config>
<aop:aspect id="myaspect" ref="minstrel">
<aop:pointcut id="questPointCut"
expression="execution(* *.springInaction.chapter1.aop.Knight.*(..))" />
<aop:before method="singBefore"
pointcut-ref="questPointCut" />
<aop:after-returning method="singAfter"
pointcut-ref="questPointCut" />
</aop:aspect>
</aop:config>
</beans>测试类:package lambert.springInaction.chapter1.aop;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Exec { private static final String PATH1 = "lambert/springInaction/chapter1/aop/aop.xml"; public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext(
new String[] { PATH1 });
Knight knight = (Knight)ctx.getBean("knight");
knight.exequest();
}
}
解决方案 »
- MyEclipse 10 连接oracle
- org.springframework.transaction.CannotCreateTransactionException:
- java上传图片到数据库的代码
- 如何在MyEclipse7.0中搭建SOA开发平台
- 利用过滤器控制session关闭问题,请高手指点,谢谢
- 那位好心的人帮帮忙啊 全部家当 数据库问题
- 请大家讨论一下框架设计的原则、理念。
- webservice 自定义404
- 怎样把编写好的EJB打包成WebLogic下可以部署的.jar文件?(WebLogic7.0)
- ###轻松拿分了!!!--新手入门小问题--用JB7做CMP实体Bean时,自定义的finder方法中的SQL语句怎么写?
- java有什么好的方法计算word页数 poi好像不准 急!!!
- json-lib包来用json转换hibernate持久化对象集合问题
TableRoundKnight method exequest called !
Theee ,Sir is not so brave!org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint$MethodSignatureImpl@14e3f41正常,我没有任何修改,直接打印出来了。
[email protected]