我正在使用 Spring 3.2 和 Hibernate,但我发现它不允许从 XML 文件创建 SessionFacotry bean 我不知道为什么会出现这个错误。我搜索了很多,但没有找到 exceat ans 请帮助。
这里我的文件是 XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<!-- For context suppport of the Controller -->
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.book.controller"></context:component-scan>
<!-- For enable the MVC Support -->
<!-- <mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/"></mvc:resources>
-->
<!-- Enabling the suppot the UI user side -->
<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>
<!-- For messages for the Labels -->
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<!-- For the Properties of JDBC -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<!--Database Properties -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />
<!-- Session facotry configration -->
<bean id="hibernate4AnnotatedSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- <property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property> -->
<property name="packagesToScan" value="com.book.bookentity" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- implementation bean config. -->
<bean id="autherMasterServiceInterfacecontroller" class="com.book.service.AutherMasterServiceInterfaceImpl"></bean>
<bean id="bookMasterServiceInterfacecontroller" class="com.book.service.BookMasterServiceInterfaceImpl"></bean>
<bean id="emailMasterInterfacecontroller" class="com.book.service.EmailMasterInterfaceImpl"></bean>
<bean id="languageMasterinterfacecontroller" class="com.book.service.LanguageMasterinterfaceimpl"></bean>
<bean id="bookMasterInterface" class="com.book.bookDao.BookMasterInterfaceImpl">
<property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</bean>
<bean id="emailMasterInterface" class="com.book.bookDao.EmailMasterInterfaceImpl">
<property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</bean>
<bean id="languageMasterInterface" class="com.book.bookDao.LanguageMasterInterfaceImpl">
<property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</bean>
<bean id="autherMasterInterface" class="com.book.bookDao.AutherMasterInterfaceImpl">
<property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</bean>
<!-- Enabling transction managar -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Config Tranaction Managar -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</bean>
</beans>
豆类1
@Entity
@Table(name = "tbl_bookmaster", uniqueConstraints = { @UniqueConstraint(columnNames = "bid") })
public class BookMasterTable {
@Id
@GeneratedValue
@Column(name = "bid")
private int bookid;
@Column(name = "bookname")
private String bookname;
@Column(name = "booktype")
private String booktype;
@Column(name = "bookprice")
private String bookprice;
@Column(name = "bestsaler")
private int bestsaller;
@ManyToOne
@JoinColumn(name = "lid")
private LanguageMasterTable languageMasterTable;
@ManyToOne
@JoinColumn(name = "auid")
private AutherMasterTable autherMasterTable;
@ManyToOne
@JoinColumn(name = "mailid")
private EmailMasterTable emailMasterTable;
// getter and setter
}
豆2
@Entity
@Table(name = "tbl_contactmaster")
public class EmailMasterTable implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@OneToMany
@JoinColumn(name = "authercontactmail")
private Set<BookMasterTable> bookMasterTables;
@Column(name = "emailcontact")
private String emailcontact;
}
豆 3
@Entity
@Table(name = "tbl_authermaster")
public class AutherMasterTable {
@Id
@GeneratedValue
@OneToMany
@JoinColumn(name = "auid")
private Set<BookMasterTable> bookMasterTables;
@Column(name = "authername")
private String authername;
// getter and setter
}
豆 4
public class LanguageMasterTable {
@Id
@GeneratedValue
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "language")
private Set<BookMasterTable> bookMasterTables;
@Column(name = "languagename")
private String languagename;
// getter and setter
}
控制器豆
@Controller
public class BookMasterController {
@Autowired
private BookMasterServiceInterface bookMasterServiceInterfacecontroller;
@RequestMapping(value="/addbook",method= RequestMethod.POST)
public String addAuther(@ModelAttribute("book") BookMasterTable bookMasterTable){
bookMasterServiceInterfacecontroller.addBook(bookMasterTable);
return "bookadded";
}
}
就像每个 bean 类都有所有控制器
服务接口
public interface BookMasterServiceInterface {
public void addBook(BookMasterTable bookMasterTable);
}
服务类
@Service
public class BookMasterServiceInterfaceImpl implements BookMasterServiceInterface{
@Autowired
private BookMasterInterface bookMasterInterface;
@Override
@Transactional
public void addBook(BookMasterTable bookMasterTable) {
// TODO Auto-generated method stub
bookMasterInterface.addBooks(bookMasterTable);
}
}
就像为所有 bean 服务一样
存储库类接口
public interface BookMasterInterface {
public void addBooks(BookMasterTable bookMasterTable);
}
具有每个 bean 的存储库类实现
@Repository
public class BookMasterInterfaceImpl implements BookMasterInterface {
@Autowired
private SessionFactory sessionFactory;
@Override
public void addBooks(BookMasterTable bookMasterTable) {
// TODO Auto-generated method stub
try {
sessionFactory.getCurrentSession().save(bookMasterTable);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
我得到的错误是。
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.book.service.BookMasterServiceInterface com.book.controller.BookMasterController.bookMasterServiceInterfacecontroller; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookMasterServiceInterfacecontroller': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.book.bookDao.BookMasterInterface com.book.service.BookMasterServiceInterfaceImpl.bookMasterInterface; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookMasterInterface': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.book.bookDao.BookMasterInterfaceImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/BookServletmvc-servlet.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:517)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
... 20 more
主要是不允许创建,因为它得到空指针,但我不知道这里在哪里。
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/BookServletmvc-servlet.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1512)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:912)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:855)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489)
... 48 more
Caused by: java.lang.NullPointerException
at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:538)
at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:126)
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:116)
at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1514)
at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1437)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1355)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1724)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1775)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:247)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:373)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:358)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509)
... 58 more
如果你能提供帮助,我会很高兴:)
我有四个实体。第一个是
@Entity
@Table(name = "tbl_bookmaster", uniqueConstraints = { @UniqueConstraint(columnNames = "bid") })
public class BookMasterTable {
@Id
@GeneratedValue
@Column(name = "bid")
private int bookid;
@Column(name = "bookname")
private String bookname;
@Column(name = "booktype")
private String booktype;
@Column(name = "bookprice")
private String bookprice;
@Column(name = "bestsaler")
private int bestsaller;
@ManyToOne
@JoinColumn(name = "lid")
private LanguageMasterTable languageMasterTable;
@ManyToOne
@JoinColumn(name = "auid")
private AutherMasterTable autherMasterTable;
@ManyToOne
@JoinColumn(name = "mailid")
private EmailMasterTable emailMasterTable;
public int getBookid() {
return bookid;
}
public void setBookid(int bookid) {
this.bookid = bookid;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getBooktype() {
return booktype;
}
public void setBooktype(String booktype) {
this.booktype = booktype;
}
public String getBookprice() {
return bookprice;
}
public void setBookprice(String bookprice) {
this.bookprice = bookprice;
}
public int getBestsaller() {
return bestsaller;
}
public void setBestsaller(int bestsaller) {
this.bestsaller = bestsaller;
}
public LanguageMasterTable getLanguageMasterTable() {
return languageMasterTable;
}
public void setLanguageMasterTable(LanguageMasterTable languageMasterTable) {
this.languageMasterTable = languageMasterTable;
}
public AutherMasterTable getAutherMasterTable() {
return autherMasterTable;
}
public void setAutherMasterTable(AutherMasterTable autherMasterTable) {
this.autherMasterTable = autherMasterTable;
}
public EmailMasterTable getEmailMasterTable() {
return emailMasterTable;
}
public void setEmailMasterTable(EmailMasterTable emailMasterTable) {
this.emailMasterTable = emailMasterTable;
}
}