Java @自动连线和空参考异常

Java @自动连线和空参考异常,java,spring,spring-mvc,Java,Spring,Spring Mvc,//位于com\src\myproject\services中 package com.src.myproject.services; public interface IMyService { public boolean Method1(); } package com.src.myproject.services; public class MyService implements IMyService { @Autowired private Test

//位于com\src\myproject\services中

package com.src.myproject.services;

public interface IMyService {
    public boolean Method1();

}
package com.src.myproject.services;

public class MyService implements IMyService {


    @Autowired
    private TestMapper testMapper;

    @Override
    public boolean Method1() {


        return gettestMapper().GetOrder(1); //Get null pointer exception as testMapper is null in debug mode
    }



    public TestMapper gettestMapper(){

            return testMapper;
        }


    public void settestMapper(TestMapper testMapper){
        this.testMapper=testMapper;
        }

}
//位于com\src\myproject\services中

package com.src.myproject.services;

public interface IMyService {
    public boolean Method1();

}
package com.src.myproject.services;

public class MyService implements IMyService {


    @Autowired
    private TestMapper testMapper;

    @Override
    public boolean Method1() {


        return gettestMapper().GetOrder(1); //Get null pointer exception as testMapper is null in debug mode
    }



    public TestMapper gettestMapper(){

            return testMapper;
        }


    public void settestMapper(TestMapper testMapper){
        this.testMapper=testMapper;
        }

}
我的
mapper.java
文件位于
com\src\mappers

相应的
mapper.xml
位于
src\main\resources\com\src\myproject\mappers

我的
servelet context.xml
文件是

<?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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
                    http://www.springframework.org/schema/tx 
                    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
                    http://www.springframework.org/schema/context 
                    http://www.springframework.org/schema/context/spring-context-3.0.xsd
                    http://www.springframework.org/schema/util
                    http://www.springframework.org/schema/util/spring-util-3.1.xsd">


    <!-- Root Context: defines shared resources visible to all other web components -->

    <context:property-placeholder location="/WEB-INF/spring/jdbc.properties,/WEB-INF/spring/mybatis.properties" />

    <context:component-scan base-package="com.src.myproject.controllers" />
    <context:component-scan base-package="com.src.myproject.services" />


    <!-- Enable annotation style of managing transactions -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- Declare a datasource that has pooling capabilities -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close" p:driverClass="${app.jdbc.driverClassName}"
    p:jdbcUrl="${app.jdbc.url}" p:user="${app.jdbc.username}" p:password="${app.jdbc.password}"
    p:acquireIncrement="10" p:idleConnectionTestPeriod="60" p:maxPoolSize="100"
    p:maxStatements="50" p:minPoolSize="10" />

    <!-- Declare a transaction manager -->
    <bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
    p:dataSource-ref="dataSource"
    autowire="byName" />


    <!-- define the SqlSessionFactory, notice that configLocation is not needed when you use MapperFactoryBean -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="typeAliasesPackage" value="${typeAliasesPackage}" />
    </bean>

    <!-- scan for mappers and let them be autowired -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.src.myproject.mappers" />
         <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean> 
</beans>


服务文件中的自动连接总是空的,当我试图通过自动连接使用映射器文件中的任何方法时,会出现空引用异常。

您不应该直接实例化Springbeans。它是负责对象创建的容器。因此,在控制器中,而不是:

IMyService mySvc = new Service();
创建这样的字段:

@Autowired
private IMyService mySvc;
Spring将发现它并分配适当的值,这是
MyService
预先为您创建的一个实例。

<context:annotation-config />


帮助?

如何获取
MyService
的实例?您没有调用
new MyService()
,是吗?@TomaszNurkiewicz:在控制器中,它被实例化为
IMyService mySvc=new Service():自动连线依赖项的注入失败;嵌套异常为org.springframework.beans.factory.BeanCreationException:无法自动连接字段:private com.src.myproejct.services.IMyService com.src.myproject.controllers.MyController.myService;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为[com.src.myproject.services.IMyService]的匹配bean为依赖项找到:应至少有1个bean符合此依赖项的autowire候选。在此行找到多个批注:-处理XML时出错“必须至少指定一个基本包”。有关更多详细信息,请参阅错误日志-cvc复杂类型。4:属性“基本包”必须出现在元素“上下文:组件扫描”上。
在您的服务中,您没有使用@service或它的一个替代项对该类进行注释。我确实放置了该注释。让我做一个干净的构建。@craftand:你的建议也有帮助。无法将2条注释标记为答案。但是非常感谢