`
Allen_Oscar
  • 浏览: 24249 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring Aop 配置

阅读更多

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>元素进行声明。

 

分享到:
评论

相关推荐

    springAop的配置实现

    在Spring XML配置文件中,我们可以定义以下元素来实现AOP配置: - `&lt;aop:config&gt;`:声明AOP配置。 - `&lt;aop:pointcut&gt;`:定义切入点表达式,例如`execution(* com.example.service.*.*(..))`表示匹配...

    Spring AOP配置事务方法

    Spring AOP 配置事务方法 Spring AOP(Aspect-Oriented Programming,面向方面编程)是一种编程范式,它允许开发者在不修改源代码的情况下,增强和修改应用程序的行为。 Spring AOP 提供了一种灵活的方式来实现事务...

    使用Spring配置文件实现AOP

    以下是一个简单的Spring AOP配置文件示例: ```xml &lt;aop:config&gt; &lt;aop:aspect id="loggingAspect" ref="loggingAdvice"&gt; &lt;aop:before method="beforeMethod" pointcut-ref="businessMethods"/&gt; &lt;aop:after-...

    Spring AOP配置实例

    **Spring AOP配置实例** Spring AOP(Aspect Oriented Programming,面向切面编程)是Spring框架的核心组件之一,它提供了一种在不修改源代码的情况下,对程序进行功能增强的技术。AOP允许开发者定义“切面”,这些...

    Spring之AOP配置文件详解

    ### Spring之AOP配置文件详解 #### 一、前言 在Java开发中,Spring框架因其强大的功能和灵活的配置而被广泛应用于企业级应用的开发。其中,面向切面编程(Aspect Oriented Programming,简称AOP)是Spring框架的...

    利用SPring AOP配置切面的一个例子

    本例中,我们关注的是如何利用Spring AOP配置一个简单的切面,该切面在`DukePerformer`类的`perform`方法执行前后,插入观众的行为,如找座位、关手机、鼓掌以及不满意的反应。通过这种方式,我们将这些行为抽象为...

    spring AOP配置的几种方式

    ### Spring AOP配置详解 #### 一、Spring AOP配置概览 面向切面编程(Aspect-Oriented Programming,简称AOP)是Spring框架的重要特性之一,它通过将业务逻辑中的横切关注点(Cross-cutting Concerns)与核心业务...

    springAOP配置动态代理实现

    2. **注解配置**:Spring 2.5引入了基于注解的AOP配置,可以在切面类上使用@Aspect注解,@Before、@After、@AfterReturning、@AfterThrowing和@Around定义通知,@Pointcut定义切点。例如: ```java @Aspect ...

    Spring AOP配置与管理

    **Spring AOP配置与管理** 在Java开发中,Spring框架以其强大的功能和灵活性深受开发者喜爱。其中,AOP(Aspect-Oriented Programming,面向切面编程)是Spring框架的一个重要特性,它允许开发者定义“切面”,即...

    Spring AOP 配置小节

    **Spring AOP 配置小节** 在Java开发中,Spring框架因其强大的功能和灵活性而备受推崇,其中AOP(面向切面编程)是其重要特性之一。AOP允许我们把关注点分离到非核心业务逻辑之外,如日志、事务管理等,使得代码...

    spring aop配置

    ### Spring AOP配置详解 #### 一、Spring AOP简介 Spring AOP(Aspect Oriented Programming,面向切面编程)是Spring框架中的一个重要模块,它提供了强大的AOP功能支持。AOP是一种编程范式,旨在将横切关注点(如...

    Spring AOP配置

    Spring AOP 配置 Spring AOP(Aspect-Oriented Programming)是一种面向方面编程技术,通过将关注点模块化,提高了系统的可维护性和可扩展性。下面将详细介绍 Spring AOP 的配置和实现。 一、基本概念 * 切面...

    Spring aop 配置

    Spring aop 配置 Spring aspect 配置 Spring advisor 配置 Spring pointcut 配置

    Spring AOP实现机制

    Spring AOP配置 Spring AOP的配置可以通过XML或注解方式进行: - **XML配置**: - 在`&lt;aop:config&gt;`标签内定义切面,`&lt;aop:pointcut&gt;`定义切入点,`&lt;aop:advisor&gt;`定义通知。 - `&lt;aop:aspect&gt;`标签用于定义完整...

    spring aop配置常用jar包

    2. **配置Spring容器**:在Spring的XML配置文件中启用AOP支持,通过`&lt;aop:config&gt;`标签开启,并可以定义切点(pointcut)和通知(advice)。 3. **定义切点**:使用`&lt;aop:pointcut&gt;`定义要拦截的方法或类。切点...

    spring aop jar 包

    在使用Spring AOP时,我们可以通过XML配置或注解的方式来定义切面。例如,可以使用`@Aspect`注解定义一个切面类,`@Before`、`@After`等注解来声明通知,`@Pointcut`定义切点表达式。 在实际开发中,Spring AOP广泛...

    Spring Aop使用实例

    Spring AOP配置 在Spring XML配置文件中,可以声明切面和通知,如下: ```xml &lt;aop:config&gt; &lt;aop:aspect id="loggingAspect" ref="loggingService"&gt; &lt;aop:before method="logBefore" pointcut="execution(* ...

    spring aop 附带测试实例

    在提供的压缩包文件"springAOP"中,可能包含了以下内容: - **切面类(Aspect Class)**:包含切点和通知的Java类,可能使用了`@Aspect`注解。 - **目标类(Target Class)**:被AOP代理的对象,通常包含业务逻辑。...

Global site tag (gtag.js) - Google Analytics