Spring @ Autowired injection is fine, while @ Resource injection is null

recently set up a ssm project of my own. I used to use @ Autowired to try @ Resource this time. However, injection always fails when using resource, no error is reported at startup, and null pointer exception is reported at run time.

  1. error
    java.lang.NullPointerException
    at TestMyBatis.test3 (TestMyBatis.java:43)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (NativeMethod)
    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.RunBeforeTestMethodCallbacks.evaluate (RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks Br (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:252)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild (SpringJUnit4ClassRunner.java:94)
    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:191)
    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)
< hr >

configuration file


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

<!--  -->
<context:component-scan base-package="com.mineblog"></context:component-scan>

<!-- properties:${url} -->
<context:property-placeholder location="classpath:jdbc.properties"/>

<!--  -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
    <property name="initialSize" value="${jdbc.initialSize}"/>
    <property name="maxActive" value="${jdbc.maxActive}"/>
    <property name="maxIdle" value="${jdbc.maxIdle}"/>
    <property name="minIdle" value="${jdbc.minIdle}"/>
    <property name="maxWait" value="${jdbc.maxWait}"/>
    <property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}"/>
</bean>

<!-- SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!--  -->
    <property name="dataSource" ref="dataSource" />
    <!-- model  -->
    <property name="typeAliasesPackage" value="com.mineblog.pojo"/>
    <!-- mapping.xml -->
    <property name="mapperLocations" value="classpath:mapper/**/*.xml"></property>
</bean>



<!-- DaoDaospring -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!-- sqlSessionFactory -->
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    <!-- Dao -->
    <property name="basePackage" value="com.mineblog.dao"/>
</bean>

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

<!-- @Transactional -->
<tx:annotation-driven transaction-manager="transactionManager"/>

<aop:aspectj-autoproxy />

< / beans >

< hr >


< beans xmlns= "http://www.springframework.org/schema/beans"

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


<context:annotation-config/>

<!-- webbean -->
<context:component-scan base-package="com.mineblog"/>

<!-- SpringMVC -->
<mvc:annotation-driven/>

<!-- servlet -->
<mvc:default-servlet-handler/>

<!-- jsp ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>

< / beans >

< hr >

Application 2.3//EN"  "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">   <display-name>Archetype Created Web Application</display-name>

  <!--  -->   <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>   </filter>   <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>   </filter-mapping>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-mybatis.xml</param-value>   </context-param>


  <!-- DispatcherServlet -->   <servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- springMVC-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-*.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>   </servlet>   <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <!-- -->
    <url-pattern>/</url-pattern>   </servlet-mapping>


  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>   </welcome-file-list> </web-app>
    
    

< hr >

In the

Test class, @ Resource tag does not need to specify name , just use the default, and there is no need to specify value on the ServiceImpl class. Service dependencies in the Test class should use interfaces instead of instance objects, and @ Resource private UserService userService; does not need = null .

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b38ac4-2c126.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b38ac4-2c126.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?