我想打印"地狱是啊"在@ValidPassword
注释出现后在控制台中。这就是我的方式。
注释:
public @interface ValidPassword {
}
方面:
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class ValidPasswordAspect {
@Before("@annotation(ru.tenet.smsc.annotation.ValidPassword)")
public void myAdvice(){
System.out.println("Hell yeah");
}
}
的xml:
<bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<array>
<bean class = "org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
</bean>
</array>
</property>
</bean>
<context:annotation-config />
<context:component-scan base-package="ru.tenet.smsc" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="saveBatchService" class="ru.tenet.smsc.service.SaveBatchServiceImpl" />
<context:component-scan base-package="ru.tenet.smsc" />
<mvc:annotation-driven />
<tx:annotation-driven />
<task:annotation-driven />
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<property name="maxUploadSize" value="6666666" />
<property name="maxInMemorySize" value="6666666" />
</bean>
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass"><value>org.springframework.security.core.context.SecurityContextHolder</value></property>
<property name="targetMethod"><value>setStrategyName</value></property>
<property name="arguments">
<list>
<value>MODE_INHERITABLETHREADLOCAL</value>
</list>
</property>
</bean>
<bean name="validPasswordAspect" class="ru.tenet.smsc.aop.ValidPasswordAspect" />
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>/WEB-INF/db/hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.dialect"> org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.connection.useUnicode">true</prop>
<prop key="hibernate.connection.characterEncoding">UTF-8</prop>
<prop key="hibernate.connection.charSet">UTF-8</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
用法:
@RequestMapping(value = "/sms/single", method = RequestMethod.GET)
@ValidPassword
public String singleSMSPage(Map<String, Object> map,
@ModelAttribute("singleSMSForm") SingleSMSForm singleSMSForm) {
map.put("title", "Отправка одного сообщения");
return "singleSMS";
}
但是没有&#34; Hell Yeah&#34;在控制台。为什么呢?
答案 0 :(得分:1)
aop:aspectj-autoproxy
需要在您希望它作用的同一应用程序上下文中指定,这意味着您的控制器被定义(或自动检测)的位置。
这是一个经常发生的错误,它不起作用的原因是Aspect检测和实现是BeanPostProcessor
工作的结果。并且BeanPostProcessor
在应用程序上下文中定义它们。在Web应用程序中,通常有两个这样的应用程序上下文和很多次aop东西,如果在正确的应用程序上下文中没有定义这样的行为,事务性,自动装配可能会失败。