我在这方面看到了很多问题,并尝试了许多不同解决方案的无数排列,但没有一个奏效。
我有一个需要休眠会话工厂来执行事务的 dao。在 SpringMVC 上下文中,我已经看到它工作但 java 类中包含的 dao 为空。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 only getter & setter 和一个构造函数):
@Repository
@SuppressWarnings({"unchecked", "rawtypes"})
public class StudentDAO {
@Autowired
private SessionFactory sessionFactory;
etc...
WEB XML Spring 行:
<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 中工作。但不确定这是否相关,因为我正在连接的文件是 vanilla java 支持类,而不是控制器或任何东西(还没有真正想用它来做 Spring-MVC)。
事实上,所有这些都在一个单独的较小应用程序的 Spring MVC servlet-context 中工作,但我似乎无法让这些相同的对象/注释在 applicationContext 中注册。