我正在我的应用程序中使用Hibernate 4进行多租户实现,同时构建应用程序,但我在Tomcat的 Catalina 中遇到以下错误。
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field:
protected org.hibernate.SessionFactory com.boxco.blip.dao.BaseDao.sessionFactory;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext-persistence.xml]:
Invocation of init method failed; nested exception is java.lang.NullPointerException
下面是我的应用程序-persistance.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="...">
<context:annotation-config />
<tx:annotation-driven />
<context:component-scan base-package="com.boxco.blip">
</context:component-scan>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>jdbc/blip</value>
</property>
<property name="resourceRef">
<value>true</value>
</property>
</bean>
<!-- Hibernate Session Factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<value>com.boxco.blip.model.Address</value>
</list>
</property>
<property name="hibernateProperties">
<map>
<entry key="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<entry key="hibernate.hbm2ddl.auto" value="validate" />
<entry key="hibernate.show_sql" value="true" />
<entry key="hibernate.connection.autocommit" value="false" />
<entry key="hibernate.jdbc.batch_size" value="0" />
<entry key="hibernate.connection.release_mode" value="after_transaction" />
<entry key="hibernate.multiTenancy" value="SCHEMA" />
<entry key="hibernate.tenant_identifier_resolver" value-ref="multiTenantIdentifierResolver1" />
<entry key="hibernate.multi_tenant_connection_provider" value-ref="multiTenantConnectionProvider1" />
</map>
</property>
</bean>
<bean id="multiTenantConnectionProvider1" class="com.boxco.blip.web.util.SchemaMultiTenantConnectionProviderBlip">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="multiTenantIdentifierResolver1" class="com.boxco.blip.web.util.SchemaCurrentTenantIdentifierResolverBlip" />
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
<property name="dataSource" ref="dataSource"/>
</bean>
我的AddressDao.java文件
package com.boxco.blip.dao;
import com.boxco.blip.model.Address;
public interface AddressDao {
public Address getAddressByAddressId(int addressId);
}
我的AddressDaoImpl.java文件
package com.boxco.blip.dao;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import com.boxco.blip.model.Address;
@Component
@Repository(value = "addressDao")
public class AddressDaoImpl extends BaseDao implements AddressDao {
private static final Logger logger = Logger.getLogger(AddressDaoImpl.class);
@Override
public Address getAddressByAddressId(int addressId) {
logger.info("Entering getAddressByAddressId()........");
Address address = null;
Criteria criteria = getSession().createCriteria(Address.class);
criteria.add(Restrictions.eq("addressId", addressId));
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
address = (Address)criteria.uniqueResult();
logger.info("Exiting getAddressByAddressId()..........");
return address;
}
}
初始化sessionFactory对象的基本Dao文件:
package com.boxco.blip.dao;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.HibernateTemplate;
import com.mysql.jdbc.Connection;
public abstract class BaseDao
{
@Autowired
@Qualifier("sessionFactory")
protected SessionFactory sessionFactory;
protected void init(SessionFactory factory) {
try {
setSessionFactory(factory);
} catch (Exception e) {
e.printStackTrace();
}
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public Session getSession() throws HibernateException {
// return sessionFactory.getCurrentSession();
return sessionFactory.withOptions().tenantIdentifier("BLIP1314ERP")
.openSession();
}
//some more methods
}
我的Dispatcher-servlet.xml文件,其中添加了
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="...">
<mvc:annotation-driven/>
<context:component-scan base-package="com.boxco">
<!-- <context:exclude-filter type="regex" expression="com.boxco.blip.dao.*" /> -->
</context:component-scan>
<bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="basenames" value="views"/>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<value>messages,error_codes</value>
</property>
</bean>
<bean id="validatorFactory"
class="org.springmodules.validation.commons.DefaultValidatorFactory">
<property name="validationConfigLocations">
<list>
<value>/WEB-INF/validator-rules.xml</value>
<value>/WEB-INF/validator.xml</value>
<value>/WEB-INF/validatorFas.xml</value>
<value>/WEB-INF/validatorFas-rules.xml</value>
</list>
</property>
</bean>
<bean id="validator" class="org.springmodules.validation.commons.DefaultBeanValidator">
<property name="validatorFactory" ref="validatorFactory"/>
</bean>
错误是在我的SessionFactory
初始化,但为什么它没有得到init是我的问题..?(我第一次处理这个错误)..
任何建议都很感谢。
2条答案
按热度按时间zfciruhq1#
这不应该是你的解决方案,在任何情况下,@repository上的@component注解都是无用的。要么插入@repository(更适合异常转换),要么插入@component。
n53p2ov02#
我通过将我的JAR文件从4.3.4替换为4.1.7(降级了我的jar)Hibernate版本来解决这个问题,我不知道为什么它不能与最新版本的Hibernate一起工作