关于Spring AOP:
Java Demo:
注重点:1.获取Request,Response的方式2.重定向和内部跳转的方式
package com.cn.aop;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;
@Aspect
@Component
public class ControllerAspect {
/**
* Log的打印(org.apache.log4j.Logger)
*/
private Logger log = Logger.getLogger(ControllerAspect.class);
/**
* 设定进入切面的类
* 切入式格式("execution(包名[*] + 类名[*] + 方法[*(..)])")
*/
@Pointcut("execution(* com.cn.controller.*.*(..))")
public void pointCut(){
log.debug("Join Controller Aspect");
}
/**
* 前置通知
*/
@Before("pointCut()")
public void before() {
// 在切面中获取HttpRequest
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
// 在切面中获取HttpResponse
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
// 通过Request获取Session,参数true:如果session没有,则新建;false:如果session没有,则返回null
HttpSession session = request.getSession(false);
Object attribute = null;
if (session != null
&& !StringUtils.isEmpty(session.getAttribute("userId"))) {
attribute = session.getAttribute("userId");
} else {
try {
// 内部跳转
request.getRequestDispatcher("occurError").forward(request, response);
// 重定向(rule:重定向地址必须为完整路径,页面URL更改)
//response.sendRedirect("http://localhost:8082/ssm/index.jsp");
} catch (Exception e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
log.debug("Join Controller Before Check Status[End]" + attribute);
}
}
Xml Configure:
注重点:开启Aop的Aspect
<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:context="http://www.springframework.org/schema/context"
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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 自动扫描 -->
<context:component-scan base-package="com.cn" />
<!-- 开启SpringMVC注解扫描 -->
<mvc:annotation-driven />
<!-- Start Aspect -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- Aop Aspect [Not comment] 1.定义切面类2.定义切面 -->
<bean id="controllerAspect" class="demo.aop.ControllerAspect"/>
<aop:config>
<aop:pointcut expression="execution(* demo.controller.*.*(..)) and !(execution(* demo.controller.LoginController.*(..))) and !(execution(* demo.controller.RegisterController.*(..)))"
id="pointCut"/>
<aop:aspect id="aspect" ref="controllerAspect">
<aop:after method="before" pointcut-ref="pointCut"/>
</aop:aspect>
</aop:config>
<!--避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->
</list>
</property>
</bean>
<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认编码 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 文件大小最大值 -->
<property name="maxUploadSize" value="10485760000" />
<!-- 内存中的最大值 -->
<property name="maxInMemorySize" value="40960" />
</bean>
<!-- 定义跳转的文件的前后缀 ,视图模式配置-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix" value="/WEB-INF/JSP/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>