spring-aop的xml配置实现

beans.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: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.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- Spring 读取db.properties -->
	<context:property-placeholder location="classpath:db.properties" />

	<bean id="userService" class="com.liuyun.aop.service.impl.UserServiceImpl">
		<property name="userDao" ref="userDao"></property>
	</bean>

	<bean id="userDao" class="com.liuyun.aop.dao.impl.UserDaoImpl">
		<property name="jdbcTemplate" ref="jdbcTemplate"></property>
	</bean>

	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

	<!-- spring-jdbc内置数据源 -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${jdbc.driverClass}"></property>
		<property name="url" value="${jdbc.jdbcUrl}"></property>
		<property name="username" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>

	<!-- 通知类交给spring进行管理 -->
	<bean id="myAopAdvice" class="com.liuyun.aop.utils.MyAopAdvice"></bean>

	<aop:config>
		<!-- 设置切入点(哪些类的哪些方法需要增强) -->
		<aop:pointcut id="servicePt" expression="execution(* com.liuyun.aop.service.impl.*.*(..))" />
		<!-- 使用切面将切入点和通知类进行绑定:即通知类的哪些方法在哪个位置对切入点方法进行增强-->
		<aop:aspect id="advice" ref="myAopAdvice">
			<aop:before method="before" pointcut-ref="servicePt" />
			<aop:after-returning method="afterReturning" pointcut-ref="servicePt" />
			<aop:after-throwing method="afterThrowing" pointcut-ref="servicePt" />
			<aop:after method="after" pointcut-ref="servicePt" />
			<aop:around method="around" pointcut-ref="servicePt" />
		</aop:aspect>
	</aop:config>
</beans>

该xml中的关键代码:
在这里插入图片描述
通知类:

package com.liuyun.aop.utils;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;

/**
 * @author liuyun
 * @since 2019年9月1日下午9:02:58
 */
@Component
public class MyAopAdvice {
	public void before() {
		System.out.println("...before方法");
	}

	public void afterReturning() {
		System.out.println("...afterReturning方法");
	}

	public void afterThrowing() {
		System.out.println("...afterThrowing方法");
	}

	public void after() {
		System.out.println("...after方法");
	}

	/**
	 * around方式可以实现其他四种方式:before、afterReturning、afterThrowing、after
	 */
	public Object around(ProceedingJoinPoint pjp) throws Throwable {
		Object resutlValue = null;
		try {
			System.out.println("around......before方法");
			resutlValue = pjp.proceed();// 调用被代理的方法
			System.out.println("around......afterReturning方法");
		} catch (Throwable e) {
			System.out.println("around......afterThrowing方法");
			throw e;
		} finally {
			System.out.println("around......after方法");
		}
		return resutlValue;
	}
}

完整代码:https://github.com/liuyunHappy/spring-aop-xml.git

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值