让aop应用于controller

来源:互联网 发布:掌上电力老是网络错误 编辑:程序博客网 时间:2024/06/05 08:56

试了几天才发现让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@Aspectpublic class AopTest {@Pointcut("execution(* regist(..))")public void testAop() {}@Before("testAop()")public void before() {System.out.print("我在注册前输出");}}
至此aop就能应用于controller了,在这之前疏忽了一点,就是没有将aop类交给spring代理。


0 0