我想让Spring帮我初始化 Action  我的配置如下 
web.xml
       <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置文件位置指定 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>struts.xml
      <struts>
<constant name="struts.devMode" value="true"></constant>
<constant name="struts.i18n.encoding" value="utf-8" />
<package name="user" extends="struts-default" namespace="/user" >
<action name="user" class="com.ssh.registration.action.UserAction">
<result>/registerSuccess.jsp</result>
<result name="fail">/registerFail.jsp</result>
</action>
  </package>
      </struts>beans.xml
        <context:annotation-config />
<context:component-scan base-package="com.ssh.registration" />
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:jdbc.properties" />
</bean>
<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 id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.ssh.registration.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
java code:
     UserAction:
         @Component("user")
        @Scope("prototype")
       public class UserAction extends ActionSupport {
   UserService userService;
   private String username;
   private String password;     public UserService getUserService() {
return userService;
    }
   @Resource
   public void setUserService(UserService userService) {
this.userService = userService;
    }     public String getUsername() {
return username;
    }     public void setUsername(String username) {
this.username = username;
    }     public String getPassword() {
return password;
    }     public void setPassword(String password) {
this.password = password;
    }     public String add() throws Exception {
User user = new User();
user.setUsername(username);
user.setPassword(password);
System.out.println(userService);
boolean exist = userService.exist(user);
if (exist) {
return "fail";
}
userService.add(user);
return SUCCESS;
      }          }       UserServiceImpl :
             @Component("userService")
            public class UserServiceImpl implements UserService {
  private UserDao userDao;
   
   public UserDao getUserDao() {
return userDao;
  }   @Resource
  public void setUserDao(UserDao userDao) {
this.userDao = userDao;
  }   public boolean exist(User u) {
return userDao.checkUserExistsWithName(u.getUsername());   }   public void add(User u) {
   userDao.save(u);
  }
       }       UserDaoImpl
          @Component("userDao")
          public class UserDaoImpl implements UserDao {
 private HibernateTemplate hibernateTemplate;  public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
 }  @Resource
 public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
 }  public void save(User u) {
hibernateTemplate.save(u);
 }  public boolean checkUserExistsWithName(String username) {
List<User> users = 
hibernateTemplate.find("from User u where u.username='" + username + "'");
if (users != null && users.size() > 0) {
return true;
}
return false;
    }       }        异常:
         Error creating bean with  name 'userService': Injection of resource methods failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique  bean of type [com.ssh.registration.dao.UserDao] is defined: Unsatisfied dependency of type [interface com.ssh.registration.dao.UserDao]: expected at least 1 matching bean        No unique bean of type [com.ssh.registration.dao.UserDao] is defined: Unsatisfied dependency of type [interface com.ssh.registration.dao.UserDao]: expected at least 1 matching bean
         
      所有的步骤都是正确的为什么我的 userService不能注入? 
            
       

解决方案 »

  1.   

    你的userService是不是应该在beans.xml配置一下?
      

  2.   

    beans.xml 中直接添加userService 和userDao 的两个实现类的 bean
      

  3.   

    我上面说错了,  仔细看了下 没发现什么错误 报错是在userService里注入userdao这里  可惜没看出来 我也关注下 。。 
      

  4.   

    我用的是注解 @Component 就已经意味着要把他注入容器