最近在看spring AOP
做测试发现下面一个问题 
这是dao@Component("userDao")
public class UserDaoImpl implements UserDao { public void addUser(User user) {
// TODO Auto-generated method stub
System.out.println("this is add method!");
} public void delUser() {
// TODO Auto-generated method stub
System.out.println("this is delete method!");
}}--------------用的是annotation注入-----------------------
下面是service代码 注入userdao
  @Component("service.userService")
public class UserService { public UserDao userDao;
public UserDao getUserDao() {
return userDao;
}

@Resource
public void setUserDao( UserDao userDao) {
this.userDao = userDao;
}


public void addUser(User user){
   
this.userDao.addUser(user);

}

public void delUser(){
this.userDao.delUser();
}
}-----------------------
这是我的Test类
   public class Test {

public static void main(String[] args){
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService us=(UserService) app.getBean("service.userService");
us.addUser(new User());
us.delUser();
}}
----------切面----------@Aspect
@Component
public class LogAop {

@Before("execution( public void com.xiao.springsimulate.dao.UserDao.addUser(com.xiao.springsimulate.model.User))")
public void before(){
System.out.println(" i am befor the add method");
}}运行应该会打出
i am befor the add method
his is add method!
this is delete method!可以不会打印第一句 只打印后面两句不知道什么原因 
这是我的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"
   xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
default-lazy-init="true"> <!-- aop的配置 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!--  autowrired配置 -->
<context:annotation-config></context:annotation-config>
   <!-- @commpont @resource配置-->
    <context:component-scan base-package="com.xiao.springsimulate"></context:component-scan>
</beans>以及后台输出的信息
2010-7-12 10:52:03 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@ecd7e: display name [org.springframework.context.support.ClassPathXmlApplicationContext@ecd7e]; startup date [Mon Jul 12 10:52:03 CST 2010]; root of context hierarchy
2010-7-12 10:52:04 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
2010-7-12 10:52:04 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@ecd7e]: org.springframework.beans.factory.support.DefaultListableBeanFactory@766a24
2010-7-12 10:52:04 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@766a24: defining beans [org.springframework.aop.config.internalAutoProxyCreator,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,userDao,service.userService]; root of factory hierarchy
this is add method!
this is delete method!帮忙看下 谢谢 

解决方案 »

  1.   

    表达式换下看看:
    @Before("execution( public void com.xiao.springsimulate.dao.UserDao.addUser(com.xiao.springsimulate.model.User))")
    改为:
    @Before("execution( public void *..dao.UserDao.addUser(..))")
      

  2.   

    XML 里的 
    <!--  autowrired配置 -->
     <context:annotation-config></context:annotation-config>需要配置下吧? 
      

  3.   

    注解如果不行的话,可以配置XML来配置试下,另外可以用("execution( public void *..dao.UserDao.addUser(..))")这种表达式来使用一下。。