空指针异常

来源:6-1 声明式事务管理方式一:基于TransactionProxyFactoryBean的方式

冰舞魅火

2018-08-04 22:22


<?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.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-util.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
      <!-- 引入外部属性文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>


    <!-- 配置c3p0的连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>

    </bean>

    <!-- 配置业务类 -->
    <bean id="accountService" class="com.demo.AccountServiceInpl">
        <property name="accountDao" ref="accountDao"/>
        <context:component-scan base-package="com.demo"/>

    </bean>
    <!-- 配置dao类 -->
    <bean id="accountDao" class="com.demo.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

配置文件如上图  不知道问题在哪

写回答 关注

7回答

  • Ming_giegie
    2018-08-05 20:19:17
    已采纳
    <context:component-scan base-package="com.demo"/>

    你把这行配置放到<bean>外边试试,不行我也看不出有什么毛病了

    冰舞魅火

    好吧 还是谢谢你了

    2018-08-05 20:37:45

    共 1 条回复 >

  • qq_牵强貌似坚强_0
    2018-08-07 21:44:25

    c3p0连接池用户名那行确定是${jdbc.user}?,应该不是的吧

  • Ming_giegie
    2018-08-05 16:55:20

    咦?回复怎么这么奇怪?com.demo.AccountServiceInpl改成com.demo.AccountServiceImpl

    冰舞魅火

    (⊙o⊙)… 名字确实错了 但是我在bean配置的名字和实现类名字是一样的 错误应该不在这吧

    2018-08-05 18:16:50

    共 1 条回复 >

  • Ming_giegie
    2018-08-05 16:53:10

    application配置文件 id为accountService的bean类名称写错了吧  

    class="com.demo.AccountService"  应该是com.demo.'AccountService


  • 冰舞魅火
    2018-08-05 13:49:46

    https://img3.mukewang.com/5b668fdd0001456109570405.jpg 不知道空指针发生在哪  代码应该没问题

  • 冰舞魅火
    2018-08-05 13:42:04


    java.lang.NullPointerException

    at com.demo.Test1.Test1(Test1.java:22)

    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

    at java.base/java.lang.reflect.Method.invoke(Method.java:564)

    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)

    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

    at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)

    at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)

    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)

    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)

    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)

    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)

    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)

    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)

    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)

    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)

    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)

    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)

    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)

    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)

    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)

    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)



  • 你爱的是我吗
    2018-08-05 11:02:14

    报错信息发上来

Spring事务管理

事务管理是Spring重要的知识,应用事务解决数据不一致问题

87318 学习 · 197 问题

查看课程

相似问题