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>


Spring AOPSpring框架中的一个重要模块,它通过动态代理实现了面向切面编程的思想。下面是Spring AOPXML配置详解: 1. 配置命名空间和约束 在使用Spring AOP之前,需要在XML配置文件中声明Spring AOP的命名空间和约束,如下所示: ```xml <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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> ``` 2. 配置切面 在Spring AOP中,切面由切点和通知组成。切点定义了在哪些方法上进行拦截,通知定义了在拦截时要执行的逻辑。下面是一个切面的配置示例: ```xml <bean id="myAspect" class="com.example.MyAspect"> <property name="transactionManager" ref="txManager"/> </bean> <aop:config> <aop:aspect ref="myAspect"> <aop:pointcut id="serviceMethods" expression="execution(* com.example.Service.*(..))"/> <aop:before pointcut-ref="serviceMethods" method="beginTransaction"/> <aop:after-returning pointcut-ref="serviceMethods" method="commitTransaction"/> <aop:after-throwing pointcut-ref="serviceMethods" method="rollbackTransaction"/> </aop:aspect> </aop:config> ``` 上面的例子中,声明了一个名为“myAspect”的切面,并定义了一个名为“serviceMethods”的切点,拦截com.example.Service包中所有方法的执行。在拦截时,分别执行了beginTransaction()、commitTransaction()和rollbackTransaction()方法。 3. 配置通知 通知是切面中的一个组成部分,它定义了在什么时候执行切面的逻辑。Spring AOP支持五种类型的通知:前置通知、后置通知、返回通知、异常通知和环绕通知。下面是通知的配置示例: ```xml <aop:before pointcut="execution(* com.example.Service.*(..))" method="beforeAdvice"/> <aop:after-returning pointcut="execution(* com.example.Service.*(..))" method="afterReturningAdvice"/> <aop:after-throwing pointcut="execution(* com.example.Service.*(..))" method="afterThrowingAdvice"/> <aop:around pointcut="execution(* com.example.Service.*(..))" method="aroundAdvice"/> ``` 上面的例子中,分别配置了前置通知、返回通知、异常通知和环绕通知,它们都拦截com.example.Service包中的所有方法。在拦截时,分别执行了beforeAdvice()、afterReturningAdvice()、afterThrowingAdvice()和aroundAdvice()方法。 4. 配置引入 引入是Spring AOP中的一个特殊功能,它允许将额外的方法和属性添加到现有的类中,而不需要修改原始类的代码。下面是引入的配置示例: ```xml <aop:config> <aop:aspect ref="myAspect"> <aop:declare-parents types-matching="com.example.Service+" implement-interface="com.example.Transactional" default-impl="com.example.TransactionalImpl"/> </aop:aspect> </aop:config> ``` 上面的例子中,声明了一个名为“myAspect”的切面,并引入了接口com.example.Transactional,将其实现类指定为com.example.TransactionalImpl。这样,在运行时,com.example.Service类就自动实现了com.example.Transactional接口。 以上就是Spring AOPXML配置详解。在实际开发中,可以根据业务需求和实际情况选择合适的配置方式来实现切面编程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值