Spring Aop实例之xml配置

       上篇博文《3幅图让你了解Spring AOP》中介绍了aop通知类型,AOP的配置方式有2种方式:xml配置和AspectJ注解方式。今天我们就来实践一下xml配置方式。


      我采用的jdk代理,所以首先将接口和实现类代码附上

package com.tgb.aop;

public interface UserManager {

	public String findUserById(int userId);
}


package com.tgb.aop;

public class UserManagerImpl implements UserManager {

	public String findUserById(int userId) {
		System.out.println("---------UserManagerImpl.findUserById()--------");
		if (userId <= 0) {
			throw new IllegalArgumentException("该用户不存在!"); 
		}
		return "张三";
	}
}

       单独写一个Advice通知类进行测试。这个通知类可以换成安全性检测、日志管理等等。

package com.tgb.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

/**
 * Advice通知类
 * 测试after,before,around,throwing,returning Advice.
 * @author Admin
 *
 */
public class XMLAdvice {

	/**
	 * 在核心业务执行前执行,不能阻止核心业务的调用。
	 * @param joinPoint
	 */
	private void doBefore(JoinPoint joinPoint) {
		System.out.println("-----doBefore().invoke-----");
		System.out.println(" 此处意在执行核心业务逻辑前,做一些安全性的判断等等");
		System.out.println(" 可通过joinPoint来获取所需要的内容");
		System.out.println("-----End of doBefore()------"
  • 29
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 33
    评论
以下是一个使用 XML 方式实现的简单 Spring AOP 示例: 首先,需要定义一个切面类和一个目标类。假设切面类是 LoggingAspect,目标类是 HelloWorld。 LoggingAspect.java: ```java public class LoggingAspect { public void beforeAdvice() { System.out.println("Going to setup student profile."); } public void afterAdvice() { System.out.println("Student profile has been setup."); } } ``` HelloWorld.java: ```java public class HelloWorld { private String message; public void setMessage(String message) { this.message = message; } public void getMessage() { System.out.println("Your Message : " + message); } } ``` 然后,在 Spring 配置文件中定义切面和目标对象,并将它们织入到一起。在此示例中,我们使用 XML 配置 AOP。 applicationContext.xml: ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="helloWorld" class="com.example.HelloWorld"> <property name="message" value="Hello World!" /> </bean> <bean id="loggingAspect" class="com.example.LoggingAspect" /> <aop:config> <aop:aspect id="aspect" ref="loggingAspect"> <aop:pointcut id="allMethods" expression="execution(* com.example.HelloWorld.*(..))" /> <aop:before pointcut-ref="allMethods" method="beforeAdvice" /> <aop:after pointcut-ref="allMethods" method="afterAdvice" /> </aop:aspect> </aop:config> </beans> ``` 在此配置文件中,我们定义了一个名为 helloWorld 的 bean(即目标对象)和一个名为 loggingAspect 的 bean(即切面)。然后,我们使用 aop:config 元素定义了一个切面,其中包含一个 pointcut(即 allMethods),它定义了要拦截的方法。我们还使用 aop:before 和 aop:after 元素定义了要在目标方法之前和之后执行的通知(即 beforeAdvice 和 afterAdvice 方法)。 最后,在应用程序中加载 Spring 配置文件,并使用它来获取 HelloWorld bean 并调用 getMessage 方法: ```java import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); obj.getMessage(); } } ``` 输出将如下所示: ``` Going to setup student profile. Your Message : Hello World! Student profile has been setup. ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值