我在這方面看到很多問題,並且嘗試了許多不同解決方案的許多排列,但都沒有工作。在我的DAO中,對於一個普通的java類,hibernate sessionFactory總是爲空
我有一個道需要一個hibernate sessionfactory做交易。在SpringMVC上下文中,我已經看到它的工作,但包含在java類中的dao是null。有一個在catalina.out中沒有錯誤:
我完全applicationContext.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Scan classpath for annotations (eg: @Service, @Repository etc)-->
<context:annotation-config/>
<context:component-scan base-package="com.shazam.di.*" />
<!-- JNDI Data Source. this works I can get to it independent of spring-->
<bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"
scope="singleton">
<property name="jndiName" value="jdbc/dostudentdb"/>
<property name="resourceRef" value="true"/>
</bean>
<tx:annotation-driven/>
<!-- Hibernate Session Factory -->
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="packagesToScan">
<array>
<value>com.shazam.di.spring.coursemgmt.dao</value>
</array>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">false</prop>
<!--<prop key="hibernate.hbm2ddl.auto">update</prop>-->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
</bean>
<!-- Hibernate Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<!--I've alternated between contructor, properties for getters & setters -->
<!--here, and nothing (just letting it get autowired into the private-->
<!--SessionFactory instance, no effing cigar!!-->
<bean id="studentDAO" class="org.shazam.di.spring.coursemgmt.dao.StudentDAO">
<!--<constructor-arg type="SessionFactory" value="mySessionFactory"/>-->
<property name="insertUserProfile" ref="insertUserProfile"/>
</bean>
</beans>
類爲其DAO而不是SessionFactory可以發現:
@Component
public class CheckClassAccess
{
@Autowired
private static StudentDAO studentDAO;...
的DAO的開始(試只autwiring吸氣劑& setter和構造函數):
@Repository
@SuppressWarnings({"unchecked", "rawtypes"})
public class StudentDAO {
@Autowired
private SessionFactory sessionFactory;
etc...
條
的WEB XML彈簧線:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:WEB-INF/applicationContext.xml</param-value>
</context-param>
and then a little later...
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
其他唯一的告誡這是我試圖得到這個叫OpenCms的一個開源的Java CMS內工作。但不確定這是相關的,因爲我接線的文件是香草java支持類,而不是控制器或任何東西(並非真的想用Spring MVC做它)。
事實上,所有這些工作在一個單獨的小應用程序的Spring MVC servlet上下文中,但我似乎無法獲得這些相同的對象/註釋來註冊applicationContext。
'StudentDAO'包含什麼包? –
您如何評估CheckClassAccess對象,並且是這些類/這些類是否包含在com.shazam.di的子包中,並且還嘗試在包掃描中刪除。* –
StudentDAO位於com.shazam.di.spring.coursemgmt.dao –