package com.cc.shop.test;
import javax.annotation.Resource;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;
@Component
public class UserTest {
private HibernateTemplate hibernateTemplate; @Resource
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
} public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
public static void main(String[] args) {
UserTest ut = new UserTest();
System.out.println(ut.getHibernateTemplate());
}
}这里输出null.下面为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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.cc.shop.*" /> <bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.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}" ></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="packagesToScan">
<list>
<value>com.cc.shop.entity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
import javax.annotation.Resource;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;
@Component
public class UserTest {
private HibernateTemplate hibernateTemplate; @Resource
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
} public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
public static void main(String[] args) {
UserTest ut = new UserTest();
System.out.println(ut.getHibernateTemplate());
}
}这里输出null.下面为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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.cc.shop.*" /> <bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.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}" ></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="packagesToScan">
<list>
<value>com.cc.shop.entity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
解决方案 »
- 大用户访问的Tomcat应该怎么设置
- 系统要加入一个论坛,推荐一下java开源论坛
- 新手求救莫名404错误
- myeclipse下的struts+hibernate问题.
- 求助:java web打印问题
- 求一个java C/S项目做做 要求新鲜 功能强!!!!!!!
- 项目转包,给个报价,本人公司接的单子,做不下来。最好是陕西的高手!!
- 求助!关于STRUTS 的多模块的配置文件问题! 十万火急,在线等!发分发分 !
- 请问一个关于checkbox的问题????
- 该怎样使用javamail发送附件?表单设置dectype="multipart/form-data"之后怎样得到附件?
- 关于Tomcat下配置DataSource的小问题
- 有人会用primefaces吗?
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"><property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>所以 UserTest类 需要继承Hibernatesupport Hibernatetemplate是对Hibernatesupport的简单封装如果你想直接在UserTest中注入Hibernatetemplate,需要在配置中注入Hibernatetemplate(一般不这么用)大多数情况下,都是注入sessionFactory
因为spring的bean用的是JAVA动态代理机制,规定必须要实现一个接口的,没有实现任何接口就不能使用动态代理,就没地方注入了,就出现空指针异常了
继承HibernateDaoSupport /**
*
*/
package dms.cclg.userinfo.dao.impl;import java.util.List;import org.hibernate.cfg.Configuration;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import dms.cclg.entity.usermanage.UserInfoEntity;
import dms.cclg.userinfo.dao.UserManagerDao;/**
* @author Administrator
*
*/
public class UserManagerDaoImpl extends HibernateDaoSupport implements
UserManagerDao {
/*
* (non-Javadoc)
*
* @see dms.cclg.userinfo.dao.UserManagerDao#addUser(dms.cclg.entity.usermanage.UserInfoEntity)
*/
public void addUser(UserInfoEntity obj) {
this.getHibernateTemplate().save(obj);
} /*
* (non-Javadoc)
*
* @see dms.cclg.userinfo.dao.UserManagerDao#deleteUser(int)
*/
public void deleteUser(int id) {
this.getHibernateTemplate().delete(this.findUserById(id));
} /*
* (non-Javadoc)
*
* @see dms.cclg.userinfo.dao.UserManagerDao#findAllUser()
*/
@SuppressWarnings("unchecked")
public List<UserInfoEntity> findAllUser() {
return this.getHibernateTemplate().find("from UserInfoEntity");
} /*
* (non-Javadoc)
*
* @see dms.cclg.userinfo.dao.UserManagerDao#findUser(int, java.lang.String,
* java.lang.String, int)
*/
public List<UserInfoEntity> findUser(int deptid, String username,
String sex, int is_manager) {
return null;
} /*
* (non-Javadoc)
*
* @see dms.cclg.userinfo.dao.UserManagerDao#findUserById(int)
*/
public UserInfoEntity findUserById(int id) {
return (UserInfoEntity) this.getHibernateTemplate().load(
UserInfoEntity.class, id);
} /*
* (non-Javadoc)
*
* @see dms.cclg.userinfo.dao.UserManagerDao#updateUser(int)
*/
public void updateUser(int id) {
this.getHibernateTemplate().update(this.findUserById(id));
}
}