我想将Spring与Hibernate集成。我是这两种技术的新手。目前我收到这样的错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public service.UserService controller.HomeController.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [service.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
有人可以帮助我吗?
package controller;
@Controller
public class HomeController {
@Autowired
private UserService userService;
@RequestMapping("/register")
public ModelAndView getRegisterForm(@ModelAttribute("user") User user,
BindingResult result) {
ArrayList<String> gender = new ArrayList<String>();
gender.add("Male");
gender.add("Female");
ArrayList<String> city = new ArrayList<String>();
city.add("Delhi");
city.add("Kolkata");
city.add("Chennai");
city.add("Bangalore");
Map<String, Object> model = new HashMap<String, Object>();
model.put("gender", gender);
model.put("city", city);
System.out.println("Register Form");
return new ModelAndView("Register", "model", model);
}
@RequestMapping("/saveUser")
public ModelAndView saveUserData(@ModelAttribute("user") User user,
BindingResult result) {
userService.addUser(user);
System.out.println("Save User Data");
return new ModelAndView("redirect:/userList.html");
}
@RequestMapping("/userList")
public ModelAndView getUserList() {
Map<String, Object> model = new HashMap<String, Object>();
model.put("user", userService.getUser());
return new ModelAndView("UserDetails", model);
}
}
package service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserDao userDao;
@Override
public void addUser(User user) {
userDao.saveUser(user);
}
@Override
public List<User> getUser() {
return userDao.getUser();
}
}
spring-servlet代码: 你需要一个Spring,Hibernate集成示例用于我的新项目。所以我从网站上复制了这个。我做了一些修改..它不起作用请帮帮我
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:property-placeholder location="classpath:jdbc.properties" />
<context:component-scan base-package="controller" />
<tx:annotation-driven/>
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean>
<bean id="myydao" class="dao.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean name="/user/*.htm" class="controller.HomeController" >
<property name="UserDao" ref="mydao" />
</bean>
<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
答案 0 :(得分:1)
我假设您尚未将bean添加到Spring Application Context
如果您使用的是xml,则需要在xml文件中添加以下内容:
<bean class="service.UserServiceImpl" id="userService"/>
或者,如果您使用的是基于Java的配置类,则需要添加如下所示的bean:
@Configuration
public class AppConfig{
@Bean
public UserService userService() {
return new UserServiceImpl();
}
}
或者,如果将@Component
注释添加到UserServiceImpl和@ComponentScan("service")
注释到Java App配置,它应该选择用户服务
答案 1 :(得分:0)
确保已启用组件扫描以自动检测类并注册相应的bean。 您应该在bean定义XML文件中添加以下条目:
<context:component-scan base-package="org.example"/>