Java 自动连线依赖项的注入失败;嵌套异常为org.springframework.beans.factory.BeanCreationException:

Java 自动连线依赖项的注入失败;嵌套异常为org.springframework.beans.factory.BeanCreationException:,java,spring,hibernate,spring-mvc,struts,Java,Spring,Hibernate,Spring Mvc,Struts,我正在使用Spring、Hibernate、Struts和Maven创建web应用程序 当我运行mvn clean install命令时,出现以下错误: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.project.action.PasswordHintActionTest': Injection of autowired dependencies faile

我正在使用Spring、Hibernate、Struts和Maven创建web应用程序

当我运行
mvn clean install
命令时,出现以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.project.action.PasswordHintActionTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.project.action.PasswordHintAction com.project.action.PasswordHintActionTest.action; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.project.action.PasswordHintAction] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
以下是具有自动连线依赖项的类:

import com.opensymphony.xwork2.Action;
import org.project.model.User;
import org.proejct.service.UserManager;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.subethamail.wiser.Wiser;

import static org.junit.Assert.*;
public class PasswordHintActionTest extends BaseActionTestCase {
    @Autowired
    private PasswordHintAction action;
    @Autowired
    private UserManager userManager;

    @Test
    public void testExecute() throws Exception {
        // start SMTP Server
        Wiser wiser = new Wiser();
        wiser.setPort(getSmtpPort());
        wiser.start();

        action.setUsername("user");
        assertEquals("success", action.execute());
        assertFalse(action.hasActionErrors());

        // verify an account information e-mail was sent
        wiser.stop();
        assertTrue(wiser.getMessages().size() == 1);

        // verify that success messages are in the request
        assertNotNull(action.getSession().getAttribute("messages"));
    }


}
My
applicationcontext.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"
       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.xsd"
       default-lazy-init="true">

    <!-- Activates scanning of @Autowired -->
    <context:annotation-config/>

    <!-- Activates scanning of @Repository and @Service -->
    <context:component-scan base-package="com.project"/>

    <!-- Compass Search Section -->
    <!-- Compass Bean, automatically scanning for searchable classes within the model -->
    <!-- Hooks into Spring transaction management and stores the index on the file system -->
    <bean id="compass" class="org.compass.spring.LocalCompassBean">
        <property name="mappingScan" value="org.project"/>
        <property name="postProcessor" ref="compassPostProcessor"/>
        <property name="transactionManager" ref="transactionManager" />
        <property name="settings">
            <map>
                <entry key="compass.engine.connection" value="target/test-index" />
            </map>
        </property>
    </bean>

我已添加到我的上下文配置中,以扫描自动连接的依赖项。但我不知道为什么它仍然给这个例外

我也试着用下面的方法添加它,但仍然得到相同的异常

<context:component-scan base-package="com.project.*"/>

更新:

下面是密码提示操作

import org.project.model.User;
import com.project.webapp.util.RequestUtil;
import org.springframework.mail.MailException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.List;


public class PasswordHintAction extends BaseAction {
    private static final long serialVersionUID = -4037514607101222025L;
    private String username;

    /**
     * @param username The username to set.
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * Execute sending the password hint via e-mail.
     *
     * @return success if username works, input if not
     */
    public String execute() {
        List<Object> args = new ArrayList<Object>();

        // ensure that the username has been sent
        if (username == null) {
            log.warn("Username not specified, notifying user that it's a required field.");

            args.add(getText("user.username"));
            addActionError(getText("errors.requiredField", args));
            return INPUT;
        }

        if (log.isDebugEnabled()) {
            log.debug("Processing Password Hint...");
        }

        // look up the user's information
        try {
            User user = userManager.getUserByUsername(username);
            String hint = user.getPasswordHint();

            if (hint == null || hint.trim().equals("")) {
                log.warn("User '" + username + "' found, but no password hint exists.");
                addActionError(getText("login.passwordHint.missing"));
                return INPUT;
            }

            StringBuffer msg = new StringBuffer();
            msg.append("Your password hint is: ").append(hint);
            msg.append("\n\nLogin at: ").append(RequestUtil.getAppURL(getRequest()));

            mailMessage.setTo(user.getEmail());
            String subject = '[' + getText("webapp.name") + "] " + getText("user.passwordHint");
            mailMessage.setSubject(subject);
            mailMessage.setText(msg.toString());
            mailEngine.send(mailMessage);

            args.add(username);
            args.add(user.getEmail());

            saveMessage(getText("login.passwordHint.sent", args));
        } catch (UsernameNotFoundException e) {
            log.warn(e.getMessage());
            args.add(username);
            addActionError(getText("login.passwordHint.error", args));
            getSession().setAttribute("errors", getActionErrors());
            return INPUT;
        } catch (MailException me) {
            addActionError(me.getCause().getLocalizedMessage());
            getSession().setAttribute("errors", getActionErrors());
            return INPUT;
        }

        return SUCCESS;
    }
}
导入org.project.model.User;
导入com.project.webapp.util.RequestUtil;
导入org.springframework.mail.MailException;
导入org.springframework.security.core.userdetails.UsernameNotFoundException;
导入java.util.ArrayList;
导入java.util.List;
公共类PasswordHintAction扩展了BaseAction{
私有静态最终长serialVersionUID=-403751460710222025L;
私有字符串用户名;
/**
*@param username要设置的用户名。
*/
public void setUsername(字符串用户名){
this.username=用户名;
}
/**
*执行通过电子邮件发送密码提示。
*
*@如果用户名有效则返回成功,如果无效则输入
*/
公共字符串execute(){
List args=new ArrayList();
//确保已发送用户名
如果(用户名==null){
警告(“未指定用户名,通知用户它是必填字段。”);
args.add(getText(“user.username”);
addActionError(getText(“errors.requiredField”,args));
返回输入;
}
if(log.isDebugEnabled()){
调试(“正在处理密码提示…”);
}
//查找用户的信息
试一试{
User=userManager.getUserByUsername(用户名);
字符串提示=user.getPasswordHint();
if(hint==null | | hint.trim().equals(“”){
log.warn(“找到用户“+”用户名“+”,但不存在密码提示。”);
addActionError(getText(“login.passwordHint.missing”);
返回输入;
}
StringBuffer msg=新的StringBuffer();
msg.append(“您的密码提示是:”).append(提示);
msg.append(“\n\n登录:”).append(RequestUtil.getAppURL(getRequest());
mailMessage.setTo(user.getEmail());
字符串subject='['+getText(“webapp.name”)+“]”+getText(“user.passwordHint”);
mailMessage.setSubject(主题);
mailMessage.setText(msg.toString());
mailnengine.send(mailMessage);
args.add(用户名);
args.add(user.getEmail());
saveMessage(getText(“login.passwordHint.sent”,args));
}catch(UsernameNotFounde异常){
log.warn(如getMessage());
args.add(用户名);
addActionError(getText(“login.passwordHint.error”,args));
getSession().setAttribute(“错误”,getActionErrors());
返回输入;
}捕获(邮件例外){
addActionError(me.getCause().getLocalizedMessage());
getSession().setAttribute(“错误”,getActionErrors());
返回输入;
}
回归成功;
}
}
更新2:

applicationContext-struts.xml:

<bean id="passwordHintAction" class="com.project.action.PasswordHintAction" scope="prototype">
    <property name="userManager" ref="userManager"/>
    <property name="mailEngine" ref="mailEngine"/>
    <property name="mailMessage" ref="mailMessage"/>
</bean>

您需要为autowire提供一个候选者。这意味着必须知道PasswordHint的一个实例是以一种可以猜测它必须引用它的方式出现的

请提供PasswordHint的类头和/或该类的Springbean定义以获得进一步帮助

尝试更改的名称

PasswordHintAction action;


如果
com.project.action.PasswordHintAction
使用原型注释,则使用下面给出的组件扫描

<context:component-scan base-package="com.project.action"/>
或者在
applicationcontext.xml
中创建xml配置,如

@Component
public class PasswordHintAction extends BaseAction {
    private static final long serialVersionUID = -4037514607101222025L;
    private String username;
<bean id="passwordHintAction" class="com.project.action.PasswordHintAction" />

在bean.xml文件或任何其他配置文件中添加bean声明。它将解决错误

<bean  class="com.demo.dao.RailwayDao"></bean>
<bean  class="com.demo.service.RailwayService"></bean>
<bean  class="com.demo.model.RailwayReservation"></bean>


我只看到上下文文件的一部分,它不包含任何类型的beanPasswordHintAction@Klits:我编辑了我的答案,看一看。@Kltis:你能在这里添加你的web.xml代码吗。。感谢您,事实上,我已经使用了一个单独的配置文件,名为applicationContext-struts.xml。我已使用此更新更新了我的问题。但是,即使这样,我还是得到了这个错误谢谢你的回答,我还没有将applicationContext-struts.xml链接到我的web.xml文件中。这就是问题所在。无论如何,您的回答帮助我进入了这个解决方案。感谢您的努力,无论如何,问题是我没有添加ApplicationContext-strtus.xml,它在上下文参数下具有链接到web.xml文件的passwordhintaction bean定义。
<bean  class="com.demo.dao.RailwayDao"></bean>
<bean  class="com.demo.service.RailwayService"></bean>
<bean  class="com.demo.model.RailwayReservation"></bean>