wwwww.wwwwww.wwwww
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!-- 启动对@AspectJ注解的支持 -->
<aop:aspectj-autoproxy/>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<!-- MultiActionController中的两个方法add和test
key必须与方法名一样
比如请求路径http://localhost:8080/SpringMVC01/test.do,类中必须有个方法名为test
-->
<prop key="*myAction.do">multyAction</prop>
<prop key="goAdd.do">goAdd</prop>
</props>
</property>
</bean>
<mvc:interceptors>
<!-- 使用bean定义一个Interceptor,直接定义在mvc:interceptors根下面的Interceptor将拦截所有的请求 -->
<!-- <bean class="cn.edu.zttc.controllerUser.MyHandlerInterceptor"/> -->
<mvc:interceptor>
<mvc:mapping path="/*"/>
<!-- <ref bean="MyHandlerInterceptor"/> -->
<!-- 定义在mvc:interceptor下面的表示是对特定的请求才进行拦截的 -->
<bean class="cn.edu.zttc.controllerUser.MyHandlerInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="goAdd" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="add"></property>
</bean>
<!-- 根据请求参数决定方法 empMutilAction.do?action=add -->
<bean id="multyAction" class="cn.edu.zttc.controllerUser.UserController">
<property name="methodNameResolver">
<bean class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<!-- 指定参数名为action -->
<property name="paramName" value="action" />
</bean>
</property>
<!-- <property name="person">
<ref bean="personServiceBean"/>
</property> -->
<!-- <property name="methodNameResolver">
<bean class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver" />
</property> -->
</bean>
<bean id="MyHandlerInterceptor" class="cn.edu.zttc.controllerUser.MyHandlerInterceptor"></bean>
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!-- 定义默认的异常处理页面,当该异常类型的注册时使用 -->
<property name="defaultErrorView" value="error"></property>
<!--定义异常处理页面用来获取异常信息的变量名,默认名value为exception-->
<property name="exceptionAttribute" value="ex"></property>
<!-- 定义需要特殊处理的异常,用类名或完全路径名作为key,异常也页名作为值 -->
<property name="exceptionMappings">
<props>
<prop key="java.lang.NullPointerException">nullPoint</prop>
<prop key="java.sql.SQLException">error</prop>
<!-- <prop key="java.lang.Exception">add</prop> -->
</props>
</property>
</bean>
<bean id="personServiceBean" class="com.bird.service.impl.PersonServiceBean"/>
<bean id="myInterceptor" class="com.bird.service.MyInterceptor"/>
</beans>
============================================================
package cn.edu.zttc.controllerUser;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class MyHandlerInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("===========HandlerInterceptor1 preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("===========HandlerInterceptor1 postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("===========HandlerInterceptor1 afterCompletion");
}
}
=========================================================================================================
package cn.edu.zttc.aop;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class BeforeAdviceTest {
public static final String EDP = "cn.edu.zttc.controllerUser.UserController.users(..))";
// 匹配 com.wicresoft.app.service.impl 包下所有类的所有方法作为切入点
@Before(EDP)
public void authorith(){
System.out.println("模拟进行权限检查。");
}
}
=================================================package com.bird.service;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
/**
* 切面
* @author Bird
*
*/
@Aspect
public class MyInterceptor {
@Pointcut("execution(* com.bird.service.impl.PersonServiceBean.*(..))")
private void anyMethod(){}//定义一个切入点
@Before("anyMethod() && args(name)")
public void doAccessCheck(String name){
System.out.println(name);
System.out.println("前置通知");
}
@AfterReturning("anyMethod()")
public void doAfter(){
System.out.println("后置通知");
}
@After("anyMethod()")
public void after(){
System.out.println("最终通知");
}
@AfterThrowing("anyMethod()")
public void doAfterThrow(){
System.out.println("例外通知");
}
@Around("anyMethod()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("进入环绕通知");
Object object = pjp.proceed();//执行该方法
System.out.println("退出方法");
return object;
}
}
============================================
package com.bird.service;
public interface PersonServer {
public void save(String name);
public void update(String name, Integer id);
public String getPersonName(Integer id);
}
============================================================
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<aop:aspectj-autoproxy/>
<bean id="personServiceBean" class="com.bird.service.impl.PersonServiceBean"/>
<bean id="myInterceptor" class="com.bird.service.MyInterceptor"/>
</beans>
=============================
package cn.edu.zttc.controllerUser;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.support.RequestContextUtils;
import com.bird.service.PersonServer;
import com.bird.service.impl.PersonServiceBean;
public class UserController extends MultiActionController {
//PersonServer person;
// public PersonServer getPerson() {
// return person;
// }
// public void setPerson(PersonServer person) {
// this.person = person;
// }
public void users(HttpServletRequest req,HttpServletResponse res) {
// person.save(null);
WebApplicationContext webApplicationContext =RequestContextUtils.getWebApplicationContext(req);//get child context
ServletContext servletContext=req.getSession().getServletContext();
Object o =req.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
//System.out.println(o.toString());
//WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
WebApplicationContext webApplicationContext_1 = (WebApplicationContext)servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
WebApplicationContextUtils.getWebApplicationContext(servletContext);//get parent web context
PersonServiceBean bean =(PersonServiceBean)webApplicationContext.getBean("personServiceBean");
bean.save(null);
System.out.println("--==-=-=-=-=-=-=-=");
}
public void add(HttpServletRequest req,HttpServletResponse res) {
System.out.println("add");
}
public void savee(HttpServletRequest req,HttpServletResponse res) {
String aa=null;
aa.split("1");
int a =1/0;
System.out.println("add");
}
}
更多推荐








所有评论(0)