SpringMVC用XML方式实现AOP的方法示例

网友投稿 248 2022-12-09


SpringMVC用XML方式实现AOP的方法示例

1.首先创建web工程,之后导入Spring jar包,目录如下

2.文件代码

2.1AfterAdvice

package com.niit.aop;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

/*

* 后置通知

* havingClass方法执行之后才执行。

* 输出日记

* */

public class AfterAdvice implements AfterReturningAdvice {

@Override

public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {

// TODO Auto-generated method stub

System.out.println("后置拦截:下课之后写作业");

}

}

2.2BeforeAdvice

package com.niit.aop;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class BeforeAdvice implements MethodBeforeAdvice {

/*

* 前置通知

* 在havingClass切入点方法执行之前通知

* 用于验证用户的合法性。/判断一些数据是否存在。适用于检索。注册判断用户名是否存在。

* */

@Override

public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {

// TODO Auto-generated method stub

System.out.println("前面拦截:上课之前要点名!在调用havingClass方法之前调用");

}

}

2.3StudentIntercepter

package com.niit.aop;

import org.aopalliance.intercept.MethodInterceptor;

import org.aopalliance.intercept.MethodInvocation;

public class StudentIntercepter implements MethodInterceptor {

@Override

public Object invoke(MethodInvocation method) throws Throwable {

// TODO Auto-generated method stub

if(method.getArguments().length>0) {

String name=(String)method.getArguments()[0];

if("hmq".equals(name)){

System.out.println("中间拦截:你是hmq");

}

else {

System.out.println("中间拦截:你是学生");

}

method.proceed();

}

return null;

}

}

2.4StudentIF

package com.niit.logic;

public interface StudentIF {

public void havingClass(String name);

public void dohomework(String name);

}

2.5Student

package com.niit.logic;

public class Student implements StudentIF {

//作为aop的目标方法

public void havingClass(String name) {

System.out.println("aop的目标方法");

System.out.println(name+"正在上课");

}

public void dohomework(String name) {

System.out.println(name+"正在写作业");

}

}

2.6StudentLogic

package com.niit.logic;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class StudentLogic {

public static void main(String[] args) {

ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");

StudentIF s=(StudentIF)context.getBean("student");

s.havingClass("hmq");

System.out.println("---------------");

s.dohomework("hmq");

System.out.println("---------------");

s.havingClass("abc");

System.out.println("---------------");

s.dohomework("abc");

System.out.println("---------------");

}

}

2.7applicationContext.xml

xmlns:context="http://springframework.org/schema/context"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx" xmlns:p="http://springframework.org/schema/p"

xmlns:util="http://springframework.org/schema/util" xmlns:jdbc="http://springframework.org/schema/jdbc"

xmlns:cache="http://springframework.org/schema/cache"

xsi:schemaLocation="

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/jdbc

http://springframework.org/schema/jdbc/springNHohrNU-jdbc-3.1.xsd

http://springframework.org/schema/cache

http://springframework.org/schema/cache/spring-cache-3.1.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util.xsd">

BeforeAdvice

AfterAdvice

StudentIntercepter

xmlns:context="http://springframework.org/schema/context"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx" xmlns:p="http://springframework.org/schema/p"

xmlns:util="http://springframework.org/schema/util" xmlns:jdbc="http://springframework.org/schema/jdbc"

xmlns:cache="http://springframework.org/schema/cache"

xsi:schemaLocation="

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/jdbc

http://springframework.org/schema/jdbc/springNHohrNU-jdbc-3.1.xsd

http://springframework.org/schema/cache

http://springframework.org/schema/cache/spring-cache-3.1.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util.xsd">

BeforeAdvice

AfterAdvice

StudentIntercepter

2.8SpringMVC.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc.xsd

">

class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">

my-error

sql-error

my-error

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc.xsd

">

class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">

my-error

sql-error

my-error

class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">

my-error

sql-error

my-error

4效果图


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java wait和notify虚假唤醒原理
下一篇:手动构建springBoot启动器过程图解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~