为什么我在服务器上正确注入的spring bean启动,当我将表单发布到控制器时为null?

时间:2011-06-17 05:29:18

标签: java hibernate spring

为什么我在服务器上正确注入的spring bean(loginInfoDAO)启动,当我将表单发布到控制器时为null?我已经在启动时介绍了安装人员,他们正在正确注入。但是,我运行get方法,然后运行post方法,并且注入的值为null。为什么会这样?

控制器

    @Controller
    @RequestMapping("/login")
    public class LoginController extends BaseController{

    private LoginInfoDAO loginInfoDAO;

    public void setLoginInfoDAO(LoginInfoDAO loginInfoDAO) {
    this.loginInfoDAO = loginInfoDAO;
    }

    @RequestMapping(method=RequestMethod.GET)
    public ModelAndView getLogin(@ModelAttribute("user") final User ur) {
        ModelAndView mav = new ModelAndView("/login/login");
        return mav;
    }

        @RequestMapping(method=RequestMethod.POST)
    public ModelAndView login(@ModelAttribute("user") final User ur) {

        loginInfoDAO.login(ur);
        ModelAndView mav = new ModelAndView();
        return mav;
    }

    }

应用context.xml中

        <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <bean id="myDataSource" 
    class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
      <property name="driverClassName">
        <value>com.mysql.jdbc.Driver</value>
      </property>
      <property name="url">
        <value>jdbc:mysql://localhost/databasename</value>
      </property>
      <property name="username">
        <value>databaseusername</value>
      </property>
      <property name="password">
        <value>databasepassword</value>
      </property>
      <!-- Disable the second-level cache  -->
        <!-- Echo all executed SQL to stdout -->
    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" /> 
        <property name="annotatedClasses">
            <list>
            <value>com.projectname.model.LoginInfo</value>
            <value>com.projectname.model.User</value>
            </list>
        </property> 
        <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
        </property>
    </bean>


    <bean id="myLoginInfoDAO" class="com.projectname.dao.LoginInfoDAOImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <bean name="/login" class="com.projectname.controllers.LoginController" >
        <property name="loginInfoDAO" ref="myLoginInfoDAO" />
    </bean>
    </beans>

弹簧servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:annotation-config />
<context:component-scan
    base-package="com.projectname" />
<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames">
        <value>/WEB-INF/messages/messages</value>
    </property>
    <property name="cacheSeconds" value="60" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>
</beans>

1 个答案:

答案 0 :(得分:2)

因为您使用context:annotation-config而为loginInfoDAO添加@Autowired。

@Autowired
private LoginInfoDAO loginInfoDAO;

然后从您的上下文xml中删除以下条目(因为它们是使用此处的注释驱动的)。

   <bean id="myLoginInfoDAO" class="com.projectname.dao.LoginInfoDAOImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <bean name="/login" class="com.projectname.controllers.LoginController" >
        <property name="loginInfoDAO" ref="myLoginInfoDAO" />
    </bean>

这应该让你去。