Spring AOP建议不要被执行 [英] Spring AOP advice not getting excuted

查看:0
本文介绍了Spring AOP建议不要被执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring AOP的新手,在实现过程中遇到了一些问题。

我正在尝试将日志记录实现为建议。但建议没有得到执行。

以下是我正在使用的文件。

LoggingAspect.java

package com.demo.conference.aspects;

@Aspect
@Component
public class LoggingAspect {
@Pointcut("execution(* com.demo.conference.daoImpl.ConferenceDAOImpl.*(..))")
public void point(){}


@Pointcut("execution(public * *(..))") //this should work for the public pointcut
private void anyPublicOperation() {}

@Before("anyPublicOperation()")
public void log(){
    System.out.println("--------------------->Aspect execuetd");

}
}

dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

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

 <bean id="hibernateTemplate"      class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory">
        <ref bean="sessionFactory"/>
    </property>
</bean>
</beans>

ApplationConext.xml//用于AOP相关配置

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


   <context:component-scan base-package="com.demo.conference.aspects"/>
   <aop:aspectj-autoproxy/>

</beans>

web.xml

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>

</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
    <listener>
          <listener-class>
          org.springframework.web.context.ContextLoaderListener
        </listener-class>
     </listener>

</web-app>

pom.xml

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>3.2.8.RELEASE</version>
</dependency>
 <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.2.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>3.2.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>2.2.2</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.6.11</version>
</dependency>                    
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.6.11</version>
</dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>3.2.8.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>3.2.8.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>3.2.8.RELEASE</version>
</dependency>

首先,我将所有的XML配置放在单个文件中(在dispatcher-servlet.xml中),然后抛出异常NoSuchBeanDefined。

然后我在一些帖子中发现在单独的文件中声明了与AOP相关的配置。我这样做了。(如果有人能解释创建单独文件的原因) 现在没有任何例外,但建议不会执行。

更新

package com.demo.conference.daoImpl;
@Component
@Repository
public class ConferenceDAOImpl implements ConferenceDAO {

@Autowired
private HibernateTemplate hibernateTemplate;

/**
 * Method to list all the conferences
 * @return list of conferences
 */
public List<Conference> listConference() {
    System.out.println("***ConferenceDAOImpl : listConference");
    System.out.println("Before fetching conferences");
    List<Conference> conferenceList = hibernateTemplate.find("from "+ Conference.class.getName());

    return conferenceList;
}
} 

推荐答案

dispatcher-servlet.xml中将组件扫描更改为com.demo.conference.serviceControllers,在applicationContext.xml中将组件扫描更改为com.demo.conference

HibernateTemplate和其他与Web无关的Bean不应放在dispatcher-servlet.xml中。这些应该在applicationContext.xml中。在dispatcher-servlet.xml中,您应该拥有控制器、视图解析器和其他仅与Web部件相关的Bean。将任何其他Bean移动到applicationContext.xml并配置组件扫描,如我所述:在dispatcher-servlet.xmlUsecom.demo.conference.serviceControllers中,在applicationContext.xmlUsecom.demo.conference中。

故事是这样的:dispatcher-servlet.xml中的Bean形成一个应用程序上下文,该应用程序上下文以applicationContext.xml中的Bean形成的应用程序上下文为父。当Dispatcher-Servlet上下文中的某个对象需要某个Bean时,将在包含它的上下文中搜索它,如果没有找到它,则在父上下文中搜索它。因此,applicationContext.xml中定义的每个Bean都应该很容易被Dispatcher-Servlet上下文中的Bean访问。但反之亦然。应用程序上下文中的Bean无法访问Dispatcher-Servlet中的Bean。

还可以对接口使用@Autowired,而不是具体的类:@Autowired private ConferenceDAO dao;@Autowired private ConferenceService service;,并限制切入点的范围(使用point()并注释anyPublicOperation())!像HibernateTemplate这样的Bean也得到了建议,而Spring正在为它创建代理。如果要在您自己的类中注入HibernateTemplate这是一个具体的类,则创建的代理是一个JDK代理(它实现一个接口),而@Autowired需要HibernateTemplate的实例。

这篇关于Spring AOP建议不要被执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆