SSH整合,自己熟悉spring架构写的一个小程序出现的问题。

pHscccr_Ayyx 2016-12-07 11:33:30
这是异常:
java.lang.NullPointerException
at com.sccc.testAccount.SpringDemo1.demo1(SpringDemo1.java:36)
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.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66)
at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
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:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
...全文
1360 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Andy的博客 2018-03-21
  • 打赏
  • 举报
回复
nullpointerexception
pHscccr_Ayyx 2016-12-07
  • 打赏
  • 举报
回复
另外一个小问题是: 大家应该可以看到我在service和dao的impl里面都写了无参构造方法,在里面输出我要的数据。 但是运行项目报出异常时发现并没有输出这两个test,所以我想问一下有@Resource注入bean的实例时,是用的无参构造方法注入的吗?
pHscccr_Ayyx 2016-12-07
  • 打赏
  • 举报
回复
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 引入外部的属性文件 --> <context:annotation-config></context:annotation-config> <!-- 扫描文件 --> <context:component-scan base-package="com.sccc"></context:component-scan> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <!-- 生成hibernate模版 --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <!-- 配置c3p0连接池 --> <!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> --> <!-- 配置业务层 --> <!-- <bean id = "accountService" class="com.sccc.webServiceImpl.AccountServiceImpl"> <property name="accountDao" ref="accountDao"></property> </bean> --> <!-- 配置DAO层 --> <!-- <bean id = "accountDao" class="com.sccc.DAOImpl.AccountDaoImpl"> <property name="dataSource" ref="dataSource"></property> </bean> --> </beans> <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>AccountTransfer</display-name> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> </web-app> 这是配置文件。
pHscccr_Ayyx 2016-12-07
  • 打赏
  • 举报
回复
package com.sccc.DAO; /** * * @author sccc * 转账案例的dao层的接口 * */ public interface AccountDao { /** * * @param out * @param money */ public void outMoney(String out, Double money); /** * * @param in * @param money */ public void inMoney(String in, Double money); } package com.sccc.DAOImpl; import javax.activation.DataSource; import javax.annotation.Resource; import org.hibernate.SQLQuery; import org.hibernate.classic.Session; import org.springframework.context.annotation.Scope; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.support.JdbcDaoSupport; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; import com.sccc.DAO.AccountDao; @Repository("accountDao") @Transactional(readOnly=false) public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao{ public AccountDaoImpl(){ System.out.println("Test_2"); } @Resource(name = "hibernateTemplate") private HibernateTemplate hibernateTemplate; @Override public void outMoney(String out, Double money) { String sql = "update account set money = money - ? where name = ?"; Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession(); SQLQuery query = session.createSQLQuery(sql); query.setParameter(0, money); query.setParameter(1, out); query.executeUpdate(); } @Override public void inMoney(String in, Double money) { String sql = "update account set money = money + ? where name = ?"; Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession(); SQLQuery query = session.createSQLQuery(sql); query.setParameter(0, money); query.setParameter(1, in); query.executeUpdate(); } } package com.sccc.webService; import org.springframework.stereotype.Service; /** * * @author sccc * 转账案例的接口 * */ public interface AccountService { /** * @param out :转出的帐号 * @param in :转入的帐号 * @param money :转账的金额 */ public void transfer(String out, String in, Double money); } package com.sccc.webServiceImpl; import javax.annotation.Resource; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.sccc.DAO.AccountDao; import com.sccc.webService.AccountService; /** * * @author sccc * 转账案例接口的实现类 * */ @Service("accountService") @Transactional(readOnly=false) public class AccountServiceImpl implements AccountService { public AccountServiceImpl(){ System.out.println("Test_1"); } //注入转账的Dao类 @Resource(name = "accountDao") private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } /** * @param out :转出的帐号 * @param in :转入的帐号 * @param money :转账的金额 */ @Override public void transfer(String out, String in, Double money) { accountDao.outMoney(out, money); accountDao.inMoney(in, money); } } package com.sccc.testAccount; import javax.annotation.Resource; import org.junit.Test; import org.springframework.context.annotation.Scope; import com.sccc.webService.AccountService; /** * * @author sccc *转账案例的测试类 */ @Scope("prototype") public class SpringDemo1 { //测试业务层类 @Resource(name = "accountService") private AccountService accountService; public AccountService getAccountService() { return accountService; } public void setAccountService(AccountService accountService) { this.accountService = accountService; } @Test public void demo1(){ System.out.println("TEST"); accountService.transfer("aaa", "bbb", 200.5d); } } 这是代码。

3,405

社区成员

发帖
与我相关
我的任务
社区描述
专题开发/技术/项目 设计模式
社区管理员
  • 设计模式
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧