zcc小桥流水
zcc小桥流水
关注数: 0
粉丝数: 1
发帖数: 4
关注贴吧数: 0
spring注解空指针怎么解决?(spring整合hibernate) 以下是我的代码: 1、applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://tieba.baidu.com/mo/q/checkurl?url=http%3A%2F%2Fwww.springframework.org%2Fschema%2Fbeans&urlrefer=2cfe664a6b89a7a8c06b489461ff152f" xmlns:xsi="http://tieba.baidu.com/mo/q/checkurl?url=http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema-instance&urlrefer=8c31805956ebb9a7292762c26181562e" xmlns:tx="http://tieba.baidu.com/mo/q/checkurl?url=http%3A%2F%2Fwww.springframework.org%2Fschema%2Ftx&urlrefer=bb0d9ebc89556cf2cb6470e2fd70d1f8" xmlns:aop="http://tieba.baidu.com/mo/q/checkurl?url=http%3A%2F%2Fwww.springframework.org%2Fschema%2Faop&urlrefer=38f64d940c80f0c3bf17aabd28afbd76" xmlns:context="http://tieba.baidu.com/mo/q/checkurl?url=http%3A%2F%2Fwww.springframework.org%2Fschema%2Fcontext&urlrefer=eda4949cc9c44315ba915f719c8a3685" xsi:schemaLocation="http://tieba.baidu.com/mo/q/checkurl?url=http%3A%2F%2Fwww.springframework.org%2Fschema%2Fbeans&urlrefer=2cfe664a6b89a7a8c06b489461ff152f http://tieba.baidu.com/mo/q/checkurl?url=http%3A%2F%2Fwww.springframework.org%2Fschema%2Fbeans%2Fspring-beans.xsd&urlrefer=4668e2927538467f0cfc282cdb1c1df9 http://tieba.baidu.com/mo/q/checkurl?url=http%3A%2F%2Fwww.springframework.org%2Fschema%2Fcontext&urlrefer=eda4949cc9c44315ba915f719c8a3685 http://tieba.baidu.com/mo/q/checkurl?url=http%3A%2F%2Fwww.springframework.org%2Fschema%2Fcontext%2Fspring-context-4.1.xsd&urlrefer=efd4ad1d435247e3fcc17a086bf8120f http://tieba.baidu.com/mo/q/checkurl?url=http%3A%2F%2Fwww.springframework.org%2Fschema%2Faop&urlrefer=38f64d940c80f0c3bf17aabd28afbd76 http://tieba.baidu.com/mo/q/checkurl?url=http%3A%2F%2Fwww.springframework.org%2Fschema%2Faop%2Fspring-aop-4.1.xsd&urlrefer=a2a7840bad20957c3f7bb1d13079ad5c http://tieba.baidu.com/mo/q/checkurl?url=http%3A%2F%2Fwww.springframework.org%2Fschema%2Ftx&urlrefer=bb0d9ebc89556cf2cb6470e2fd70d1f8 http://tieba.baidu.com/mo/q/checkurl?url=http%3A%2F%2Fwww.springframework.org%2Fschema%2Ftx%2Fspring-tx-4.1.xsd&urlrefer=107f895cfd7583acf08057d76b4f03df"> <!-- 自动扫描与装配bean --> <context:component-scan base-package="com.ssh.dao"/> <!-- 导入资源文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置数据源DataSource --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property> <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property> </bean> <!-- 定义Hibernate的sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 指定Hibernate所有映射文件的路径 --> <property name="mappingLocations" value="classpath:com/ssh/entity/*.hbm.xml"/> <!-- 设置Hibernate的属性 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="show_sql">true</prop> <prop key="format_sql">true</prop> <prop key="current_session_context_class">thread</prop> </props> </property> </bean> <!-- 配置Spring的声明式事务 --> <!-- 1配置事务管理器 --> <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到--> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 2配置事务属性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" propagation="REQUIRED"/> <tx:method name="find*" propagation="REQUIRED" /> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 3配置事务切点 --> <aop:config proxy-target-class="true"> <aop:pointcut expression="execution(* com.ssh.dao.*.*(..))" id="txPointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config> </beans> 2、实现类 package com.ssh.dao; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; import com.ssh.entity.User; @Transactional @Repository public class UserDAOImpl implements UserDAO { @Autowired private SessionFactory sessionFactory; public Session getSession(){ return sessionFactory.getCurrentSession(); } public User find(int id) { User user = (User) getSession().get(User.class, id); return user; } public void add(User user) { getSession().save(user); } } 用JUnit4测试时报错为: java.lang.NullPointerException at com.ssh.dao.UserDAOImpl.getSession(UserDAOImpl.java:17) at com.ssh.test.TestSave.testGetSession(TestSave.java:20) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) at com.ssh.dao.UserDAOImpl.getSession(UserDAOImpl.java:17) at com.ssh.test.TestSave.testGetSession(TestSave.java:20) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
1
下一页