簡體   English   中英

按名稱自動接線在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中的適當更改一起使用時,Spring可以正常工作。

我的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>

盡管我仍然建議您使用注釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM