在应用程序中如何自动加载sessionFactory
使用SPring,Hibernate,Proxool如何设置dataSource.或者不用设置dataSource
Spring 配置如下:
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!--Scans within the base package of the application for @Components toconfigure as beans--><bean id="sessionFactory"value="org.hibernate.cfg.AnnotationConfiguration" /><property name="configLocation" value="classpath:hibernate.cfg.xml" /></bean><bean id="transactionManager"/><!-- 启用注解 --><context:annotation-config /><!--Instruct Spring to perform declarative transaction managementautomatically on annotated classes.--><tx:annotation-driven /><bean id="gdscImpl" /></beans>
@ContextConfigurationpublic abstract class AbstractIGdscTests extends AbstractTransactionalJUnit4SpringContextTests {@Autowired protected GdscDaoImpl gdscDaoImpl;private static String phone="13800000000";//@Resource(name = "dataSourceContent") @Override public void setDataSource(DataSource dataSource) { super.setDataSource(dataSource); }@Testpublic void TestGdscDao() {List<Gdsc> cacheMessages=this.gdscDaoImpl.findByPhone(phone);assertEquals(35,cacheMessages.size());for(Gdsc cacheMessage:cacheMessages){System.out.println(cacheMessage.getMsg());}}
public interface GdscDao {Gdsc findByPhone(String phone)throws DataAccessException;}
public class GdscDaoImpl implements GdscDao{ @Autowiredprivate SessionFactory sessionFactory; private HibernateTemplate ht;public void setSessionFactory(SessionFactory sessionFactory) {this.sessionFactory=sessionFactory;this.ht=new HibernateTemplate(this.sessionFactory);}@Overridepublic int countByPhone(String phone)throws DataAccessException {return findByPhone(phone).size();}@SuppressWarnings("unchecked")@Overridepublic List<Gdsc> findByPhone(String phone)throws DataAccessException {DetachedCriteria criteria = DetachedCriteria.forClass(Gdsc.class);criteria.add(Expression.eq("destMobile", phone));return (List<Gdsc>)this.ht.findByCriteria(criteria);}
public class GdscSplashProc{[color=red]这里应该如何添加启用spring的Ioc,[/color]@Autowired GdscDaoImpl gdscDaoImpl;public static void main(String[] args){gdscDaoImpl.findByPhone("138001380");}}