错误是:继承HibernateDaoSupport的UsersImpl获取到的getHibernateTemplate()一直没有值 这是applicationContext的配置<bean id=”dataSource” class=”com.mchange.v2.c3p0.ComboPooledDataSource” destroy-method=”close”> <bean id=”userDao” class=”test1.impl.UsersImpl”> <bean id=”transactionManager” class=”org.springframework.orm.hibernate4.HibernateTransactionManager”> <bean id=”transactionInterceptor” class=”org.springframework.transaction.interceptor.TransactionInterceptor”> import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import test1.dao.UsersDao; public class UsersImpl extends HibernateDaoSupport implements UsersDao { return SUCCESS; 运行后出现结果为: 发现好像是spring没给UsersImpl里的sessionFactory注入值,导致hibernateTemplate也是null 到底是哪里出错了呢? |
|
5分 |
hibernate和spring结合的时候,可以直接用hibernateTemplate,也是由spring进行注入,加上代码试试:
<bean id=”hibernateTemplate” class=”org.springframework.orm.hibernate3.HibernateTemplate”> <property name=”sessionFactory” ref=”sessionFactory” /> </bean> |
UsersDao uu=new UsersImpl();
你这里用new了当然没注入了。你都叫给spring管理了应该由spring来创建这个UsersDao UsersDao uu=(UsersDao)new ClassPathXmlApplicationContext(“applicationContext.xml”).getBean(“userDao”); |
|
sessionFactory多好。
|
|
无论怎样sessionFactory都是注入不了值
|
|
javassist的jar包重复了一个。
|
|
我发现重复了,后来也删掉了,但还是没用。反正测试来测试去就是发现sessionFactory无法注入
|
|
把控制台信息全贴出来,初始化的时候如果有何异常,其中应该会有所体现 |
|
2013-1-4 13:42:51 org.apache.catalina.core.AprLifecycleListener init
信息: Loaded APR based Apache Tomcat Native library 1.1.24 using APR version 1.4.6. 2013-1-4 13:42:51 org.apache.catalina.core.AprLifecycleListener init 信息: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true]. 2013-1-4 13:42:52 org.apache.catalina.core.AprLifecycleListener initializeSSL 信息: OpenSSL successfully initialized (OpenSSL 1.0.1c 10 May 2012) 2013-1-4 13:42:52 org.apache.coyote.AbstractProtocol init 信息: Initializing ProtocolHandler [“http-apr-8080”] 2013-1-4 13:42:52 org.apache.coyote.AbstractProtocol init 信息: Initializing ProtocolHandler [“ajp-apr-8009”] 2013-1-4 13:42:52 org.apache.catalina.startup.Catalina load 信息: Initialization processed in 1233 ms 2013-1-4 13:42:52 org.apache.catalina.core.StandardService startInternal 信息: Starting service Catalina 2013-1-4 13:42:52 org.apache.catalina.core.StandardEngine startInternal 信息: Starting Servlet Engine: Apache Tomcat/7.0.32 2013-1-4 13:42:52 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deploying web application directory C:\apache-tomcat-7.0.32\webapps\docs 2013-1-4 13:42:52 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deploying web application directory C:\apache-tomcat-7.0.32\webapps\examples 2013-1-4 13:42:53 org.apache.catalina.core.ApplicationContext log 信息: ContextListener: contextInitialized() 2013-1-4 13:42:53 org.apache.catalina.core.ApplicationContext log 信息: SessionListener: contextInitialized() 2013-1-4 13:42:53 org.apache.catalina.core.ApplicationContext log 信息: ContextListener: attributeAdded(“”org.apache.jasper.compiler.TldLocationsCache””, “”org.apache.jasper.compiler.TldLocationsCache@940b84″”) 2013-1-4 13:42:53 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deploying web application directory C:\apache-tomcat-7.0.32\webapps\host-manager 2013-1-4 13:42:53 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deploying web application directory C:\apache-tomcat-7.0.32\webapps\manager 2013-1-4 13:42:53 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deploying web application directory C:\apache-tomcat-7.0.32\webapps\ROOT 2013-1-4 13:42:53 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deploying web application directory C:\apache-tomcat-7.0.32\webapps\ssh_test1 2013-1-4 13:42:57 org.apache.catalina.core.ApplicationContext log 信息: No Spring WebApplicationInitializer types detected on classpath 2013-1-4 13:42:57 org.apache.catalina.core.ApplicationContext log 信息: Initializing Spring root WebApplicationContext 2013-1-4 13:42:57 org.springframework.web.context.ContextLoader initWebApplicationContext 信息: Root WebApplicationContext: initialization started 2013-1-4 13:42:57 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing Root WebApplicationContext: startup date [Fri Jan 04 13:42:57 CST 2013]; root of context hierarchy 2013-1-4 13:42:57 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] 2013-1-4 13:42:58 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@14653a3: defining beans [dataSource,sessionFactory,hibernateTemplate,userDao,mgr,transactionManager,transactionInterceptor,proxyCreator]; root of factory hierarchy 2013-1-4 13:42:58 com.mchange.v2.log.MLog <clinit> 信息: MLog clients using java 1.4+ standard logging. 2013-1-4 13:42:58 com.mchange.v2.c3p0.C3P0Registry banner 信息: Initializing c3p0-0.9.1 [built 16-January-2007 14:46:42; debug? true; trace: 10] 2013-1-4 13:42:59 org.hibernate.annotations.common.Version <clinit> INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final} 2013-1-4 13:42:59 org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {4.1.9.Final} 2013-1-4 13:42:59 org.hibernate.cfg.Environment <clinit> INFO: HHH000206: hibernate.properties not found 2013-1-4 13:42:59 org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist 2013-1-4 13:42:59 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide! 2013-1-4 13:42:59 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager 信息: Initializing c3p0 pool… com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1bquq9k8s14sgwby4xsao|ed65e0, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> oracle.jdbc.driver.OracleDriver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1bquq9k8s14sgwby4xsao|ed65e0, idleConnectionTestPeriod -> 0, initialPoolSize -> 1, jdbcUrl -> jdbc:oracle:thin:@localhost:1521:orcl, lastAcquisitionFailureDefaultUser -> null, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 20, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 1, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ] 2013-1-4 13:43:00 org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect 2013-1-4 13:43:00 org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException 2013-1-4 13:43:00 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService INFO: HHH000399: Using default transaction strategy (direct JDBC transactions) 2013-1-4 13:43:00 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init> INFO: HHH000397: Using ASTQueryTranslatorFactory 2013-1-4 13:43:01 org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000228: Running hbm2ddl schema update 2013-1-4 13:43:01 org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000102: Fetching database metadata 2013-1-4 13:43:01 org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000396: Updating schema 2013-1-4 13:43:01 org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000261: Table found: SSH.USERS 2013-1-4 13:43:01 org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000037: Columns: [id, birth, name, pass] 2013-1-4 13:43:01 org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000108: Foreign keys: [] 2013-1-4 13:43:01 org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000126: Indexes: [pk_user_id] 2013-1-4 13:43:01 org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000232: Schema update complete 2013-1-4 13:43:01 org.springframework.orm.hibernate4.HibernateTransactionManager afterPropertiesSet 信息: Using DataSource [com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1bquq9k8s14sgwby4xsao|ed65e0, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> oracle.jdbc.driver.OracleDriver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1bquq9k8s14sgwby4xsao|ed65e0, idleConnectionTestPeriod -> 0, initialPoolSize -> 1, jdbcUrl -> jdbc:oracle:thin:@localhost:1521:orcl, lastAcquisitionFailureDefaultUser -> null, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 20, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 1, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]] of Hibernate SessionFactory for HibernateTransactionManager 2013-1-4 13:43:01 org.springframework.web.context.ContextLoader initWebApplicationContext 信息: Root WebApplicationContext: initialization completed in 4228 ms |
|
2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Parsing configuration file [struts-default.xml] 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Parsing configuration file [struts-plugin.xml] 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Parsing configuration file [struts.xml] 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Choosing bean (spring) for (com.opensymphony.xwork2.ObjectFactory) 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Choosing bean (struts) for (com.opensymphony.xwork2.FileManagerFactory) 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Choosing bean (struts) for (com.opensymphony.xwork2.conversion.impl.XWorkConverter) 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Choosing bean (struts) for (com.opensymphony.xwork2.TextProvider) 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Choosing bean (struts) for (com.opensymphony.xwork2.LocaleProvider) 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Choosing bean (struts) for (com.opensymphony.xwork2.ActionProxyFactory) 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Choosing bean (struts) for (com.opensymphony.xwork2.conversion.ObjectTypeDeterminer) 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Choosing bean (struts) for (org.apache.struts2.dispatcher.mapper.ActionMapper) 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Choosing bean (jakarta) for (org.apache.struts2.dispatcher.multipart.MultiPartRequest) 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Choosing bean (struts) for (org.apache.struts2.views.freemarker.FreemarkerManager) 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Choosing bean (struts) for (org.apache.struts2.components.UrlRenderer) 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Choosing bean (struts) for (com.opensymphony.xwork2.validator.ActionValidatorManager) 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Choosing bean (struts) for (com.opensymphony.xwork2.util.ValueStackFactory) 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Choosing bean (struts) for (com.opensymphony.xwork2.util.reflection.ReflectionProvider) 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Choosing bean (struts) for (com.opensymphony.xwork2.util.reflection.ReflectionContextFactory) 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Choosing bean (struts) for (com.opensymphony.xwork2.util.PatternMatcher) 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Choosing bean (struts) for (org.apache.struts2.dispatcher.StaticContentLoader) 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Choosing bean (struts) for (com.opensymphony.xwork2.UnknownHandlerManager) 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Choosing bean (struts) for (org.apache.struts2.views.util.UrlHelper) 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Choosing bean (struts) for (com.opensymphony.xwork2.conversion.impl.CollectionConverter) 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Choosing bean (struts) for (com.opensymphony.xwork2.conversion.impl.ArrayConverter) 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Choosing bean (struts) for (com.opensymphony.xwork2.conversion.impl.DateConverter) 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Choosing bean (struts) for (com.opensymphony.xwork2.conversion.impl.NumberConverter) 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Choosing bean (struts) for (com.opensymphony.xwork2.conversion.impl.StringConverter) 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Initializing Struts-Spring integration… 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Setting autowire strategy to name 2013-1-4 13:43:02 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: … initialized Struts-Spring integration successfully 2013-1-4 13:43:02 org.apache.coyote.AbstractProtocol start 信息: Starting ProtocolHandler [“http-apr-8080”] 2013-1-4 13:43:02 org.apache.coyote.AbstractProtocol start 信息: Starting ProtocolHandler [“ajp-apr-8009”] 2013-1-4 13:43:02 org.apache.catalina.startup.Catalina start 信息: Server startup in 10525 ms |
|
信息贴出来了 |
|
你用new的方式当然不行了,你只要提供setter getter由spring注入就可以了
|
|
是UsersImpl里的getHibernateTemplate()获取不到值,不关LoginAction里的代码的事。那是为了方便测试写的,实际上自然不会那么写。 |
|
extends HibernateDaoSupport
HibernateDaoSupport 这个楼主配置了吗? |
|
5分 |
楼主用这种方式能获取的到就ok了 |
<bean id="HibernateDaoSupport" class="org.springframework.orm.hibernate3.support.HibernateDaoSupport"> <property name="dataSource" ref="dataSource"/> </bean> 试一试 |
|
不行啊,说是什么抽象类 |
|
试了下,报错很严重 应该就是 <bean id=”userDao” class=”test1.impl.UsersImpl”> |
|
是在测试类里面写的还是在启动服务后的action里面写的。你配置没有什么问题如果在启动服务后的action里面直接给setter getter就可以了,如果是不启动服务的JUnit用这种写法测试
|
|
启动服务器后的LoginAction里的: public int loginValid(String name, String pass) throws Exception { } <bean id=”mgr” class=”test1.impl.UsersManagerImpl”> WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide! |
|
30分 |
你这异常信息不是正规的。
|
注入有没成功先看
|
|
好像注入成功了,但还是报错,这句话错了吗? Users是 public class Users implements Serializable { public Users() { —————————– <hibernate-mapping package=”test1.po”> |
|
突然间发现代码有几处地方错了,改正后新错误信息变为hibernate4和hibernate3不能混用,因为hibernateDaoSupport只有hibernate3有,4没有。后来换为hibernate3,能正确出结果了。
只是hibernate4既然出了,难道就不能用吗? |
|
这句话引导出了我的错误。 |
|
肯跌啊 居然是版本! |
|
hibernate4不再支持HibernateDaoSupport这个类了,需要使用session来完成增删改查。
获取session的方法: private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { |