sping中如何配置拦截器
配置如下
<bean id="aroundAdvice" class="com.cs.advice.AroundAdvice" ></bean>
<bean id="beforeAdvice" class="com.cs.advice.BeforeAdvice" ></bean>
<!-- 相当于JDKProxy,不过他能代理任何类的对象 -->
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean" >
<!-- 以下是JDK动态代理要求的传入的接口 -->
<!-- 第一步:配置接口 -->
<property name="proxyInterfaces">
<list>
<value>com.cs.dao.UserDao</value>
</list>
</property>
<!-- 这是如何配置cglib 方式 cglib方式是spring已经提供的只需要配置即可 能代理任何类 与上面的jdk配置只能二选一
<property name="proxyTargetClass" value="true" />
-->
<!-- 第二步:配置目标对象 , 及腰拦截的对象是哪个 -->
<property name="target">
<bean class="com.cs.dao.UserDaoImpl"></bean>
</property>
<!-- 第三步:配置拦截器 , 及使用哪些拦截器 -->
<property name="interceptorNames">
<list >
<value>aroundAdvice</value>
</list>
</property>
</bean>
aroundAdvice的代码如下:(本人觉得此方法较好)
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class AroundAdvice implements MethodInterceptor {
public Object invoke(MethodInvocation arg0) throws Throwable {
System.out.println("开启事务");
Object obj = arg0.proceed() ; // 相当于调用真正的方法
System.out.println("关闭事务");
return obj;
}
}
beforeAdvice的代码如下:(二选一)
import org.springframework.aop.MethodBeforeAdvice;
public class BeforeAdvice implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("日志记录:" + method.getName() + " is inteceptor");
//在方法调用之前做的事情
}
}
测试类:
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml") ;
UserDao uDao = (UserDao)ctx.getBean("proxy") ; //如果配置的是cglib方式 ,则能代理任何类
uDao.addUser(new User()) ;
}
分享到:
相关推荐
spring boot jpa连接数据库,设置拦截器拦截指定路径下的文件。
spring-boot 拦截器 登录拦截spring-boot 拦截器 登录拦截spring-boot 拦截器 登录拦截spring-boot 拦截器 登录拦截
spring拦截器,高级参数绑定,controller返回值
Spring AOP 拦截器 Advisor Spring AOP 拦截器 Advisor
NULL 博文链接:https://gongsy.iteye.com/blog/1856240
CXF3.0 Spring3.2 自定义拦截器
Spring MVC 拦截器Spring MVC 拦截器
使用CGLIB编写的Spring的拦截器 非常使用的哦
初学者很容易将 Spring MVC 拦截器 和 Spring Cloud OpenFeign 拦截器搞混,误以为OpenFeign拦截器就是SpringMVC拦截器:Spring MVC拦截器发生在客户端 和 服务端之间,在客户端向服务端发送请求时进行拦截处理。...
自己spring boot 拦截器,可以自定义限制跳转路径及自定义不拦截哪些具体路径,具体设置可以看注释,一看就懂,引用哪些包已经包含了,不明白的请浏览,看到后第一时间给您回复
针对spring boot 集成jpa sql操作进行自定义分库分表逻辑
spring mvc 拦截器获取请求数据信息 解压之后放到项目中 直接运行就可以了 (将流多次运用)
spring+springMVC+mybatis拦截器分页
AOP拦截器示例,告诉我们初学者理解aop,通过一个银行转帐说明问题。
springboot spring aop 拦截器 注解方式实现脱敏(涉及到:pom.xml -->application.properties --->启动类-->拦截器)
这一版本的项目中新增了拦截器的一个demo
主要介绍了spring boot配置拦截器代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Spring Boot 拦截器 各项日志 通过kibana具体查询分析 Advanced Settings [7.2.0] 综合分析 定时任务执行日志 数据同步日志 用户访问操作日志 异常日志 内存日志 Spring逻辑参数执行日志 SQL...
1.针对SpringMVC注解的配置,可起到入门的作用 2.Spring+JdbcTemplate事物管理 3.Spring+Hibernate事物管理 ...5.Spring配置拦截器 6.Spring通过拦截器实现防止重复提交实例 对学习,会起到非常好的效果
idea软件。SpringBoot的拦截器的博客所写的例子。preHandle()方法的返回值true和false的详细区别还未描述