Reputation: 6233
Why is my spring bean (loginInfoDAO) which is injected properly on server start up, null when I post my form to the controller? I have stepped through the setters on start up and they are injecting properly. However, I run the get method and then the post method and the values that were injected are null. Why would this happen?
Controller
@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;
}
}
application-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>
spring-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>
Upvotes: 3
Views: 599
Reputation: 20810
Add @Autowired for loginInfoDAO since you are using context:annotation-config
.
@Autowired
private LoginInfoDAO loginInfoDAO;
Then remove the following entries from your context xml (since they are driven with the annotation here).
<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>
This should get you going.
Upvotes: 2