Spring3 @Resource hibernateTemplate失败

xuming9 2011-12-03 11:28:23
 
@Repository("AccountGroupDaoImpl")
public class AccountGroupDaoImpl implements IAccountGroupDao {
private static final Logger log = LoggerFactory
.getLogger(AccountGroupDaoImpl.class);
// property constants
public static final String AGDESC = "agdesc";


private HibernateTemplate hibernateTemplate;

@Resource
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}

/*
* (non-Javadoc)
*
* @see
* com.jsyy.tb.dao.impl.IAccountGroupDao#save(com.jsyy.tb.module.AccountGroup
* )
*/
@Override
public void save(SuperModule sm) {
// TODO Auto-generated method stub
log.debug("saving Accountgroup instance");
try {
hibernateTemplate.save((AccountGroup)sm);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
}





<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>/Hibernate/AccountGroup.hbm.xml</value>
</list>
</property>
<!-- 定义Hibernate的SessionFactory的属性 -->
<property name="hibernateProperties">
<props>
<!-- 一般属性设置 -->
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="heibernat.show_sql">true</prop>
<prop key="heibernat.format_sql">true</prop>
<prop key="heibernat.c3p0.validate">true</prop>
<!-- 解决查询中文的问题 -->
<prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
<!-- auto将为JTA和CMT事务策略选择after_statement, 为JDBC事务策略选择after_transaction -->
<prop key="hibernate.connection.release_mode">auto</prop>
<prop key="hibernate.autoReconnect">true</prop>
<prop key="hibernate.autoReconnectForPools">true</prop>
<!-- 缓存设置,启动缓存配置 -->
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<!-- 设置二级缓存:缓存中的持久化对象仅能通过id进行查找利用 -->
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<!-- 设置查询缓存:缓存中的持久化对象可以通过SQL、HQL进行查找利用,且语句的入参必须相同 -->
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="javax.persistence.validation.mode">none</prop>
</props>
</property>
</bean>
<bean id="dataSource"
class ="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- mysql数据库链接
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/DB_DADA</value>
</property>
<property name="password">
<value></value>
</property>
<property name="username">
<value>root</value>
</property>
-->
<!-- sqlServer连接 -->
<property name="driverClassName">
<value>net.sourceforge.jtds.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:jtds:sqlserver://localhost:1433;DatabaseName=TerminalBox</value>
</property>
<property name="password">
<value>sa</value>
</property>
<property name="username">
<value>sa</value>
</property>
</bean>

<bean name="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>



public class SpringHibernateTest01 {
public static void main(String[] args) {
Resource rs=new ClassPathResource("Spring/applicationContext.xml");
BeanFactory bf=new XmlBeanFactory(rs);
// BeanFactory bf = new ClassPathXmlApplicationContext("classpath:WebRoot/WEB-INF/config/Spring/applicationContext.xml");
AccountGroup ag=(AccountGroup)bf.getBean("accountGroup");
ag.setAgname("test001");
ag.setAgdesc("test001");

AccountGroupDaoImpl iagd=(AccountGroupDaoImpl)bf.getBean("AccountGroupDaoImpl");
iagd.save(ag);
System.out.println(ag.getAgname());
}
}


老是说hibernateTemplate是java.lang.NullPointerException

在网上查了好久,实在不知道怎么办了,请教大家了,谢谢
...全文
92 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuming9 2011-12-04
  • 打赏
  • 举报
回复
晕了 ,好像@Resource只能应用与web应用下.

81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧