2013-10-15 39 views
0

我有一个Spring MVC 3.2.4应用程序,我想使用Spring AOP的。因此,我创建与<aop:aspectj-autoproxy />配置文件。切入点不会被触发

我也写了一个看点(通过在网页代码中找到):

@Component 
@Aspect 
public class PerformanceMonitoring { 
    private static final Logger logger = Logger.getLogger(PerformanceMonitoring.class); 

    public PerformanceMonitoring() { 
     System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++"); 
    } 

// @Around("execution(* com.silabs.moka.*(..))") 
// @Pointcut(value="execution(public * *(..))") 
// @Before("execution(* * com.foo.bar.controller.*.*(..))") 
    @Before("execution(public DefaultClientResponse<UserProfile> com.foo.bar.controller.LoginLogoutController.login(..))") 
    public void xyz(JoinPoint joinPoint) { 
     Signature signature = joinPoint.getSignature(); 
     String methodName = signature.getName(); 
     String stuff = signature.toString(); 
     String arguments = Arrays.toString(joinPoint.getArgs()); 
     System.out.println("################################################"); 
     logger.info("Write something in the log... We are just about to call method: " 
      + methodName + " with arguments " + arguments + "\nand the full toString: " 
      + stuff); 
    } 
} 

(我实现了构造函数只是为了看看这个bean被TOMCAT的启动过程中实例化... ,它是!

但是,每当我进入LoginLogoutController不执行我的方面的XYZ方法的登录方法。我的Pointcut的表达是否错误?

我怎样才能知道春天来执行我的建议吗?

我的控制器:

@Controller 
@PropertySource("classpath:xxx.properties") 
public class LoginLogoutController { 

private static final Logger logger = Logger.getLogger(LoginLogoutController.class); 
@Inject 
protected Environment env; 

/** 
* Returns a json list of first names. 
* 
* @param term the beginning part of the first name 
* @return json string array of first names 
*/ 
@RequestMapping(value = "/login", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) 
@ResponseBody 
public DefaultClientResponse<UserProfile> login(final @RequestParam String userName, final @RequestParam String passWord, final @RequestParam(required = false) String deviceToken, HttpServletRequest req) { 

    logger.info("login: " + userName + ", passWord: " + passWord + ", deviceToken: " + deviceToken); 
//  logger.info("Communicating with host: " + env.getProperty("url.base")); 

    UserProfile up = null; 
    up = userProfileService.findUserProfileByNameAndPassword(userName, passWord); 

    if (up != null) { 
     logger.info("UserProfile for " + up.getFirstName() + " " + up.getLastName() + " found."); 
     HttpSession session = req.getSession(true); 
     logger.info("Created session with ID:[" + session.getId() + "]"); 

     // Set session parameters 
     session.setMaxInactiveInterval(Integer.valueOf(env.getProperty("xxx.session.timeout")) * 60 * 60); 
     session.setAttribute("isAuthenticated", true); 
     session.setAttribute("deviceToken", deviceToken); 
     session.setAttribute("authInfo", up.getAuthInfo()); 
     session.setAttribute("KundenID", up.getUserProfileId()); 
... 

     return new DefaultClientResponse<UserProfile>(session.getId(), 0, "", up); 
    } else { 
     return new DefaultClientResponse<UserProfile>(null, 1, String.format("No user for gogin <%1$s> and password <%2$s> registered.", userName, passWord), null); 
    } 
} 
} 

的aop_config.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xsi:schemaLocation="http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> 

<aop:aspectj-autoproxy /> 
</beans:beans> 

了AppConfig-context.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:beans="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:task="http://www.springframework.org/schema/task" 
xmlns:util="http://www.springframework.org/schema/util" 
xsi:schemaLocation="http://www.springframework.org/schema/mvc 
         http://www.springframework.org/schema/mvc/spring-mvc.xsd 
         http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
         http://www.springframework.org/schema/context 
         http://www.springframework.org/schema/context/spring-context-3.2.xsd 
         http://www.springframework.org/schema/data/jpa 
         http://www.springframework.org/schema/data/jpa/spring-jpa.xsd 
         http://www.springframework.org/schema/tx 
         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
         http://www.springframework.org/schema/task 
         http://www.springframework.org/schema/task/spring-task-3.2.xsd 
         http://www.springframework.org/schema/util 
         http://www.springframework.org/schema/util/spring-util-3.2.xsd"> 


<mvc:annotation-driven /> 
<task:annotation-driven/> 
<context:component-scan base-package="com.foo.bar"> 
    <context:include-filter type="aspectj" expression="com.foo.bar.aop.PerformanceMonitoring" /> 
</context:component-scan> 


<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
<mvc:resources mapping="/resources/**" location="/resources/" /> 

<!--Placeholder configuration--> 
<beans:bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> 
    <beans:property name="locations"> 
     <beans:list> 
      <beans:value>classpath:xxx.properties</beans:value> 
     </beans:list> 
    </beans:property> 
    <beans:property name="ignoreUnresolvablePlaceholders" value="true"/> 
</beans:bean> 


<beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
    <beans:property name="basename" value="classpath:xxx_messages" /> 
    <beans:property name="defaultEncoding" value="UTF-8"/> 
</beans:bean> 

<beans:bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> 
    <beans:property name="paramName" value="lang" /> 
</beans:bean> 

<!-- Declare the Resolver --> 
<beans:bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"> 
    <beans:property name="defaultLocale" value="de"/> 
</beans:bean> 

<beans:bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> 
    <beans:property name="interceptors"> 
     <beans:ref bean="localeChangeInterceptor" /> 
    </beans:property> 
</beans:bean> 



<beans:bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> 
    <beans:property name="contentNegotiationManager"> 
     <beans:bean class="org.springframework.web.accept.ContentNegotiationManager"> 
      <beans:constructor-arg> 
       <beans:bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy"> 
        <beans:constructor-arg> 
         <beans:map> 
          <beans:entry key="json"> 
           <util:constant static-field="org.springframework.http.MediaType.APPLICATION_JSON_VALUE" /> 
          </beans:entry> 
          <beans:entry key="xml"> 
           <util:constant static-field="org.springframework.http.MediaType.APPLICATION_XML_VALUE" /> 
          </beans:entry> 
         </beans:map> 
        </beans:constructor-arg> 
       </beans:bean> 
      </beans:constructor-arg> 
     </beans:bean> 
    </beans:property> 
</beans:bean> 




<!-- Resolves views selected for rendering by @Controllers to .jsp resources 
in the /WEB-INF/views directory --> 
    <beans:bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="prefix" value="/WEB-INF/views/" /> 
     <beans:property name="suffix" value=".jsp" /> 
    </beans:bean> 

<!--Persistence data source--> 
<beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
      p:driverClassName="com.mysql.jdbc.Driver" 
      p:url="jdbc:mysql://localhost:3306/m12" 
      p:username="root" 
      p:password=""> 
</beans:bean> 

<!--Persistence JPA--> 
<jpa:repositories base-package="com.foo.bar.repository"/> 

<beans:bean class="org.springframework.orm.jpa.JpaTransactionManager" 
      id="transactionManager"> 
    <beans:property name="entityManagerFactory" 
        ref="entityManagerFactory" /> 
    <beans:property name="jpaDialect"> 
     <beans:bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /> 
    </beans:property> 
</beans:bean> 
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" /> 

<beans:bean id="entityManagerFactory" 
      autowire="default" 
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <beans:property name="dataSource" ref="dataSource" /> 
    <beans:property name="packagesToScan" value="com.foo.bar.repository, com.silabs.moka.domain" /> 
    <beans:property name="jpaVendorAdapter"> 
     <beans:bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
      <beans:property name="generateDdl" value="false" /> 
      <beans:property name="showSql" value="false" /> 
      <beans:property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/> 
      <beans:property name="database" value="MYSQL"/> 
     </beans:bean> 
    </beans:property> 
    <!-- put any ORM specific stuff here --> 
    <beans:property name="jpaProperties"> 
     <beans:props> 
      <beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop> 
     </beans:props> 
    </beans:property> 
</beans:bean> 

<!-- <beans:bean id="persistenceExceptionTranslationPostProcessor" 
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> --> 


我已分裂的大的AppConfig-context.xml的成较小的块:

  1. 根-config.xml中
  2. TX-context.xml中
  3. AOP-context.xml中
  4. 的AppConfig上下文.xml(即DispatcherServlet的MVC上下文)

虽然在web.xml具有这样的:

<context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/aop-context.xml 
        /WEB-INF/spring/root-context.xml 
        /WEB-INF/spring/tx-context.xml 
     </param-value> 
    </context-param> 

至少AOP-context.xml中似乎并没有被考虑。当我使用<beans:import resource="../aop-context.xml"/>导入的AppConfig-context.xml中AOP-context.xml中我总是得到

HTTP状态404 - /bar/WEB-INF/views/.jsp

类型状态报告

消息/bar/WEB-INF/views/.jsp

从TOMCAT在浏览器中。 description请求的资源不可用。

+0

请发布您的上下文和您的'@ Controller'类。 –

回答

1

我从来没有成功地将Spring MVC(对于我的@Controller类)和其他所有不错的AOP内容(例如@Transactional)都放在一个类中。这也可能只是原因,但这是一个猜测,因为我们没有看到所有的设置。


扩展答案(现在的配置给出):

你有这三对同样的情况下:

<aop:aspectj-autoproxy /> 
<mvc:annotation-driven /> 
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" /> 

你应该有一个控制器和一切独立的上下文。请参考@Service are constructed twice以供参考。 AOP(以及事务)在控制器的上下文中不起作用。

+0

配置xml文件以及控制器现在被添加... –

+0

我做了一个建议,但这导致现在既没有我的服务也没有找到我的存储库。 我把组件扫描除了控制器之外的所有东西放到root-context.xml中,然后扫描appConfig-context.xml(它是servlet-context.xml)中的控制器。 在web.xml中,我注册了root-config.xml以及aop_context.xml作为contectConfigLocation。 –

+0

我解决了这个问题,通过分割现有的复杂的比较配置在较小的部分。提取一部分影响其他部分,因此发生了一些异常。 服务和控制器现在被发现。按照您的建议,除了组件扫描标签之外,还需要标签。没有这个标签,无论是服务还是控制器......都可以找到。 –