My homecontroller has a UserService object that gets wired using spring correctly (it renders the index page just fine using a method no UserService).
Now I setup hibernate, so inside UserService I have a UserDao object that I am trying to wire using spring.
@Service
public class UserServiceImpl implements UserService{
UserDao userDao;
public String sayHello() {
return "hello from user service impl part 2";
}
public String getTestUser() {
return userDao.getById(1L).getUsername();
}
}
So my HomeController was calling the 'sayHello' method and it was working fine like I said.
@Controller
public class HomeController {
@Autowired
private UserService userService;
@RequestMapping("/")
public ModelAndView Index() {
ModelAndView mav = new ModelAndView();
mav.setViewName("index");
mav.addObject("message", userService.sayHello());
mav.addObject("username", userService.getTestUser());
//userService.getTestUser();
return mav;
}
The call to userService.getTestUser() fails, as the UserDao is null.
My app-config.xml is:
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--<property name="packagesToScan" value="com.blah.core.db.hibernate"/> -->
<property name="configLocation" value="/WEB-INF/classes/hibernate.cfg.xml"/>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.connection.url=jdbc:mysql://localhost/blah
hibernate.connection.username=dbuser
hibernate.connection.password=123
hibernate.query.substitutions=true 'Y', false 'N'
hibernate.cache.use_query_cache=true
hibernate.cache.use_second_level_cache=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.jdbc.batch_size=0
</value>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="userDao" class="com.blah.core.db.hibernate.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
Why is my UserDao null? I must be doing something wrong with the wiring?
Also, if I uncomment out the name=packagesToScan line, do I really need to define a bean for each Dao like I did with UserDao? Will the sessionFactory get wired somehow?