java - SSH整合時(shí) getHibernateTemplate()為空
問題描述
@Repositorypublic class AuthorAdminDaoImpl implements IAuthorAdminDao { @Resource private BaseDAO basedao;public boolean loginAuthorAdmin(AuthorAdmin aa) {try { String queryString = ' from AuthorAdmin a where a.authorName= ? and a.authorPwd= ? '; basedao=new BaseDAO(); if (basedao.getTemplate() == null) {System.out.println('沒有獲得HibernateTemplate?'); } List<AuthorAdmin> find = (List<AuthorAdmin>) basedao.getTemplate().find( queryString, aa.getAuthorUsername(), aa.getAuthorPwd()); if (find != null && find.size() > 0) {return true; } else {return false; }} catch (Exception e) { e.printStackTrace(); throw new RuntimeException();}} }
public class BaseDAO extends HibernateDaoSupport{ public HibernateTemplate getTemplate(){return getHibernateTemplate(); }}
HibernateTemplate為空不知道為什么?用的是Spring注入
<?xml version='1.0' encoding='UTF-8'?><beans xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://www.springframework.org/schema/beans' xmlns:context='http://www.springframework.org/schema/context'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.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.1.xsd'><!-- 激活組件掃描功能,在包gk.aop及其子包下面自動(dòng)掃描通過注解配置的組件 --> <context:component-scan base-package='com.lcy' /> <!-- 激活自動(dòng)代理功能,false或者省略基于接口的代理,為true基于類的的代理 --> <aop:aspectj-autoproxy proxy-target- /> <bean class='org.apache.commons.dbcp.BasicDataSource'><property name='driverClassName' value='com.mysql.jdbc.Driver'></property><property name='url' value='jdbc:mysql://localhost:3306/ssh'></property><property name='username' value='root'></property><property name='password' value='root'></property> </bean> <bean class='org.springframework.orm.hibernate3.LocalSessionFactoryBean'><property name='dataSource'> <ref bean='dataSource' /></property><property name='hibernateProperties'> <props><prop key='hibernate.dialect'> org.hibernate.dialect.MySQLDialect</prop><prop key='hibernate.format_sql'> true</prop><prop key='hibernate.show_sql'> true</prop> </props></property><property name='mappingResources'> <list><value>com/lcy/po/DocumentInfo.hbm.xml</value><value>com/lcy/po/ColumnEditorAdmin.hbm.xml</value><value>com/lcy/po/ExpertAdmin.hbm.xml</value><value>com/lcy/po/AttachInfo.hbm.xml</value><value>com/lcy/po/DocumentStateInfo.hbm.xml</value><value>com/lcy/po/ColumnInfo.hbm.xml</value><value>com/lcy/po/MessageInfo.hbm.xml</value><value>com/lcy/po/MagazineEditorAdmin.hbm.xml</value><value>com/lcy/po/BoardInfo.hbm.xml</value><value>com/lcy/po/ExpertAssess.hbm.xml</value><value>com/lcy/po/AuthorAdmin.hbm.xml</value> </list></property> </bean> <bean class='com.lcy.dao.BaseDAO'><property name='sessionFactory'> <ref bean='sessionFactory'></ref></property> </bean></beans>
問題解答
回答1:本來 Spring 已經(jīng)幫你把 basedao 注入了你XML里配置好的對(duì)象, 你后面又用
basedao = new BaseDAO();
創(chuàng)建了個(gè)新對(duì)象, 把那個(gè) Spring 給注入的 bean 替換掉了...
相關(guān)文章:
1. javascript - 怎樣定位一個(gè)動(dòng)作調(diào)用了哪個(gè)js,不打斷點(diǎn)調(diào)試?2. javascript - 如何清除向可編輯的(contenteditable)元素里粘貼的文本的標(biāo)簽和樣式?3. javascript - js正則替換日期格式問題4. javascript - 關(guān)于微信掃一掃的技術(shù)問題5. javascript - ios上fixed定位問題,定位在底部的按鈕不顯示了,但是又可以點(diǎn)擊到,換了一個(gè)類名就可以顯示了,但是設(shè)置的字體大小卻失效了6. javascript - webpack 打包 reactjs項(xiàng)目 css 分離7. javascript - vuex中子組件無法調(diào)用公共狀態(tài)8. javascript - 請(qǐng)教移動(dòng)端從詳情頁返回到列表頁原來位置的問題?9. javascript - (_a = [""], _a.raw = [""],....); js一個(gè)小括號(hào)的是什么意思?10. javascript - Vue.js的ElementUI庫(kù)中,如何主動(dòng)觸發(fā)checkbox組件的change事件?
