让aop应用于controller

本文介绍了如何将AOP应用到Spring MVC的Controller中,关键在于两点:1. @Aspect注解的AOP类需由Spring管理;2. AOP注解配置需置于Spring MVC配置文件中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

试了几天才发现让aop应用于controller就两个要点,第一,@Aspect注解的类也就是aop类得交给spring代理;第二,开启aop注解的配置必须放在xxx-servlet.xml,也就是springMvc配置文件里,因为controller本身是由springMvc代理而不是spring。

xxx-servlet.xml,就是springMvc的配置文件 
<?xml version="1.0" encoding="UTF-8"?>
<!-- Bean头部 -->
<beans xmlns="http://www.springframework.org/schema/beans"
<span style="white-space:pre">	</span>xmlns:context="http://www.springframework.org/schema/context"
<span style="white-space:pre">	</span>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
<span style="white-space:pre">	</span>xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
<span style="white-space:pre">	</span>xmlns:util="http://www.springframework.org/schema/util"
<span style="white-space:pre">	</span>xsi:schemaLocation="  
    <span style="white-space:pre">	</span>http://www.springframework.org/schema/context  
    <span style="white-space:pre">	</span>http://www.springframework.org/schema/context/spring-context.xsd  
    <span style="white-space:pre">	</span>http://www.springframework.org/schema/beans  
    <span style="white-space:pre">	</span>http://www.springframework.org/schema/beans/spring-beans.xsd  
    <span style="white-space:pre">	</span>http://www.springframework.org/schema/tx  
    <span style="white-space:pre">	</span>http://www.springframework.org/schema/tx/spring-tx.xsd    
   <span style="white-space:pre">	</span>http://www.springframework.org/schema/aop  
   <span style="white-space:pre">	</span>http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <span style="white-space:pre">	</span><!-- 开启aop注解 -->
    <span style="white-space:pre">	</span><aop:aspectj-autoproxy />
    <span style="white-space:pre">	</span><!-- 扫描@controller -->
<span style="white-space:pre">	</span><context:component-scan base-package="controller" />

<span style="white-space:pre">	</span><!--Spring3.1开始的注解 HandlerMapping -->
<span style="white-space:pre">	</span><bean
<span style="white-space:pre">		</span>class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<span style="white-space:pre">	</span><!--Spring3.1开始的注解 HandlerAdapter -->
<span style="white-space:pre">	</span><bean
<span style="white-space:pre">		</span>class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />

<span style="white-space:pre">	</span><!-- ViewResolver -->
<span style="white-space:pre">	</span><bean
<span style="white-space:pre">		</span>class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<span style="white-space:pre">		</span><property name="viewClass"
<span style="white-space:pre">			</span>value="org.springframework.web.servlet.view.JstlView" />
<span style="white-space:pre">		</span><property name="prefix" value="/WEB-INF/jsp/" />
<span style="white-space:pre">		</span><property name="suffix" value=".jsp" />
<span style="white-space:pre">	</span></bean>

</beans>  
applicationContext.xml,就是spring的配置文件,本例只需要将aop应用于controller,所以这里并没有配置aop,否则也需要配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
<span style="white-space:pre">	</span>xmlns:context="http://www.springframework.org/schema/context"
<span style="white-space:pre">	</span>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
<span style="white-space:pre">	</span>xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
<span style="white-space:pre">	</span>xmlns:util="http://www.springframework.org/schema/util"
<span style="white-space:pre">	</span>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/aop  
    http://www.springframework.org/schema/aop/spring-aop.xsd">
    
<span style="white-space:pre">	</span><context:component-scan base-package="dao" />
<pre name="code" class="html"><span>	</span><!-- 扫描@aspect -->
<span>	</span><context:component-scan base-package="aop" />


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/springHibernate" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>


<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="packagesToScan" value="model" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!-- 是否根据需要每次自动创建数据库 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!-- 显示Hibernate持久化操作所生成的SQL -->
<prop key="hibernate.show_sql">true</prop>
<!-- 将SQL脚本进行格式化后再输出 -->
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
</beans> 

aop类,对应要点一,利用注解@Component将此类交给spring代理
@Component
@Aspect
public class AopTest {
	
	@Pointcut("execution(* regist(..))")
	public void testAop() {}
	
	@Before("testAop()")
	public void before() {
		System.out.print("我在注册前输出");
	}
	
}
至此aop就能应用于controller了,在这之前疏忽了一点,就是没有将aop类交给spring代理。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值