`
yjian84
  • 浏览: 39972 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring aop 如何切面到mvc 的controller

阅读更多
网上搜罗半天,不知道什么原因,看了源码,好像他们说的controller 是不受代理的,也对哈,不知道怎么办,于是在http://stackoverflow.com/questions/17834958/spring-aop-is-not-working-in-with-mvc-structure?rq=1 这个地方有个人说了:

<context:component-scan base-package="com.dao" />
<mvc:annotation-driven/>

<aop:aspectj-autoproxy />  
from application-context.xml to controller-servlet.xml?

The aspects and the beans to be applied needs to be in the same ApplicationContext but ApplicationContext is not aware of WebApplicationContext .


Indeed your controller (annotated by @Controller) and your aspects (annotated by @Aspect) should be in the same Spring context.

Usually people define their controllers in the dispatch-servlet.xml or xxx-servlet.xml and their service beans (including the aspects) in the main applicationContext.xml. It will not work.

When Spring initializes the MVC context, it will create a proxy for your controller but if your aspects are not in the same context, Spring will not create interceptors for them.


这个人说的好像很对啊。我把aspectj 和springmvc的配置文件放到一起就可以用到controller上了。

spring-mvc.xml

	<context:component-scan base-package="com.cms.controller" />
	<context:component-scan base-package="com.cms.aspectj" />
	<!-- <bean id="sysLogAspectJ" class="com.cms.aspectj.SysLogAspectJ" /> -->
	<aop:aspectj-autoproxy proxy-target-class="true">
	<!-- 	<aop:include name="sysLogAspectJ" /> -->
	</aop:aspectj-autoproxy>
	<!-- <mvc:annotation-driven /> -->
	<bean
		class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
	<bean
		class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />


sysLogAspectJ

package com.cms.aspectj;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class SysLogAspectJ {

	@Pointcut("within(@org.springframework.stereotype.Controller *)")
	public void cutController(){
		
	}
	
	@Around("cutController()")
	public Object recordSysLog(ProceedingJoinPoint point) throws Throwable{
		System.out.println("lfkdjj================================================================");
		return point.proceed();
	}
	
	
	
}







分享到:
评论
1 楼 qinshi110 2014-03-24  
好像有用,至少能出拦截信息了!!

相关推荐

    Spring Mvc AOP通过注解方式拦截controller等实现日志管理

    在Spring MVC框架中,AOP(面向切面编程)是一种强大的工具,用于实现跨切面的关注点,如日志管理。本教程将详细介绍如何利用注解来配置和使用AOP来拦截Controller层的方法,以便记录执行过程中的相关信息,实现日志...

    spring MVC AOP注解方式如何拦截controller 例子

    在Spring MVC框架中,AOP(面向切面编程)是一种强大的工具,用于实现跨切面的关注点,如日志、事务管理、权限控制等。当我们想通过注解方式拦截Controller层的方法时,可能会遇到一些问题。本文将详细介绍如何使用...

    Spring MVC AOP通过注解方式拦截Controller等实现日志管理demo版本2

    在Spring MVC框架中,AOP(面向切面编程)是一种强大的工具,用于实现跨切面的关注点,如日志管理、事务控制等。本项目"Spring MVC AOP通过注解方式拦截Controller等实现日志管理demo版本2"是基于注解的AOP实践,...

    Spring IOC AOP MVC 简单例子

    在`SpringAOP`目录中,可能包含了定义切面、通知(advice)、切入点(pointcut)等内容。AOP的实现通常通过定义切面类,其中包含通知方法,并通过切入点表达式确定这些通知在何时何地执行。这使得代码更加模块化,...

    spring_aop1.rar_java aop_spring mvc 例子

    本实例“spring_aop1.rar”是一个关于Spring AOP入门的教程,旨在帮助开发者更好地理解和运用Spring的AOP特性,同时也涉及到Spring MVC的相关知识。下面我们将深入探讨这两个关键概念。 首先,让我们了解一下Spring...

    spring aop ioc mvc

    Spring 框架是 Java 开发中的一个核心框架,它主要由 AOP(面向切面编程)、IOC(控制反转)和 MVC(模型-视图-控制器)三大组件构成。这三大组件是 Spring 提供的强大功能的核心,使得开发更加高效、灵活。 **1. ...

    SpringMVC+springAOP+spring security+Hibernate整合实例代码

    2. Spring AOP:AOP(面向切面编程)允许开发者定义“切面”,即关注点的模块化,如日志、事务管理等。Spring AOP通过代理模式实现了这一概念,可以在不修改原有代码的情况下,在运行时向目标对象添加额外的行为。在...

    spring ioc aop mvc boot-学习笔记.docx

    Spring框架是Java开发中不可或缺的一部分,它为开发者提供了强大的依赖注入(IOC)和面向切面编程(AOP)功能,以及用于构建Web应用程序的MVC框架。Spring Boot则是基于Spring框架构建的应用程序启动器,旨在简化...

    struts2+spring aop demo

    Struts2主要用于处理HTTP请求和响应,提供MVC(Model-View-Controller)架构,而Spring则是一个全面的后端解决方案,包括依赖注入、AOP(面向切面编程)、事务管理等功能。在"struts2+spring aop demo"这个项目中,...

    最全最经典spring-mvc教程

    另外,Spring MVC与Spring框架的其他组件无缝集成,如Spring AOP(面向切面编程)用于实现日志、事务管理等功能,Spring JDBC和MyBatis等持久层框架用于数据库操作,以及Spring Data JPA、Hibernate等ORM工具,使得...

    spring aop日志拦截

    在Spring MVC框架中,AOP(面向切面编程)是一种强大的工具,用于实现日志拦截,特别是对于controller层的操作。AOP允许我们定义横切关注点,这些关注点可以是如日志记录、事务管理、权限检查等通用功能,它们在程序...

    springMVC AOP拦截拦截Controller等实现日志管理

    在Spring MVC中,AOP(面向切面编程)是一种强大的工具,可以让我们在不修改代码的情况下,对程序的特定部分进行增强,例如日志记录、事务管理或性能监控。在这个场景中,我们将讨论如何利用AOP来实现Controller的...

    Spring Boot Aspect 切面 AOP 拦截器 Interceptor 监控control请求耗时

    在Spring Boot应用中,面向切面编程(AOP)是一种强大的设计模式,它允许我们以声明式的方式插入代码,比如日志记录、事务管理或权限检查。Aspect是AOP的核心概念,它封装了关注点,使得我们可以将这些关注点与业务...

    spring-aop-3.0.xsd spring-beans-3.0 spring-context-3.0.xsd spring-mvc-3.1.xsd

    Spring AOP(面向切面编程)是 Spring 框架的一个重要部分,它允许开发者在不修改源代码的情况下,实现跨切面的关注点,如日志、事务管理等。`spring-aop-3.0.xsd` 是 Spring AOP 的 XML 配置文件,定义了与 AOP ...

    Spring AOP 在WEB中的使用

    Spring AOP(Aspect-Oriented Programming,面向切面编程)是Spring框架的重要组成部分,它允许我们通过定义切面来实现代码的模块化,特别是那些具有横切关注点的代码,如日志、事务管理、权限控制等。在Web应用中,...

    Spring MVC jar包

    - **AOP集成**:Spring MVC与Spring的面向切面编程(AOP)无缝集成,可以方便地实现事务管理和其他切面功能。 2. **Hibernate 3.6.8**: - **ORM框架**:Hibernate是一个强大的ORM工具,它将Java对象映射到数据库...

    spring.jar spring-webmvc-struts.jar spring-webmvc.jar

    "spring.jar"是Spring框架的核心库,包含了核心容器、AOP(面向切面编程)、上下文、表达式语言等模块。这个核心库支持依赖注入,这是Spring最著名和最广泛使用的特性,它使得组件之间的耦合度降低,增强了代码的可...

    Spring框架+Spring工作原理+AOP面向切面编程+JDK代理+Cglib代理

    - **面向切面编程**(AOP)是一种编程范式,它允许开发者将跨多个对象的公共行为(如日志记录、安全检查等)定义为“切面”,并通过配置而非显式编程将其添加到应用程序中。 #### 五、Spring MVC的工作流程 Spring...

Global site tag (gtag.js) - Google Analytics