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
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 自动扫描web包,将带有注解的类 纳入spring容器管理 --> <!-- <context:component-scan base-package="com.niit.beans"> </context:component-scan> --> <!-- 定义通知 --> <bean id="BeforeAdvice" class="com.niit.aop.BeforeAdvice"></bean> <bean id="AfterAdvice" class="com.niit.aop.AfterAdvice"></bean> <!-- 定义拦截器 --> <bean id="StudentIntercepter" class="com.niit.aop.StudentIntercepter"> </bean> <!-- 定义目标 --> <bean id="target" class="com.niit.logic.Student"></bean> <!-- 切入点 哪些方法会被aop影响 可选 --> <bean id="pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"> <!-- 模式 --> <property name="pattern" value=".*dohomework.*" > </property> </bean> <!-- 通知器advisor 连接通知和切入点 可选--> <bean id="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"> <property name="advice" ref="BeforeAdvice"/> <property name="pointcut" ref="pointcut"/> </bean> <!-- 定义代理 --> <bean id="student" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- 注入目标 --> <property name="target" ref="target"></property> <!-- 设置拦截器 --> <property name="interceptorNames"> <list> <value>BeforeAdvice</value> <value>AfterAdvice</value> <value>StudentIntercepter</value> </list> </property> <!-- 定义代理接口 --> <property name="proxyInterfaces" value="com.niit.logic.StudentIF"></property> </bean> </beans>
2.8SpringMVC.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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!-- 包扫描:扫描注解所在的包controller类所在的包 --> <context:component-scan base-package="com.niit.controller"></context:component-scan> <context:component-scan base-package="com.niit.service" /> <context:component-scan base-package="com.niit.dao" /> <!-- 开启注解驱动AnnotationHandlerMapping --> <mvc:annotation-driven/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"/> <property name="suffix" value=".jsp"/> </bean> <!--SimpleMappingExceptionResolver(异常类与 View 的对应关系) --> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <!-- 定义默认的异常处理页面,当该异常类型注册时使用 --> <property name="defaultErrorView" value="error"></property> <!-- 定义异常处理页面用来获取异常信息的变量名,默认名为exception --> <property name="exceptionAttribute" value="ex"></property> <!-- 定义需要特殊处理的异常,用类名或完全路径名作为key,异常页名作为值 --> <property name="exceptionMappings"> <props> <prop key="exception.MyException">my-error</prop> <prop key="java.sql.SQLException">sql-error</prop> <prop key="exception.KeyWordNotFoundException">my-error</prop> <!-- 在这里还可以继续扩展对不同异常类型的处理 --> </props> </property> </bean> <!--托管MyExceptionHandler--> <!--<bean class="com.niit.exception.MyExceptionHandler"/> --> </beans>
4效果图