按名称自动装配不起作用 spring MVC

autowire by name not working spring MVC

web.xml

     <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <servlet>
        <servlet-name>spring-mvc</servlet-name>
        <servlet-class>
               org.springframework.web.servlet.DispatcherServlet
           </servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>

我的spring-mvc-servlet.xml

     <context:component-scan base-package="org.app.controller" />   
        <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />
        <mvc:annotation-driven />
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix">
                <value>/WEB-INF/pages/</value>
            </property>
            <property name="suffix">
                <value>.jsp</value>
            </property>
        </bean>

我的控制器:仅供参考

        @Controller
    public class HomeController {
        LoginService loginService;
        @RequestMapping(value = "/", method = RequestMethod.GET)
        public String login(Model model) {
            loginService.checkLoginDetails(new LoginDetails("Svn", 1));
            return "login";
        }

AppplicationContext.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!--    <context:annotation-config /> -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="username" value="root"></property>
        <property name="password" value=""></property>
    </bean>
    <bean class="org.app.controller.HomeController" autowire="byName"></bean>
    <bean id="loginDAO" class="org.app.DAOImpl.LoginDAOImpl" autowire="byName"></bean>
    <bean id="loginService" class="org.app.DAOServiceImpl.LoginServiceImpl" autowire="byName"></bean>
    <bean id="employeeDAO" class="org.app.DAOImpl.EmployeeDAOImpl" autowire="byName"></bean>
    <bean id="employeeService" class="org.app.DAOServiceImpl.EmployeeDAOServiceImpl" autowire="byName"></bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="annotatedClasses">
            <list>
                <value>org.app.entity.Employee</value>
                <value>org.app.entity.LoginDetails</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>
    <!-- <context:component-scan base-package="org.infy"> <context:exclude-filter 
        expression="org.springframework.stereotype.Controller" type="annotation" 
        /> </context:component-scan> -->
    <tx:annotation-driven />
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
</beans>

LoginServiceImpl

public class LoginServiceImpl implements LoginService{

    LoginDAO loginDAO;
    @Override
    @Transactional
    public boolean checkLoginDetails(LoginDetails loginDetails) {
        return loginDAO.checkLoginDetails(loginDetails);
    }

}

问题: 我的 LoginDAO 是空的。 Spring 在没有注释的情况下无法使用自动连接实例化它的对象,但是,当我将它与注释一起使用并在 ApplicationContext.xml.

中进行适当的更改时,它工作正常

我的 LoginService bean 正在通过调度程序 servlet 上下文的组件扫描进行实例化。现在,当实例化 LoginServiceImpl spring 时,应该查看 RootApplication 上下文以查找其他 bean 定义,但它不会。 我不明白为什么在使用 @autowire 或启用组件扫描时会发生这种情况。它工作正常那么为什么不没有注释。

我也不确定我要做什么。我在玩 Spring MVC 并卡在了这个上。

在 AppplicationContext.xml 中,您需要将 bean 的注入依赖项指定为

中的元素

请更新您的ApplicationContext.xml

<bean id="loginService" class="org.app.DAOServiceImpl.LoginServiceImpl" autowire="byName">
   <property name="loginDAO" ref="loginDAO"> </property>
</bean>

EmployeeService 和您的控制器也是如此。

<bean class="org.app.controller.HomeController" autowire="byName">
    <property name="loginService" ref="loginService"> </property>
</bean>
<bean id="employeeService"  class="org.app.DAOServiceImpl.EmployeeDAOServiceImpl" autowire="byName">
    <property name="employeeDAO" ref="employeeDAO"> </property>
</bean>

虽然我仍然建议您使用注释。