SpringAOP实例

  1. 目标对象的接口:IStudent.java
package com.springAOP.demo;

public interface IStudent {
	
	public   void  addStudent(String name);

}
  1. 目标类:StudentImpl.java
package com.springAOP.demo;

public class StudentImpl implements IStudent{

	@Override
	public void addStudent(String name) {
		 System.out.println( " 欢迎  " + name + "  你加入Spring家庭! " );
	}

}

  1. 前置通知:BeforeAdvice.java
package com.springAOP.demo;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class BeforeAdvice implements MethodBeforeAdvice {

	@Override
	public void before(Method method, Object[] args, Object target) throws Throwable {
		System.out.println(" 这是BeforeAdvice类的before方法. ");
	}

}

  1. 后置通知:AfterAdvice.java
package com.springAOP.demo;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

public class AfterAdvice implements AfterReturningAdvice{

	@Override
	public void afterReturning(Object returnValue,Method method,
			Object[] args,Object target) throws Throwable {
		System.out.println("这是AfterAdvice类的afterReturning方法.");
	}

}

  1. 环绕通知:CompareInterceptor.java
package com.springAOP.demo;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class CompareInterceptor implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		Object result = null;
		String stu_name = invocation.getArguments()[0].toString();
		if (stu_name.equals("test")) {
			// 如果学生是test时,执行目标方法,
			result = invocation.proceed();
		} else {
			System.out.println("此学生是" + stu_name + "而不是test,不批准其加入.");
		}
		return result;
	}

}

  1. 配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://www.springframework.org/schema/beans" 
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-4.2.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.2.xsd 
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">

	<bean id="beforeAdvice" class="com.springAOP.demo.BeforeAdvice"></bean>
	<bean id="afterAdvice" class="com.springAOP.demo.AfterAdvice"></bean>
	<bean id="compareInterceptor" class="com.springAOP.demo.CompareInterceptor"></bean>
	<bean id="studenttarget" class="com.springAOP.demo.StudentImpl"></bean>
	
	<bean id="student" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="proxyInterfaces">
			<value>com.springAOP.demo.IStudent</value>
		</property>
		<property name="interceptorNames">
			<list>
			<value>beforeAdvice</value>
			<value>afterAdvice</value>
			<value>compareInterceptor</value>  
			</list>
		</property>
		<property name="target">
			<ref bean="studenttarget"/>
		</property>

	</bean>


</beans>
  1. 测试类,App.java
package com.springAOP.demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;


public class App {
	public static void main(String[] args) {
		ApplicationContext ctx =new FileSystemXmlApplicationContext("src/main/resources/applicationContext.xml");
		IStudent person = (IStudent)ctx.getBean("student");
		person.addStudent("test");
		//person.addStudent("sunbeam");
	}
}

运行结果

 这是BeforeAdvice类的before方法. 
 欢迎  test  你加入Spring家庭! 
这是AfterAdvice类的afterReturning方法.
  1. maven jar包 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>springAOP</groupId>
  <artifactId>springAOP</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>springAOP</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
  
 	<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-core</artifactId>
	    <version>5.1.5.RELEASE</version>
	</dependency>
  
  	<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-aop</artifactId>
	    <version>5.1.5.RELEASE</version>
	</dependency>
  	
  	<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-context</artifactId>
	    <version>5.1.5.RELEASE</version>
	</dependency>
  	
  	
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

这里项目是一个maven项目引入jar包即可,如果是java项目需要下载对应的jar包
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值