Spring Aop 配置

3 篇文章 0 订阅

1.切面类:在方法前后加逻辑(日志,执行时间...)。

package com.tech.allen.util;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

  
public class AspectClass {

	public void doBefore(JoinPoint jp) { 
		
		System.out.println("method:doBefore ");  
		               
	}  
	
	public void doAfter(JoinPoint jp) { 
		
		System.out.println("method:doAfter ");  
		               
	}  
	 public Object doAround(ProceedingJoinPoint pjp) throws Throwable {   
		 long time = System.currentTimeMillis();   
		 Object retVal = pjp.proceed();   //类似设计模式的责任链模式
		 time = System.currentTimeMillis() - time;   
		 System.out.println("process time: " + time + " ms");   
		 return retVal;   
	 }   
	
}


2.userDao   :在getUserList()方法前后加逻辑。

package com.tech.allen.dao;

import java.util.List;

public interface UserDao {

	public List getUserList();
	
}


3.bean.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-3.0.xsd
           http://www.springframework.org/schema/tx
	       http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
	       http://www.springframework.org/schema/aop
	       http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
           
<!--定义数据源DBCP--> 
	 
	<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>	
<!--定义一个hibernate的SessionFactory-->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="annotatedClasses">
			<list>
				<value>com.tech.allen.model.User</value>
				 
			</list>
		</property>
		
		
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.Oracle10gDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
	</bean>
	
	<!-- 注入HibernateTemplate -->
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<!--
	<bean id="txManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	-->
	
	
	
	
	
	<!--   业务逻辑组件 -->  
	
	<!-- 用户管理 -->
	<bean name="UserDao" class="com.tech.allen.dao.impl.UserDaoImpl">
		<property name="hibernateTemplate" ref="hibernateTemplate">
		</property>
	</bean>
	<bean name="UserService" class="com.tech.allen.service.impl.UserServiceImpl">
		<property name="UserDao" ref="UserDao"/>
		 
	</bean>
	
	<bean name="UserAction" class="com.tech.allen.action.UserAction">
		<property name="UserService" ref="UserService"/>
		 
	</bean>
	
    <bean id="aspectClass" class="com.tech.allen.util.AspectClass"/>
    
	<aop:config>
    <aop:aspect id="myAop" ref="aspectClass">
      <aop:pointcut id="target" expression="execution(* com.tech.allen.service.*.*(..))"/>
      <aop:before method="doBefore" pointcut-ref="target"/>
      <aop:after method="doAfter" pointcut-ref="target"/>
      <aop:around method="doAround" pointcut-ref="target"/> 
    </aop:aspect>
  </aop:config>
 


4.测试程序:

package com.tech.allen.action;

 
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.tech.allen.model.User;
import com.tech.allen.service.UserService;
import com.tech.allen.vo.UserForm;

 

public class UserAction extends  ActionSupport implements ModelDriven<UserForm>{

	private UserForm  userForm = new UserForm();
	private UserService userService;
	private User user = new User();
	
	public String  getUserList(){
	
		userService.getUserList();
		
		return "success";
		
		
	}
	
	public UserForm getModel() {
		// TODO Auto-generated method stub
		return userForm;
	}

	public UserForm getUserForm() {
		return userForm;
	}

	public void setUserForm(UserForm userForm) {
		this.userForm = userForm;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}


5.执行结果:

method:doBefore
Hibernate: select user0_.id as id0_, user0_.address as address0_, user0_.birthday as birthday0_, user0_.createId as createId0_, user0_.createName as createName0_
method:doAfter
process time: 524 ms

 

1.切面(Aspect) :官方的抽象定义为“一个关注点的模块化,这个关注点可能会横切多个对象”,在本例中,“切面”就是类AspectClass所关注的具体行为。

2.连接点(Joinpoint) :程序执行过程中的某一行为。

3.通知(Advice) :“切面”对于某个“连接点”所产生的动作。

4.切入点(Pointcut) :匹配连接点的断言,在AOP中通知和一个切入点表达式关联。

5.目标对象(Target Object) :被一个或者多个切面所通知的对象。

6.AOP代理(AOP Proxy) 在Spring AOP中有两种代理方式,JDK动态代理和CGLIB代理。默认情况下,TargetObject实现了接口时,则采用JDK动态代理,例如,AServiceImpl;反之,采用CGLIB代理。

通知(Advice)类型

1.前置通知(Before advice) :在某连接点(JoinPoint)之前执行的通知,但这个通知不能阻止连接点前的执行。ApplicationContext中在<aop:aspect>里面使用<aop:before>元素进行声明。

2.后通知(After advice) :当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。ApplicationContext中在<aop:aspect>里面使用<aop:after>元素进行声明。

3.返回后通知(After return advice) :在某连接点正常完成后执行的通知,不包括抛出异常的情况。ApplicationContext中在<aop:aspect>里面使用<after-returning>元素进行声明。

4.环绕通知(Around advice) :包围一个连接点的通知,类似Web中Servlet规范中的Filter的doFilter方法。可以在方法的调用前后完成自定义的行为,也可以选择不执行。ApplicationContext中在<aop:aspect>里面使用<aop:around>元素进行声明。

5.抛出异常后通知(After throwing advice) : 在方法抛出异常退出时执行的通知。 ApplicationContext中在<aop:aspect>里面使用<aop:after-throwing>元素进行声明。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值