总结四

来源:互联网 发布:fetch.js 编辑:程序博客网 时间:2024/04/29 17:59

AOP(Aspect Oriented Programming):面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。


示例:


package com.cml.bean;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.apache.ibatis.session.SqlSession;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import com.cml.util.Tools;

@Aspect
@Component()
public class UserAspect {

 private SqlSession session=Tools.getSession();
 
 @Before(value = "execution(* com.cml.service.impl.*ServiceImpl.*UserBean(..))")
 public void beforeAdvice(JoinPoint jp) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{

  Object target = jp.getTarget();
  Method method = target.getClass().getMethod("setUserAndSession", SqlSession.class);
  method.invoke(target, session);
  
 }
 
 
 @Around(value = "execution(* com.cml.service.impl.*ServiceImpl.*UserBean(..))")
 public Object aroundAdvice(ProceedingJoinPoint jp)throws Throwable{

  Object re=jp.proceed();

  session.commit();
  return re;
 
 }
 @AfterThrowing(throwing="ex",value="execution(* com.cml.service.impl.*ServiceImpl.*UserBean(..))")
 public void exceptionAdvice(Throwable ex){
  session.rollback();
 }
 
 @After(value="execution(* com.cml.service.impl.*ServiceImpl.*UserBean(..))")
 public void mustAdvice(){
  
  session.close();
  
 }

}





package com.cml.service.impl;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import com.cml.bean.UserBean;
import com.cml.dao.IUserDao;
import com.cml.service.IUserService;
@Service("userService")
public class UserServiceImpl implements IUserService{

 
 private IUserDao userDao;

 public void setUserAndSession(SqlSession session){
  userDao=session.getMapper(IUserDao.class);
 }

 public void addUserBean(UserBean user) throws Exception{
  
  userDao.addUserBean(user);
 }

 public void deleteUserBean(int id) {
  userDao.deleteUserBean(id);
  
 }

 public UserBean findByIdUserBean(int id) {
  
  return userDao.findByIdUserBean(id);
 }

 public void updateUserBean(UserBean user,int id) {
  userDao.updateUserBean(user,id);
  
 }

}




<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
 <aop:aspectj-autoproxy/>
 

 <context:component-scan base-package="com.cml">

  <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"></context:include-filter>
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
 </context:component-scan>
 

</beans>








0 0