Spring Bean错误的路径

时间:2016-10-25 10:05:09

标签: java spring jsp spring-mvc

我收到此错误,我无法弄清楚原因。我相信它的道路。试图改变它几次但没有成功。

***************************
APPLICATION FAILED TO START
***************************

Description:

Field loginDelegate in com.codeEvaluator.controller.LoginController required a bean of type 'com.codeEvaluator.delegate.LoginDelegate' that could not be found.


Action:

Consider defining a bean of type 'com.codeEvaluator.delegate.LoginDelegate' in your configuration.


Process finished with exit code 1

这是我的sprinBeanConfiguration.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-3.0.xsd">



<bean id="loginDelegate" class="com.codeEvaluator.delegate.LoginDelegate">
    <property name="userService" ref="userService"></property>
</bean>

<bean id="userService" class="com.codeEvaluator.service.impl.UserServiceImpl">
    <property name="userDao" ref="userDao"></property>
</bean>

<bean name="userDao" class="com.codeEvaluator.dao.impl.UserDaoImpl">
    <property name="dataSource" ref="dataSource"></property>
</bean>

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/codeevaluator" />
    <property name="username" value="root" />
    <property name="password" value="" />
</bean>

我的springWeb.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-3.0.xsd">

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

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/jsp/" />
  <property name="suffix" value=".jsp" />

<import resource="springBeanConfiguration.xml"/>

控制器类

package com.codeEvaluator.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.codeEvaluator.delegate.LoginDelegate;
import com.codeEvaluator.viewBean.LoginBean;


@Controller
public class LoginController
{
        @Autowired
    private LoginDelegate loginDelegate;

    @RequestMapping(value="/login",method=RequestMethod.GET)
    public ModelAndView displayLogin(HttpServletRequest request, HttpServletResponse response, LoginBean loginBean)
    {
        ModelAndView model = new ModelAndView("login");
        //LoginBean loginBean = new LoginBean();
        model.addObject("loginBean", loginBean);
        return model;
    }
    @RequestMapping(value="/login",method=RequestMethod.POST)
    public ModelAndView executeLogin(HttpServletRequest request, HttpServletResponse response, @ModelAttribute("loginBean")LoginBean loginBean)
    {
            ModelAndView model= null;
            try
            {
                    boolean isValidUser = loginDelegate.isValidUser(loginBean.getUsername(), loginBean.getPassword());
                    if(isValidUser)
                    {
                            System.out.println("User Login Successful");
                            request.setAttribute("loggedInUser", loginBean.getUsername());
                            model = new ModelAndView("welcome");
                    }
                    else
                    {
                            model = new ModelAndView("login");
                            request.setAttribute("message", "Invalid credentials!!");
                    }

            }
            catch(Exception e)
            {
                    e.printStackTrace();
            }

            return model;
    }
}

这是一个项目视图。

enter image description here

login.jsp在de jsp包中。

2 个答案:

答案 0 :(得分:2)

更改为

<context:component-scan base-package="com.jcg, com.codeEvaluator" />

你的导入应该是这样的

<import resource="classpath:springBeanConfiguration.xml"/>

<import resource="classpath*:springBeanConfiguration.xml"/>

答案 1 :(得分:0)

在你的&#34; sprinBeanConfiguration.xml&#34;文件添加注释<context:component-scan base-package="com.codeEvaluator" /><context:annotation-config>以启用注释&#34; @ Autowired&#34;