我们正在开发Tapestry5-Spring-Hibernate应用程序
我们使用的是Tapestry 5.4.1,Spring 4.3.1.RELEASE和Hibernate 4.3.6.Final
我们正在使用基于XML的应用程序
我们面临的问题是@Autowired到spring服务中的所有daos总是为null。每当需要dao来对数据库执行操作时,这当然会生成NullpointerException
我们使用通用服务和dao接口
以下是代码示例:
Spring配置文件
<context:annotation-config />
<context:component-scan base-package="org.prism.forecast" />
<context:property-placeholder location="classpath:hibernate.properties" />
<!--Generic DAO -->
<bean id="appDao" primary="true" class="org.prism.forecast.dao.AppDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!--Generic service-->
<bean id="appServiceImpl" class="org.prism.forecast.services.AppServiceImpl">
<property name="dao">
<ref bean="appDao" />
</property>
</bean>
<bean id="requestDao" class="org.prism.forecast.dao.RequestDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="requestServiceImpl" class="org.prism.forecast.services.RequestServiceImpl">
<property name="dao">
<ref bean="requestDao" />
</property>
</bean>
<bean id="SOPUploadDao" class="org.prism.forecast.dao.SOPUploadDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="SOPUploadServiceImpl" class="org.prism.forecast.services.SOPUploadServiceImpl">
<property name="dao">
<ref bean="SOPUploadDao" />
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="org.prism.forecast.entities" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="txManager"/>
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
通用DAO接口
public interface AppDao {
/**
* Finds an object by id
*
* @param <T>
* @param <PK>
* @param type
* @param id
* @return the object
*/
<T, PK extends Serializable> T find(Class<T> type, PK id);
//other methods
}
通用DAO实施
@Repository("appDao")
public class AppDaoImpl implements AppDao {
protected static final Logger logger = LoggerFactory.getLogger(RequestDaoImpl.class);
@Inject
protected Session session;
@Resource(name = "sessionFactory")
protected SessionFactory sessionFactory;
/**
* @param sessionFactory the sessionFactory to set
*/
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@SuppressWarnings("unchecked")
public <T, PK extends Serializable> T find(Class<T> type, PK id) {
return (T) session.get(type, id);
}
//other methods
}
请求DAO
public interface RequestDao extends AppDao {
}
请求DAO实施
@Repository("requestDao")
public class RequestDaoImpl extends AppDaoImpl implements RequestDao {
}
通用服务
public interface AppService {
/**
* @param <T>
* @param <PK>
* @param type
* @param id
* @return the object
*/
<T, PK extends Serializable> T find(Class<T> type, PK id);
//Other methods
}
通用服务实施
@Service("appService")
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public class AppServiceImpl implements AppService {
@Autowired
private AppDao dao = null;
/**
* @return the dao
*/
public AppDao getDao() {
return dao;
}
/**
* @param dao
* the dao to set
*/
public void setDao(AppDao dao) {
this.dao = dao;
}
@Override
public <T, PK extends Serializable> T find(Class<T> type, PK id) {
return dao.find(type, id);//dao is null here
}
//other methods
}
请求服务
public interface RequestService extends AppService {
}
请求服务实施
@Service("requestService")
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public class RequestServiceImpl extends AppServiceImpl implements RequestService {
@Autowired
private RequestDao dao;
@Override
public Request find(Request request) {
dao.find(request);
}
//other methods
}
Tapestry页面中的用法
public class ManageRequest{
//dao needed for persistence operations
@InjectService("requestService")
protected requestService service;
}
在调试应用程序时,我们发现服务已正确注入页面,但dao尚未注入服务。谁能告诉我这里出了什么问题?
答案 0 :(得分:0)
这看起来像Spring配置问题,可能需要在<context:component-scan ... />
中明确指定DAO服务的子包,即:
<context:component-scan base-package="org.prism.forecast, org.prism.forecast.dao" />
一些提示:@Autowired
service is null和multiple packages in context:component-scan
。