javax.servlet.ServletException:在类路径资源中定义名为'userService'的bean时出错

时间:2012-02-02 22:08:38

标签: java mysql spring java-ee struts

javax.servlet.ServletException: Error creating bean with name 'userService' defined in class path resource [applicationContext.xml]:
Cannot resolve reference to bean 'helloDao' while setting bean property 'helloDao';
nested exception is
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloDao' defined in class path resource [applicationContext.xml]: Initialization of bean failed;
nested exception is
    org.springframework.beans.InvalidPropertyException: Invalid property 'jdbcTemplate' of bean class [dao.HelloDaoImpl]: No property 'jdbcTemplate' found

我一次又一次地得到这个例外但是找不到原因.. 我正在附上我必要的文件,以使其更容易理解..

[web.xml中]

<web-app>
    <display-name>helloWorld</display-name>

    <listener>
        <listener-class>
            utils.listener.HelloContextListener
        </listener-class>
    </listener>

    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>
            org.apache.struts.action.ActionServlet
        </servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Struts Action Servlet Mapping -->
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.me</url-pattern>
    </servlet-mapping>


    <welcome-file-list>
        <welcome-file>/jsps/homepage.jsp</welcome-file>
    </welcome-file-list>
</web-app>

[struts-config.xml中]

<struts-config>

    <form-beans>
        <form-bean name="formBean" type="ui.forms.UserRegisterForm"></form-bean>
    </form-beans>

    <global-forwards>
        <forward name="home" path="/jsps/homepage.jsp"></forward>
    </global-forwards>

    <action-mappings>
        <action path="/register" type="ui.actions.UserRegisterAction" name="formBean" validate="true" input="/jsps/homepage.jsp">
            <forward name="success" path="/jsps/infoadded.jsp"></forward>
            <forward name="error" path="/jsps/underConstruction.jsp"></forward>
            <forward name="error2" path="/jsps/userinfo.jsp"></forward>
    </action>

    <action path="/home" forward="/jsps/homepage.jsp"></action>
    </action-mappings>
</struts-config>

[applicationContext.xml中]

<beans>

    <bean id="userService" class="service.UserServiceImpl" init-method="initialize">
        <property name="helloDao">
            <ref bean="helloDao"/>
        </property>
    </bean>

    <bean id="helloDao" class="dao.HelloDaoImpl">
        <property name="jdbcTemplate">
            <ref bean="jdbcTemplate"/>
        </property>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg>
            <ref bean="dataSource"/>
        </constructor-arg>
    </bean>

     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver" />
      <property name="url" value="jdbc:mysql://localhost:3306/helloworld" />
      <property name="username" value="root" />
      <property name="password" value="" />
     </bean>


</beans>

[homepage.jsp]

<body>
    <form action="register.me" method="post">
    <pre>
    Name        :   <input type="text" name="userName" >
    Password    :   <input type="text" name="userPassword" >
                    <input type="submit" value="Click to Register">
    </pre>
    </form>
</body>

[所有java类]

public class UserRegisterAction extends Action {
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        RegisterUser user=new RegisterUser();
        UserRegisterForm form2=(UserRegisterForm)form;
        user.setM_userName(form2.getM_userName());
        user.setM_userPassword(form2.getM_userPassword());

        int i= ((IUserService)HelloServiceFactory.getUserServices()).addNewUser(user);
        if(i==1){
            return mapping.findForward("success");
        }
        else {
            request.setAttribute("Output", "Wrong Credentials");
            return mapping.findForward("error");
        }
        }
}

public class HelloContextListener implements ServletContextListener {

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {

        ClassPathResource classPathResource=new ClassPathResource("applicationContext.xml");
        XmlBeanFactory beanFactory=new XmlBeanFactory(classPathResource);
        HelloServiceFactory.setBeanFactory(beanFactory);
    }

}

public class HelloServiceFactory {

    private static XmlBeanFactory beanFactory;

    public static XmlBeanFactory getBeanFactory() {
        return beanFactory;
    }

    public static void setBeanFactory(XmlBeanFactory beanFactory) {
        HelloServiceFactory.beanFactory = beanFactory;
    }

    public static IUserService getUserServices(){

        return (IUserService)getBeanFactory().getBean("userService");
    }
}

public class UserRegisterForm extends ActionForm {
    /**
     * 
     */

private static final long serialVersionUID = 1L;

private String m_userName;

private String m_userPassword;

public String getM_userName() {
        return m_userName;
    }

public void setM_userName(String m_userName) {
        this.m_userName = m_userName;
    }

public String getM_userPassword() {
        return m_userPassword;
    }

public void setM_userPassword(String m_userPassword) {
        this.m_userPassword = m_userPassword;
    }


}


public class UserServiceImpl implements IUserService {

    private IHelloDao m_dao;
    public IHelloDao getM_dao() {
        return m_dao;
    }
    public void setM_dao(IHelloDao m_dao) {
        this.m_dao = m_dao;
    }
    public void initialize() {

    }

    public int addNewUser(RegisterUser user) throws Exception {
        int stat=0;
        try {
            stat=m_dao.addNewUser(user);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return stat;
    }


}

public interface IUserService {

    public int addNewUser(RegisterUser user)throws Exception;
}

public class RegisterUser {

private static String m_userName;
private static String m_userPassword;
 public String getM_userName() {
        return m_userName;
    }
 public void setM_userName(String m_userName) {
        RegisterUser.m_userName = m_userName;
    }
 public String getM_userPassword() {
        return m_userPassword;
    }
 public void setM_userPassword(String m_userPassword) {
        RegisterUser.m_userPassword = m_userPassword;
    }
}

public class HelloDaoImpl implements IHelloDao{

    private JdbcTemplate template;
    public JdbcTemplate getTemplate() {
        return template;
    }
    public void setTemplate(JdbcTemplate template) {
        this.template = template;
    }

    public int addNewUser(RegisterUser user) throws Exception {
        Object []a={user.getM_userName(),user.getM_userPassword()};
        return template.update("insert into user(uName, uPasswordv) values (?,?)", a);
    }

}

public interface IHelloDao {
    public int addNewUser(RegisterUser user)throws Exception;
}

我在错误页面底部得到的是

org.springframework.beans.InvalidPropertyException: Invalid property 'jdbcTemplate' of bean class [dao.HelloDaoImpl]: No property 'jdbcTemplate' found
对我来说一切都很顺利 但我相信专家的看法总是很好,所以我急切地寻找建议.. 请做一点努力,这可以帮助我很多 提前谢谢......

2 个答案:

答案 0 :(得分:0)

在您的应用程序上下文中,您已定义了以下依赖项。

<bean id="helloDao" class="dao.HelloDaoImpl">
    <property name="jdbcTemplate">
        <ref bean="jdbcTemplate"/>
    </property>
</bean>

您可能缺少jdbcTemplate中字段HelloDaoImpl的设置者。请检查。

答案 1 :(得分:0)

<bean id="helloDao" class="dao.HelloDaoImpl">
        <property name="jdbcTemplate">
            <ref bean="jdbcTemplate"/>
        </property>
    </bean>

您应该在HelloDaoImpl类中设置相同的属性 <而不是

private JdbcTemplate template;

并为其重制你的getter / setter。