您的位置:首页 > 编程语言 > Java开发

Spring Aop的应用

2011-12-24 12:13 225 查看
大家好,这次我来讲一讲Spring Aop的几个常见应用。

首先,我们共同来理解一下Spring Aop,为什么要有它呢,它可以替代OOP吗,它在开发中能给我们带来什么呢,它又有哪些应用呢,我们带着这些疑问一起探讨Spring Aop的知识。

第一,在使用前需要加入相应的Spring包以及Spring依赖包:

主包:org.springframework.aop-3.0.5.RELEASE.jar与org.springframework.aspects-3.0.5.RELEASE.jar

依赖包:com.springsource.org.aopalliance-1.0.0.jar与com.springsource.net.sf.cglib-2.2.0.jar

这里需要说一下 com.springsource.net.sf.cglib-2.2.0.jar,使用cglib中的类库可以实现 拦截具体的类,不光是接口例如,可以是Controller类,大家都知道jdk 的反射机制把,cglib的功能与它类似,比它更强大。

第二,异常处理实现:

Java代码

public class MyException1 implements<SPAN style="COLOR: #ff6600"> HandlerExceptionResolver</SPAN>

{
@Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object arg2, Exception ex) {
System.out.println("进入异常处理......");
if (ex instanceof ArrayIndexOutOfBoundsException) {
System.out.println(ex);
request.setAttribute("error", "发生了数组越界异常....");
return new ModelAndView("/SpringAop/exception/error.jsp");
} else if (ex instanceof RuntimeException) {
System.out.println(ex);
request.setAttribute("error", "发生了运行时异常....");
return new ModelAndView("/SpringAop/exception/error.jsp");
}
return null;
}

}

public class MyException1 implements HandlerExceptionResolver
{
@Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object arg2, Exception ex) {
System.out.println("进入异常处理......");
if (ex instanceof ArrayIndexOutOfBoundsException) {
System.out.println(ex);
request.setAttribute("error", "发生了数组越界异常....");
return new ModelAndView("/SpringAop/exception/error.jsp");
} else if (ex instanceof RuntimeException) {
System.out.println(ex);
request.setAttribute("error", "发生了运行时异常....");
return new ModelAndView("/SpringAop/exception/error.jsp");
}
return null;
}

}

第三,基本拦截器实现:

Java代码

public class MyInterceptor implements HandlerInterceptor {
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("执行后....");
}
public void postHandle(HttpServletRequest arg0,
HttpServletResponse arg1,Object arg2, ModelAndView arg3)
throws Exception {
System.out.println("执行了......");
}

public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,Object arg2) throws Exception {
System.out.println("执行前....");
return false;
}

public class MyInterceptor implements HandlerInterceptor {

public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("执行后....");
}
public void postHandle(HttpServletRequest arg0,
HttpServletResponse arg1,Object arg2, ModelAndView arg3)
throws Exception {
System.out.println("执行了......");
}

public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,Object arg2) throws Exception {
System.out.println("执行前....");
return false;
}

第四:以日志为例,实现使用注释实现AOP

第一步: 在Spring 配置文件中加入注释

Java代码
<aop:aspectj-autoproxy />

<aop:aspectj-autoproxy />

第二步: 写一个日志代理

Java代码
@Before("pointCut()")

public void before(JoinPoint jp) {
// 获取目标类名
String className = jp.getTarget().getClass().toString();
className = className.substring(className.indexOf("com"));
// 获取目标方法签名
String signature = jp.getSignature().toString();
// 获取方法名
String methodName = signature.substring(signature.lastIndexOf(".") + 1,signature.indexOf("("));
// 获取方法参数名称
Object[] args = jp.getArgs();
if (args != null) {
for (int i = 0; i < args.length; i++) {
System.out.println("args[" + i + "] =" + args[i]);
}
}
System.out.println("获取目标类名" + className);
System.out.println("获取目标方法签名" + signature);
System.out.println("获取方法名称" + methodName);

}

@Before("pointCut()")
public void before(JoinPoint jp) {
// 获取目标类名
String className = jp.getTarget().getClass().toString();
className = className.substring(className.indexOf("com"));
// 获取目标方法签名
String signature = jp.getSignature().toString();
// 获取方法名
String methodName = signature.substring(signature.lastIndexOf(".") + 1,signature.indexOf("("));
// 获取方法参数名称
Object[] args = jp.getArgs();
if (args != null) {
for (int i = 0; i < args.length; i++) {
System.out.println("args[" + i + "] =" + args[i]);
}
}
System.out.println("获取目标类名" + className);
System.out.println("获取目标方法签名" + signature);
System.out.println("获取方法名称" + methodName);

}


第五,事务管理

第一步:修改Spring配置文件,加入以下代码

Java代码
<bean id="transactionManager"

class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

主要是与数据库sessionFactory关联

第二步:加入自动事务处理注解

Java代码
<tx:annotation-driven transaction-manager="transactionManager" />

<tx:annotation-driven transaction-manager="transactionManager" />

下面就可以在Service层在需要进行Spring事务管理的类或方法上加入@Transaction注解,这样再进行数据库处理时,就会自动与数据库保持一致。

ok,今天就写这些,另外,Spring Aop是还有好多应用,希望大家共同进步,有好的用法,共同进步。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: