autowire的名字不工作春天MVC

时间:2016-06-02 11:59:43

标签: java spring spring-mvc autowired applicationcontext

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>

my 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并且坚持这个。

2 个答案:

答案 0 :(得分:0)

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

中的元素

答案 1 :(得分:0)

请更新您的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>

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