Spring AOP 起步
(2007年12月07日) Published by [Technorati] Tag results for java
本站相关内容:
在使用spring aop的时候碰到一个问题,在一个aop:config里面可以配多个切点表达式,在另外的aop:config里也是可以引用.配切面的时候,一个切面里面怎么不可以配多个通知,比如在记录日志的时候,有个logaspt类把它配置为切面,在这个切面里面配了一个前置的通知,是可以正常工作但是在配一个后置通知怎么就可以了,会出异常代码如下:
<!-- 日志aspectbean-->
<bean id="loggerBean" class="cn.hxex.springhibernate.base.LoggerBean"/>
<!-- 声明aop -->
<aop:config> <!-- 声明事务切点 -->
<aop:pointcut id="productServiceMethods"
expression="execution(* cn.hxex.springhibernate.service..*.*(..))"/>
<!--声明日志切点 -->
<aop:pointcut id="loggerCalls"
expression="execution(* cn.hxex.springhibernate..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />
<aop:aspect id="logAspect" ref="loggerBean"> <!--声明日志切面-->
<!--声明通知 -->
<aop:before pointcut-ref="loggerCalls" method="before"/>
<aop:after-returning pointcut-ref="loggerCalls" method="after"/>
</aop:aspect>
</aop:config>
难道一个切面不可以声明多个通知吗?
如果一个切面里不可以声明如一个前置或一个环绕的是一个理解的,怎么不可以声明一个前置和一个后置的?不解
已有 0 人发表留言,猛击->> 这里<<-参与讨论
JavaEye推荐
- 加盟电子商务创业公司,获得一场精彩的生活
- 首届亚太地区Android技术大会 5.16北京 5.17上海-JavaEye报名开始!
- 加入阿里巴巴,发展潜力无限
- 浙江:百世物流科技有限公司诚聘资深Java工程师
- 上海: 巨人网络诚聘Java, JS, 网页游戏工程师
这几天在看Spring Aop的东西,做了一个简单的小例子:
public interface IHello {
public String hello(String name) ;
}
public class HelloSpeaker implements IHello {
public String hello(String name) {
String hello = " Hello " + name ;
System.out.println( hello );
return hello ;
}
}
上面的是接口和实现类,也就是需要被AOP处理的类,也就是Target.
下面这个类,是AOP中Aspect,切入类,我采用的是,afterReturn类型
public class AfterReturnExample {
public void profile( Object retVal ) {
System.out.println(" the result " + retVal );
System.out.println(" after return ");
}
}
下面是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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
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">
<aop:config>
<aop:aspect id="beforeExample" ref="afterReturnExample">
<aop:after-returning pointcut="execution(* com.ultrapower.demo.aop.*.*(..))" method="profile" returning="retVal"/>
</aop:aspect>
</aop:config>
<bean id="hello" class="com.ultrapower.demo.aop.HelloSpeaker"></bean>
<bean id="afterReturnExample" class="com.ultrapower.demo.aop.before.AfterReturnExample"></bean>
</beans>
下面是测试类:
public class Test {
public static void main(String [] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("aop/applicationContext.xml");
IHello hello = (IHello) context.getBean("hello");
hello.hello("zzx") ;
}
}
刚刚开始的时候出现了一个错误:
- Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1808199: defining beans
[org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,hello,
afterReturnExample]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.s
pringframework.aop.aspectj.AspectJPointcutAdvisor#0': Instantiation of bean failed; nested exception is org.springframew
ork.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.aop.aspectj.AspectJPointcutA
dvisor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: warning no match for this
type name: com.ultrapower.demo.aop [Xlint:invalidAbsoluteTypeName]
这个错误主要是 pointcut配置的错误,修改之后就可以了:pointcut="execution(* com.ultrapower.demo.aop.*.*(..))"
已有 0 人发表留言,猛击->> 这里<<-参与讨论
JavaEye推荐
dzone.com: java (35 reads)
In this article i present to you a simple tutorial on how to set up Spring AOP with minimal configuration. Spring AOP is a very powerful tool and the only runtime weaving implementation currently available.互联网相关内容:
- Spring AOP 起步 (2008年01月13日)
- spring aop (2009年06月03日)
- Spring Aop (2009年09月20日)
- AOP with Spring (2010年06月18日)
- Spring AOP Advice types (2007年10月18日)
- Spring AOP - Designating Pointcuts (2008年01月10日)
- Spring AOP Docs (2008年01月13日)
- JSF and Spring AOP (2008年02月02日)
- Spring + DWR namespaces + AOP (2008年02月16日)




