java - 将 daos Autowiring 到服务中,并将该服务 Autowiring 到 Junit 测试类中

标签 java spring mybatis

我正在尝试将 Dao Autowiring 到服务中,并将服务 Autowiring 到 Junit 测试类中,但出现了以下错误。

    productService:null
java.lang.NullPointerException
    at com.localbazaar.product.test.ProductServiceTest.test01_selectAuthor(ProductServiceTest.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    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:236)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
    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)

当我为 ProductService 编写接口(interface)和实现时,服务对象正在打印,但作者对象为空。

谁能告诉我如何解决这个问题?

//DAO
package com.dao;

import com.domain.Author;

public interface AuthorMapper {

    int deleteByPrimaryKey(Integer authorId);
}

//service
package com.service;

@Service
public class ProductService{

    @Autowired
    private AuthorMapper authorMapper;
        public AuthorselectAuthorByPrimaryKey(Integer authorId) {
        Author author = null;
        try{
            author = authorMapper.selectAuthorByPrimaryKey(authorId);
        }catch(Exception e){}
        return author ;
    }
}
---------------------------------------------------------------------------------
// JUnit Test Class
@ContextConfiguration(locations = {"classpath:springTest/springcontext.xml"})
@Controller
public class ProductServiceTest extends   AbstractTransactionalJUnit4SpringContextTests {

        @Autowired
    private ProductService productService;

    @Test
    public void test01_selectAuthor(){
        try{
        System.out.println("productService:"+productService);
            Author author = productService.selectAuthorByPrimaryKey(1);
            System.out.println(author);
            System.out.println(author.getAuthorId());
            System.out.println(author.getSourceId());
            System.out.println(author.getName());
            System.out.println(author.getPersonalName());
            System.out.println(author.getRevision());
            System.out.println(author.getLastModified());
        }catch(Exception e){
            e.printStackTrace();
        }
    }   
}
----------------------------------------------------------------------------------

AuthoerMapper.xml



  <resultMap id="BaseResultMap" type="com.domain.Author" >
    <id column="author_id" property="authorId" jdbcType="INTEGER" />
    <result column="source_id" property="sourceId" jdbcType="VARCHAR" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="personal_name" property="personalName" jdbcType="VARCHAR" />
    <result column="revision" property="revision" jdbcType="INTEGER" />
    <result column="last_modified" property="lastModified" jdbcType="VARCHAR" />
  </resultMap>

  <sql id="Base_Column_List" >
    author_id, source_id, name, personal_name, revision, last_modified
  </sql>

  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from author_t
    where author_id = #{authorId,jdbcType=INTEGER}
  </select>

  <select id="selectAuthorBySourceId" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from author_t
    where source_id = #{sourceId,jdbcType=VARCHAR}
  </select>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from author_t
    where author_id = #{authorId,jdbcType=INTEGER}
  </delete>

  <insert id="insert" parameterType="com.domain.Author" >
    <selectKey resultType="java.lang.Integer" keyProperty="authorId" order="AFTER" >
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into author_t (source_id, name, personal_name, 
      revision, last_modified)
    values (#{sourceId,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{personalName,jdbcType=VARCHAR}, 
      #{revision,jdbcType=INTEGER}, #{lastModified,jdbcType=VARCHAR})
  </insert>

  <insert id="insertSelective" parameterType="com.domain.Author" >
    <selectKey resultType="java.lang.Integer" keyProperty="authorId" order="AFTER" >
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into author_t
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="sourceId != null" >
        source_id,
      </if>
      <if test="name != null" >
        name,
      </if>
      <if test="personalName != null" >
        personal_name,
      </if>
      <if test="revision != null" >
        revision,
      </if>
      <if test="lastModified != null" >
        last_modified,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="sourceId != null" >
        #{sourceId,jdbcType=VARCHAR},
      </if>
      <if test="name != null" >
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="personalName != null" >
        #{personalName,jdbcType=VARCHAR},
      </if>
      <if test="revision != null" >
        #{revision,jdbcType=INTEGER},
      </if>
      <if test="lastModified != null" >
        #{lastModified,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>

  <update id="updateByPrimaryKeySelective" parameterType="com.domain.Author" >
    update author_t
    <set >
      <if test="sourceId != null" >
        source_id = #{sourceId,jdbcType=VARCHAR},
      </if>
      <if test="name != null" >
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="personalName != null" >
        personal_name = #{personalName,jdbcType=VARCHAR},
      </if>
      <if test="revision != null" >
        revision = #{revision,jdbcType=INTEGER},
      </if>
      <if test="lastModified != null" >
        last_modified = #{lastModified,jdbcType=VARCHAR},
      </if>
    </set>
    where author_id = #{authorId,jdbcType=INTEGER}
  </update>

  <update id="updateByPrimaryKey" parameterType="com.domain.Author" >
    update author_t
    set source_id = #{sourceId,jdbcType=VARCHAR},
      name = #{name,jdbcType=VARCHAR},
      personal_name = #{personalName,jdbcType=VARCHAR},
      revision = #{revision,jdbcType=INTEGER},
      last_modified = #{lastModified,jdbcType=VARCHAR}
    where author_id = #{authorId,jdbcType=INTEGER}
  </update>

----------------------------------------------------------------------------------
//springcontext.xml


    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>classpath:springTest/config.properties</value>
        </property>
    </bean>     

    <!-- define the mysql dataSource -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${driverClassName}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />    
    </bean> 

    <!-- transaction manager -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

     <aop:config>  
         <aop:advisor pointcut="execution(* com.service..*.*(..))" advice-ref="txAdvice"/>  
     </aop:config>
     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
         <tx:attributes>  
             <tx:method name="get*" read-only="true"/>  
             <tx:method name="find*" read-only="true"/>  
             <tx:method name="*"  propagation="REQUIRED"/>  
        </tx:attributes>  
     </tx:advice>       

      <!-- enable component scanning and autowire (beware that this does not enable mapper scanning!) -->    
    <context:component-scan base-package="com.service" />

     <!-- define the SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="typeAliasesPackage" value="com.domain" />
        <property name="mapperLocations" value="classpath*:com/dao/*.xml" />
    </bean>

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

    <bean id="productService" class="com.service.ProductService" />

最佳答案

异常表明您的类路径上没有 CGLIB。您应该能够在 CGLIB SourceForge page 获取该 jar .

关于java - 将 daos Autowiring 到服务中,并将该服务 Autowiring 到 Junit 测试类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13140315/

相关文章:

java - 比较两个表中的值

java - 我们如何使列表可编辑?

java - Jersey 和 spring 集成 - bean 注入(inject)在运行时为空

java - 通过 Mybatis 运行时无法识别 SQLite 命令

spring - Web 应用程序启动警告 :No MyBatis mapper was found in . .. ,请检查您的配置

java - 在 SpringBoot 和 MyBatis 应用程序中,对于动态数据源,查询总是先于 AOP 执行

java - 在 Debian 上运行的 Tomcat 上部署 Hibernate Web 应用程序

java - 非法字符 < :> at index 40: com. 识别.app-mergeDebugResources-33 :/values/values. xml)

java - OAuth2RestTemplate 将字符集添加到内容类型 header 中

java - 使用 Spring JDBCTemplate 更新结果集