我在Eclipse IDE中创建了一个用户注册项目,我现在使用hibernate模板进行crud操作使用注释我得到这些会话工厂所需的错误我该怎么办?
我的web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>SpringMvcCrud</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
我的dispatcher-servlet.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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.xsd">
<context:component-scan base-package="controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="username" value="system" />
<property name="password" value="rrr" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="packagesToScan">
<array>
<value>beans</value>
</array>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate"
autowire="constructor" />
<bean id="userservice" class="service.UserServiceImpl" />
<bean id="userdao" class="dao.UserDaoImpl" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="dataSource" ref="dataSource" />
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
MY UserControllerclass
package controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import beans.User;
import service.UserService;
@Controller
public class UserRegistration {
@Autowired
UserService userservice;
@RequestMapping(value="/registration", method=RequestMethod.GET)
public String userdisplay(){
return "registration";
}
@RequestMapping(value="/doregistration", method=RequestMethod.POST)
public ModelAndView doregistration(@ModelAttribute("user") User user, BindingResult result){
ModelAndView mod=null;
if (result.hasErrors()){
mod=new ModelAndView("registration");
return mod;
}
else{
mod=new ModelAndView("success");
userservice.save(user);
return mod;
}
}
}
我的User.java文件
package beans;
import javax.persistence.*;
@Entity
public class User {
@Id
@Column()
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQUENCE1")
@SequenceGenerator(name="SEQUENCE1", sequenceName="User_seq", allocationSize=1)
private String id;
@Column()
private String name;
@Column()
private String password;
public User(){}
public User(String id, String name, String password) {
super();
this.id = id;
this.name = name;
this.password = password;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
}
我的UserDao.java文件
package dao;
import beans.User;
public interface UserDao {
public void save (User user);
}
我的UserDaoImpl文件
package dao;
import org.springframework.beans.factory.annotation.*;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;
import beans.User;
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
HibernateTemplate hibernateTemplate;
@Override
public void save(User user) {
hibernateTemplate.save(user);
}
}
我的UserService文件
package service;
import beans.User;
public interface UserService {
public void save(User user);
}
我的UserServiceImpl
package service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import beans.User;
import dao.UserDao;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserDao userdao;
@Override
public void save(User user) {
userdao.save(user);
}
}
这些是我的服务器日志
1
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userservice': Unsatisfied dependency expressed through field 'userdao': Error creating bean with name 'userdao': Unsatisfied dependency expressed through field 'hibernateTemplate': Error creating bean with name 'hibernateTemplate' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sessionFactory' is required; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sessionFactory' is required; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userdao': Unsatisfied dependency expressed through field 'hibernateTemplate': Error creating bean with name 'hibernateTemplate' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sessionFactory' is required; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sessionFactory' is required
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userdao': Unsatisfied dependency expressed through field 'hibernateTemplate': Error creating bean with name 'hibernateTemplate' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sessionFactory' is required; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sessionFactory' is required
2
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sessionFactory' is required
3
Caused by: java.lang.IllegalArgumentException: Property 'sessionFactory' is required
答案 0 :(得分:0)
HibernateTemplate(doc)需要sessionFactory
。
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
答案 1 :(得分:0)
我认为通过在hibernateTemplate中注入sessionFactory可以解决您的问题。但是你可以完全摆脱hibernateTemplate。尝试以下,对我来说似乎比使用hibernateTemplate
更好UserDaoImpl文件
package dao;
import org.springframework.beans.factory.annotation.*;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import beans.User;
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private SessionFactory sessionFactory;
@Override
public void save(User user) {
sessionFactory.getCurrentSession().save(user);
}
}
UserServiceImpl文件
package service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import beans.User;
import dao.UserDao;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserDao userdao;
@Transactional
@Override
public void save(User user) {
userdao.save(user);
}
}
dispatcher-servlet.xml文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
// Put all the entries in a similar way
//
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>