最近打算整合最新的spring3和hibernate3.6。但是老是出现问题。
首先我引入了所需要的jar包。
我的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:aop="http://www.springframework.org/schema/aop"
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-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/home?characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="11111" />
</bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.bfa.model"/>
</bean>
<bean id="recordDao" class="com.bfa.daoImp.RecordDaoImp"/>
<bean id="recordService" class="com.bfa.serviceImp.RecordServer">
<property name="recordDao" ref="recordDao"></property>
</bean>
</beans>
我在hibernate3.6 中使用了annotation。所以我想在spring中也使用annotation。我初步的设想是将sessionFactory注入到recordDao中。但是,当我测试的时候却报了一个空指针错误。recordDaoImp 中的内容如下:private SessionFactory sessionFactory; public SessionFactory getSessionFactory() {
return sessionFactory;
} @Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public List<Record> getAll() throws SQLException {
List<Record> list=new ArrayList<Record>();
System.out.println(this.getSessionFactory());//这里打印出来一个null
Session session=this.sessionFactory.getCurrentSession();
session.beginTransaction();
list=session.createQuery("from Record order by id desc").list();
session.getTransaction().commit();
return list;
}请大家帮我看看是什么原因。
首先我引入了所需要的jar包。
我的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:aop="http://www.springframework.org/schema/aop"
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-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/home?characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="11111" />
</bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.bfa.model"/>
</bean>
<bean id="recordDao" class="com.bfa.daoImp.RecordDaoImp"/>
<bean id="recordService" class="com.bfa.serviceImp.RecordServer">
<property name="recordDao" ref="recordDao"></property>
</bean>
</beans>
我在hibernate3.6 中使用了annotation。所以我想在spring中也使用annotation。我初步的设想是将sessionFactory注入到recordDao中。但是,当我测试的时候却报了一个空指针错误。recordDaoImp 中的内容如下:private SessionFactory sessionFactory; public SessionFactory getSessionFactory() {
return sessionFactory;
} @Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public List<Record> getAll() throws SQLException {
List<Record> list=new ArrayList<Record>();
System.out.println(this.getSessionFactory());//这里打印出来一个null
Session session=this.sessionFactory.getCurrentSession();
session.beginTransaction();
list=session.createQuery("from Record order by id desc").list();
session.getTransaction().commit();
return list;
}请大家帮我看看是什么原因。
解决方案 »
- ejb和javabean什么区别
- java压缩成.zip格式问题
- J2EE 课程,大家都学多久了,这些试一下吧?
- MyEclipse自动加载DTD头问题
- hibernate java.util.NoSuchElementException错误
- glassfish问题 请教大神
- 关于部署的一个很菜的问题有那位高手帮看一下谢了!
- easy
- EJB 如何调用 EJB ?见者有分!bbsnews 调用 bbsdb
- 求教,JPA调用Service如何能不用@Autowired,我想用接口封装Service
- |.NET转Java|在C#中给字段添加特性和取得特性内容在JAVA中是怎么做的,谢谢
- 快速开发平台
加上:
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.jdbc.batch_size">50</prop>
<prop key="hibernate.query.substitutions">true 1, false 0</prop>
<prop key="hibernate.connection.SetBigStringTryClob">true</prop>
</props>
</property>
然后写方法注入
例如:
@Resource(name="sessionFactory")
public void setSuperSessionFactory(SessionFactory sessionFactory){
super.setSessionFactory(sessionFactory);
}
没有注入进去!首先你的dao实现类要继承Spring提供的HibernateDaoSupport
然后写方法注入
例如:
@Resource(name="sessionFactory")
public void setSuperSessionFactory(SessionFactory sessionFactory){
super.setSessionFactory(sessionFactory);
}