`

SpringXML方式实现AOP demo

阅读更多
1、新建一个java的工程,导入spring需要的jar包与开发切面需要的jar包。

dist\spring.jar
lib\jakarta-commons\commons-logging.jar
如果使用了切面编程(AOP),还需要下列jar文件
lib/aspectj/aspectjweaver.jar和aspectjrt.jar
lib/cglib/cglib-nodep-2.1_3.jar
如果使用了JSR-250中的注解,如@Resource/@PostConstruct/@PreDestroy,还需要下列jar文件
lib\j2ee\common-annotations.jar

2、Person.java

package cn.ehoo.bean;
/**
 * @author whp
 * @Email whp@ehoo.cn
 * @Jan 4, 2011
 * 
 */
public class Person {
	private Long id;
	private String name;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}



2、PersonService.java 与PersonServiceBean.java

PersonService.java
package cn.ehoo.service;

import cn.ehoo.bean.Person;

/**
 *@author whp
 *@Email whp@ehoo.cn
 *@Jan 4, 2011
 *
 */
public interface PersonService {

	public void save(Person person);

}


PersonServiceBean.java
package cn.ehoo.service.impl;


import cn.ehoo.bean.Person;
import cn.ehoo.service.PersonService;

/**
 * @author whp
 * @Email whp@ehoo.cn
 * @Jan 4, 2011
 * 
 */
public class PersonServiceBean implements PersonService {
	private String user;

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public PersonServiceBean() {
	}

	public PersonServiceBean(String user) {
		super();
		this.user = user;
	}

	public void save(Person person) {
		System.out.println("执行PerServiceBean的save方法");
		//throw new RuntimeException("======");
	}
}

3、MyInterceptor.java

package cn.ehoo.service.impl;

import org.aspectj.lang.ProceedingJoinPoint;

/**
 * @author whp
 * @Email whp@ehoo.cn
 * @Jan 4, 2011
 * 
 */
public class MyInterceptor {
	private void anyMethod() {
	}
	public void doAccessCheck() {
		System.out.println("执行前置通知");
	}

	public void doReturnCheck() {
		System.out.println("执行后置通知");
	}

	public void doReleaseAction() {
		System.out.println("执行最终通知");
	}

	public void doExceptionAction() {
		System.out.println("执行例外通知");
	}

	public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("执行环绕通知");
		Object retule = pjp.proceed();
		System.out.println("退出环绕通知");
		return retule;
	}

}


4 在src下对beans.xml进行配置
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
	<bean name="personService" class="cn.ehoo.service.impl.PersonServiceBean" />
	<bean name="myInterceptor" class="cn.ehoo.service.impl.MyInterceptor" />
	<aop:config>
		<aop:aspect id="myaop" ref="myInterceptor">
			<aop:pointcut id="mycut" expression="execution(* cn.ehoo.service.impl.PersonServiceBean.*(..))" /><!--切入点表达式 -->
			<aop:before pointcut-ref="mycut" method="doAccessCheck" /><!--前置通知 -->
			<aop:after-returning pointcut-ref="mycut" method="doReturnCheck" /><!--后置通知 -->
			<aop:after-throwing pointcut-ref="mycut" method="doExceptionAction" /><!--例外通知 -->
			<aop:after pointcut-ref="mycut" method="doReleaseAction" /><!--执行最终通知 -->
			<aop:around pointcut-ref="mycut" method="doBasicProfiling" /><!--执行环绕通知-->
		</aop:aspect>
	</aop:config>

</beans>

5、测试类
package junit.test;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.ehoo.bean.Person;
import cn.ehoo.service.PersonService;

/**
 * @author whp
 * @Email whp@ehoo.cn
 * @Jan 4, 2011
 * 
 */
public class AOPTest {
	static PersonService personService;
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		AbstractApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
		personService = (PersonService) cxt.getBean("personService");
	}

	/**
	 * @author whp
	 * @Email whp@ehoo.cn
	 * @Jan 4, 2011
	 * 
	 */
	
	public static void main(String[] args) {
		try {
			setUpBeforeClass();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Test
	public void saveTest() {
		personService.save(new Person());
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}
}
分享到:
评论

相关推荐

    spring xml 实现aop切面编程

    spring xml 实现aop切面编程 内附注释,希望对入门的新手有帮助

    SpringAop学习笔记以及实现Demo

    SpringAOP学习笔记以及四个可运行的Demo,涵盖经典代理模式、基于注解、基于xml配置这3方面的Demo

    Spring-AOP demo

    spring aop 采用注解定义以及xml定义,在java四层结构中无缝实现

    SpringAop两种配置demo

    详细讲解了aop的xml配置和注解的方式配置,和本人的博客想应

    springAOP demo 带错误解决文档

    Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/aop] Offending resource: class path resource [beans.xml] at org....

    Spring AOP demo

    基于注解与 XML 配置文件两种形式的 AOP demo。 基于 xml 配置文件的 aop 管理 ```xml &lt;!-- 配置切面的bean --&gt; &lt;bean id="loggingAspect" class="com.jas.aop.xml.LoggingAspect"/&gt; &lt;aop:config&gt; &lt;!...

    spring-aspectj-ltw-xml-based-demo

    Spring demo to demonstrate aop using aspectj with xml based configuration.

    spring注解事务实现demo

    声明式事务管理也有两种常用的方式,一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解。显然基于注解的方式更简单易用,更清爽。spring注解事务实现demo

    JavaEE Spring AOP使用

    压缩包中函数Spring AOP开发时使用注解和xml文件配置demo

    AOP 和 Aspect 注解切入 测试 Demo

    基于 MethodBeforeAdvice、AfterReturningAdvice 利用 Spring Api 定义前、后置处理方法,并通过代理工厂类获取代理对象(代码或Xml配置实现) 2.ProxyFactoryBean 显式地设置 Advisors、Advice、Target等(基于代理...

    spring练习项目.7z

    jdkproxy-transaction,jdkproxy-salary,day02-itheima11-spring-08-cglibproxy,day02-itheima11-spring-09-aop.xml,day02-itheima11-spring-10-aop.xml.advice,day02-itheima11-spring-11-aop.xml.exception等Demo

    Spring基础demo学习项目.7z

    资料包含day03-itheima11-spring-01-aop-xml-methodinvocation-count,day03-itheima11-spring-02-aop-xml-privilege,day03-itheima11-spring-03-aop-xml-salary,day03-itheima11-spring-04-aop-annotation,day03...

    spring aop的两种配置方式.docx

    sring aop的方式有两种:(1)xml文件配置方式(2)注解的方式实现,我们可以先通过一个demo认识spring aop的实现,然后再对其进行详细的解释。

    SSM框架教程Spring+SpringMVC+MyBatis全覆盖_Java热门框架视频教程

    视频详细讲解,需要的小伙伴自行百度网盘下载,链接见附件,永久有效。...2、MyBatis的Dao层实现方式 3、MyBatis的映射文件深入 4、MyBatis的核心文件深入 5、MyBatis的多表操作 6、MyBatis的注解开发

    ssh整合demo 整合在一起

    ssh整合demo 整合, structs 附带了多个测试页面可以跳转,写了一个简单action,structs.xml 使用了模糊匹配, spring 注释方式 注入依赖,xml方式实现aop,hibernate 配置好数据库,可自动生成表,标准的model ,dao...

    SpringDemo:Spring 框架知识案例

    1.创建第一个 Spring 程序案例 2.Spring IoC案例 ...5.Spring xml 配置案例 6.Spring Annontation案例 7.Spring 泛型依赖注入案例 8.Spring AOP案例 9.Spring JDBCTemplate案例 10.Spring 事务案例

    Spring demo

    简易的spring demo,包含了bean自动装配,泛型依赖注入,Aop切面编程的实例

    xmljava系统源码-spring-boot-demo:springbootdemo是一个用来深度学习并实战springboot的项目,该项

    |哨兵||集群||Lettuce)、RocketMQ-demo(RocketMQ,实现消息的发送和接收)、RocketMQ-shop-project(RocketMQ模拟电商网站购物)、Spring Security(实现动态权限的实现||Spring Cloud Security OAuth2)、websocket...

    Spring demo 2017-04-18

    spring demo,包含了bean的自动装配,aop以及泛型依赖注入的实现,jdbc和jdbc的xml配置以及注解两种实现方式。每个功能的程序入口为各自包里面的main方法

Global site tag (gtag.js) - Google Analytics