现在学到了Spring 的AOP,都说AOP是最难理解的思想课,,哪位高手,能根据自己的理解,说说AOP 。为什么要用??它的思想,等等。。
解决方案 »
- Hibernate报异常“nested transactions not supported“,这怎么解决啊?
- maven subclipse tortoiseSVN m2Eclipse subversion到底是什么东西
- 字符串
- 散分:如何推广开源项目?
- JSF的MyFaces调试错误java.lang.IllegalStateException
- C# 调用JAVA webservice 我是初学者
- [200分]Struts中如何使用动态form,能否详细说明,并给个能运行的例子!!![急等]
- 关于session的问题
- 初学struts碰到的问题,希望大家能帮帮忙看一看,谢谢
- 怎样从JAVA门外汉到精通J2EE的整个体系?(50分)
- 为什么我找不到new下面的hibernate xml mapping file那项,我确定是在hibernate下面看的。不是位置不对。是没有
- ssh2,启动报错,求助了
不知道这一想法对不对头。我还以为,AOP更重要的是把某一类相似的操作交由Aspect去处理,使代码更优雅。
aop的相关名词()
advice:表示一个增强的功能.
pointCut:表示一次或多次方法调用.
advisor=advice+pointcut
interceptor=advice或advisor
1、创建一个Advice对象,覆写方法完成增加的功能。
package biz;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class JiaJiFenAdvice implements MethodInterceptor {
@Override//覆写方法完成增加的功能
public Object invoke(MethodInvocation invocation) throws Throwable {
Object res=null;
try {
//这里添加:在原有功能前加功能
res=invocation.proceed();
//这里添加:在原有功能后加功能
} catch (Exception e) {
}
return res;
}
}
2、在“applicationContext.xml”文件里配置:
<!-- 客户 -->
<bean name="/doLogin" class="web.EpetDispatchAction">
<property name="diaryBiz" ref="petDiaryBiz"></property>
<property name="loginBiz" ref="petLoginBiz"></property>
</bean>
<!-- 目标 -->
<bean id="petDiaryBiz" class="biz.DiaryBiz"></bean>
<bean id="petLoginBiz" class="biz.LoginBiz"></bean>
<!-- 增强 -->
<bean id="petJiaJiFenAdvice" class="biz.JiaJiFenAdvice"></bean>
<!-- 代理 -->
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>petDiaryBiz</value>
<value>petLoginBiz</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>petJiaJiFenAdvice</value>
</list>
</property>
</bean>如果看不明白,可以去我的资源里下载例子
不错!!希望还能把AOP 的思想,还有其他重要的作用讲的更详细些。
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="myadvice" transaction-manager="trasactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="edit*" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="del*" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="drop*" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="register*" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<!--
切点: 对哪些包下面的接口进行事务管理
第一个星号代表方法返回值类型任意。
第二个星号代表包下面的接口或类名任意。
第三个星号代表包下面的接口或类的方法名任意。
小括号里的两个点代表方法参数任意 。
-->
<aop:pointcut expression="execution(* com.skeyedu.hibernate.service.*.*(..))"
id="mycutpoint" />
<!--
以何种方式处理知道了,对象进行处理知道了,下面就是整合了
-->
<aop:advisor advice-ref="myadvice" pointcut-ref="mycutpoint" />
</aop:config>
</beans>
这个是AOP的一个重要方面。。面向切面编程