大家好!我自己写了一个ssh的项目,使用了spring 的aop,使用xml方式,没有使用注解。
tomcat运行起来之后,执行操作时,并没有执行aspect中的方法。但是我使用junit写的测试类可以执行aspect中的方法。
测试类如下:
public class Test {
private static ApplicationContext ctx ;
@BeforeClass
public static void before() {
 ctx = new ClassPathXmlApplicationContext("beans.xml");
// catDao = (ForumDao) ctx.getBean("catDao");
}
@org.junit.Test
public void test001(){
IUserService userService=(IUserService)ctx.getBean("userService");
// System.out.println("userService:"+userService);
userService.login("a", "b");
}

}
为什么以web app的方式运行时,aspect的方法没有被执行。这到底是什么原因?帮帮忙啊
AOPTomcatXMLSSH

解决方案 »

  1.   

    beans.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="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/aop
               http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
               http://www.springframework.org/schema/tx 
               http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
    default-lazy-init="false"> <!--<context:annotation-config /> <context:component-scan base-package="com" 
    /> -->
    <bean id="dataSource" destroy-method="close"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    </bean>
    <bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
    <value>classpath:jdbc.properties</value>
    </property>
    </bean> <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!--<property name="packagesToScan"> <list> <value>com.pass.bean</value> 
    </list> </property> -->
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
    org.hibernate.dialect.MySQLDialect
    </prop>
    <!-- org.hibernate.dialect.PostgreSQLDialect -->
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.format_sql">true</prop>
    <prop key="hibernate.hbm2ddl.auto">none</prop>
    <prop key="hibernate.use_sql_comments">true</prop>
    <prop key="current_session_context_class">thread</prop>
    <!--<prop key="javax.persistence.validation.mode">none</prop> 
    --></props>
    </property>
    <property name="mappingLocations">
    <list>
    <value>classpath:com/shop/jn/**/*.hbm.xml</value>
    </list>
    </property>
    </bean>
    <bean id="txManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- 事务的注解,如 @Transactional(readOnly=true) <tx:annotation-driven transaction-manager="txManager" 
    /> --><bean id="loginTimesAop" class="com.shop.jn.aop.LoginTimesAop"></bean>
    <aop:config> <aop:pointcut id="userServicePointcut"
    expression="execution(* com.shop.jn.service.*.login(..)) and args(username,passwd)" />
    <aop:aspect id="myAspect" ref="loginTimesAop" > <aop:before pointcut-ref="userServicePointcut" method="monitor"
    arg-names="username,passwd" />
    </aop:aspect>
    </aop:config>
    <aop:config>
    <aop:pointcut id="bussinessService"
    expression="execution(public 
    * com.shop.jn.dao..*.*(..))" />
    <aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" />
    </aop:config>
    <tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
    <tx:method name="get*" read-only="true" />
    <tx:method name="test*" read-only="true" />
    <tx:method name="is*" read-only="true" />
    <tx:method name="show*" read-only="true" />
    <tx:method name="delete*" propagation="REQUIRED" />
    <tx:method name="update*" propagation="REQUIRED" />
    <tx:method name="save*" propagation="REQUIRED" />
    <tx:method name="add*" propagation="REQUIRED" />
    <tx:method name="add*" propagation="REQUIRED" />
    <tx:method name="set*" propagation="REQUIRED" />
    <tx:method name="verify*" propagation="REQUIRED" />
    </tx:attributes>
    </tx:advice>
    <!-- aop -->

    <!-- dao -->
    <bean id="goodsDao" class="com.shop.jn.dao.GoodsDao">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <bean id="superetDao" class="com.shop.jn.dao.SuperetDao">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <bean id="userDao" class="com.shop.jn.dao.UserDao">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <bean id="userService" class="com.shop.jn.service.UserService">
    <property name="userDao" ref="userDao"></property>
    </bean><bean id="addInputGoodsAction" class="com.shop.jn.action.goods.AddInputGoodsAction"
    scope="prototype">
    <property name="superetDao" ref="superetDao"></property>
    </bean>
    <bean id="addSaveGoodsAction" class="com.shop.jn.action.goods.AddSaveGoodsAction"
    scope="prototype">
    <property name="goodsDao" ref="goodsDao"></property>
    <property name="superetDao" ref="superetDao"></property>
    </bean> <bean id="viewGoodsAction" class="com.shop.jn.action.goods.ViewGoodsAction"
    scope="prototype">
    <property name="goodsDao" ref="goodsDao"></property>
    </bean>
    <bean id="deleteGoodsAction" class="com.shop.jn.action.goods.DeleteGoodsAction"
    scope="prototype">
    <property name="goodsDao" ref="goodsDao"></property>
    </bean>
    <bean id="updateInputGoodsAction" class="com.shop.jn.action.goods.UpdateInputGoodsAction"
    scope="prototype">
    <property name="goodsDao" ref="goodsDao"></property>
    <property name="superetDao" ref="superetDao"></property>
    </bean>
    <bean id="getOneGoodsAction" class="com.shop.jn.action.goods.GetOneGoodsAction"
    scope="prototype">
    <property name="goodsDao" ref="goodsDao"></property>
    <!--<property name="superetDao" ref="superetDao"></property>
    --></bean>
    <bean id="updateSaveGoodsAction" class="com.shop.jn.action.goods.UpdateSaveGoodsAction"
    scope="prototype">
    <property name="goodsDao" ref="goodsDao"></property>
    <property name="superetDao" ref="superetDao"></property>
    </bean>
    <!-- superet -->
    <bean id="addSuperetAction" class="com.shop.jn.action.superet.AddSuperetAction"
    scope="prototype">
    <property name="superetDao" ref="superetDao"></property>
    </bean>
    <bean id="viewSuperetAction" class="com.shop.jn.action.superet.ViewSuperetAction"
    scope="prototype">
    <property name="superetDao" ref="superetDao"></property>
    </bean>
    <bean id="deleteSuperetAction" class="com.shop.jn.action.superet.DeleteSuperetAction"
    scope="prototype">
    <property name="superetDao" ref="superetDao"></property>
    </bean>
    <bean id="updateInputSuperetAction"
    class="com.shop.jn.action.superet.UpdateInputSuperetAction"
    scope="prototype">
    <property name="superetDao" ref="superetDao"></property>
    </bean>
    <bean id="updateSaveSuperetAction"
    class="com.shop.jn.action.superet.UpdateSaveSuperetAction"
    scope="prototype">
    <property name="superetDao" ref="superetDao"></property>
    </bean> <!-- user below -->
    <bean id="userLoginAction" class="com.shop.jn.action.user.UserLoginAction"
    scope="prototype">
    <property name="userService" ref="userService"></property>
    </bean>
    <bean id="loginInputAction" class="com.shop.jn.action.user.LoginInputAction"
    scope="prototype">
    <property name="userService" ref="userService"></property>
    </bean>
    <bean id="logOutAction" class="com.shop.jn.action.user.UserLogOutAction"
    scope="prototype">
    </bean>

    </beans>
      

  2.   

    终于找到原因了,请参阅:
    http://hw1287789687.iteye.com/blog/1882540
    http://hw1287789687.iteye.com/admin/blogs/1882291