Spring Aop XML



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

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

	<!--XML配置 拦截类	-->
	<bean id="logInterceptor" class="com.mth.aop.LogInterceptor"></bean>
	
<!--第一种写法	-->
<!--	<aop:config>-->

<!--		 在aspect外配置的point是全局切入点	-->
<!--		<aop:pointcut expression="" id="p2" />-->

<!--		定义一个切面-->
<!--		<aop:aspect id="a1" ref="logInterceptor">-->

<!--			定义一个切入点 在aspect里面配置的只有自己可以使用-->
<!--			<aop:pointcut expression="execution(public void com.mth.impl..*.*(..))"-->
<!--				id="p1" />-->

<!--			<aop:before pointcut-ref="p1" method="before" />-->
<!--			<aop:after pointcut-ref="p1" method="afterMethod" />-->

<!--		</aop:aspect>-->
<!--	</aop:config>-->

<!--第二种写法 直接指定pointcut-->
	<aop:config>
		<aop:aspect id="a1" ref="logInterceptor">
			<aop:before  method="before" pointcut="execution(public void com.mth.impl..*.*(..))"/>
		</aop:aspect>
	</aop:config>
</beans>

LogInterceptor 类:

package com.mth.aop;

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.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 
 * @ClassName: LogInterceptor
 * @Description: Aop 面像切面编程XML方式 此类也要交给Spring初始化就需要添加注解@Component
 * @author mth 75100313@qq.com
 * @date 2014-1-20 上午08:44:19
 * 
 */
// @Aspect
// @Component
public class LogInterceptor {
	// 多个一样的植入点可以抽出来定义
	// @Pointcut("execution(public void com.mth.impl..*.*(..))")
	public void myMethod() {

	}

	// 在方法执行之前先要执行这个方法
	// 植入点语句
	// @Before("execution(public void com.mth.impl.StuDaoImpl.save*(com.mth.bean.Student))")
	public void before() {
		System.out.println("before save");
	}

	// 方法正常执行完成之后(参数就是方法名)
	// @AfterReturning("myMethod()")
	public void afterReturning() {
		System.out.println("after save");
	}

	// 方法抛异常之后执行(StuDaoImpl里面的保存学生方法)
	// @AfterThrowing("myMethod()")
	public void afterThrowing() {
		System.out.println("afterThrowing");
	}

	// 环绕方法
	// @Around("myMethod()")
	public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("around save start");
		pjp.proceed();
		System.out.println("arround save end");
	}

	// 最终通知 任意返回值类型 com.mth下的任意包中的任意类中的任意方法 参数任意
	// @After("execution(* com.mth..*.*(..))")
	public void afterMethod() {
		System.out.println("---------after----------");
	}

}

StuDaoImpl:

package com.mth.impl;

import org.aspectj.lang.annotation.AfterThrowing;
import org.springframework.stereotype.Component;

import com.mth.bean.Student;
import com.mth.dao.IStuDao;

@Component("stuDao")
public class StuDaoImpl implements IStuDao {

	@Override
	public void saveStu(Student stu) {
		System.out.println("保存学生");
		//配合注解@AfterThrowing测试
		// throw new RuntimeException();
	}

}

Service:

package com.mth.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;


import com.mth.bean.Student;
import com.mth.dao.IStuDao;
@Component("ser")
public class Service {
	private IStuDao dao;

	public IStuDao getDao() {
		return dao;
	}

	@Resource(name="stuDao")
	public void setDao(IStuDao dao) {
		this.dao = dao;
	}

	public void saveStu(Student student) {
		dao.saveStu(student);
	}

}


测试代码:

package com.mth.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mth.bean.Student;
import com.mth.service.Service;

public class Test {

	/**
	 * @Title: main
	 * @Description: 测试Spring注解
	 * @param @param args 设定文件
	 * @return void 返回类型
	 * @throws
	 */
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		Service service = (Service) context.getBean("ser");
		service.saveStu(new Student());
	}
}

附第一种配置写法:

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

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

	<!--XML配置 拦截类	-->
	<bean id="logInterceptor" class="com.mth.aop.LogInterceptor"></bean>

	<aop:config>
		<aop:pointcut expression="" id="p2" />
		<aop:aspect id="a1" ref="logInterceptor">
			<aop:pointcut expression="execution(public void com.mth.impl..*.*(..))"
				id="p1" />
			<aop:before pointcut-ref="p1" method="before" />
			<aop:after pointcut-ref="p1" method="afterMethod" />
		</aop:aspect>
	</aop:config>

</beans>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值