首页 新闻 搜索 专区 学院

SSM框架中自己写了一个工具包,在此包得类中注入Mapper接口是null,

0
[已解决问题] 解决于 2018-05-07 11:33

这是工具类

 

这是定时任务包

 

是在定时包中调用工具类里得方法来处理数据库数据,

在工具类中需要注入Mapper接口来操作数据库,但注入得是null;

public class Close_data 
{
    @Autowired
    public CategoryMapper categoryMapper;
    
    public void delete()
    {
        System.out.println("categoryMapper : "+categoryMapper);
        //类别偏好
        categoryMapper.delete1();
        //借还书信息,评分
        categoryMapper.delete2();
        //聚类号,分组
        categoryMapper.delete3();
        //邻居信息
        categoryMapper.delete4();
        //创建借还书表信息,评分初始化为0
        categoryMapper.add1();
    }
}
注入接口得类

用tomcat启动后报的错误信息

我打印了下mapper是null

categoryMapper : null
ERROR [scheduler-1] - Unexpected error occurred in scheduled task.
java.lang.NullPointerException
    at com.how2java.tools.Close_data.delete(Close_data.java:18)
    at com.how2java.job.TimerTask.test2(TimerTask.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.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:64)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:53)
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(Unknown Source)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
报错信息
问题补充:
<?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:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    
    

   <context:annotation-config />
     <context:component-scan base-package="com.how2java.service,com.how2java.mapper" />
    <!--<context:component-scan base-package="com.how2java" /> -->
    

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
      <property name="driverClassName">  
          <value>com.mysql.jdbc.Driver</value>  
      </property>  
      <property name="url">  
          <value>jdbc:mysql://localhost:3306/library?characterEncoding=UTF-8</value>  
    
      </property>  
      <property name="username">  
          <value>root</value>  
      </property>  
      <property name="password">  
          <value>admin</value>  
      </property>      
    </bean>
    
    
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="typeAliasesPackage" value="com.how2java.pojo" />
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:com/how2java/mapper/*.xml"/>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.how2java.mapper"/>
    </bean>
    


</beans>
配置信息applicationContext.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">



    <context:annotation-config/>

    <context:component-scan base-package="com.how2java.controller">
          <context:include-filter type="annotation" 
          expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

   <mvc:annotation-driven >
       <mvc:message-converters register-defaults="true">
          <bean class="org.springframework.http.converter.StringHttpMessageConverter">
             <property name="supportedMediaTypes" value="text/plain;charset=UTF-8" />
          </bean>
       </mvc:message-converters>   
    </mvc:annotation-driven>
    
    <mvc:default-servlet-handler />


    <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>
<?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" 
         xmlns:web="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_2_5.xsd" version="2.5">
    
    <!-- spring的配置文件-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        classpath:applicationContext.xml
        classpath:spring-task.xml
        </param-value>
        
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    
    <!-- spring mvc核心:分发servlet -->
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- spring mvc的配置文件 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springMVC.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
</web-app>
配置信息web.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:context="http://www.springframework.org/schema/context"  
       xmlns:task="http://www.springframework.org/schema/task"  
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"  
       default-autowire="byName" default-lazy-init="false">  
    <!--配置包扫描-->  
    <context:component-scan base-package="com.how2java.job" />  
  
    <!-- 定时任务配置 scheduler 方式 注解 -->  
    <task:executor id="executor" pool-size="5"/>  
    <task:scheduler id="scheduler" pool-size="10"/>  
    <task:annotation-driven executor="executor" scheduler="scheduler"/>  
  
</beans>  
配置信息spring-task.xml
窦光大大存大大凯的主页 窦光大大存大大凯 | 菜鸟二级 | 园豆:220
提问于:2018-05-04 10:18
< >
分享
最佳答案
0

你的CategoryMapper 所在包有没有被扫描?类似于这种配置

<mybatis:scan base-package="mapper所在包路径" />

奖励园豆:5
让我发会呆 | 老鸟四级 |园豆:2638 | 2018-05-04 16:11

写了呀,我把那个配置文件也上传了,第一个,你点展开看看,,我怀疑是spring配置文件加载问题.junit单元测试可以,

窦光大大存大大凯 | 园豆:220 (菜鸟二级) | 2018-05-04 16:16

@窦光大大存大大凯: 你这个tools所在的包应该也要被扫描的把,不然他也是没办法注入的

让我发会呆 | 园豆:2638 (老鸟四级) | 2018-05-04 16:25

@让我发会呆: 试了下,不行,还是空

窦光大大存大大凯 | 园豆:220 (菜鸟二级) | 2018-05-04 16:41

@窦光大大存大大凯: 我本地试了下,再工具类里确实没办法用注解的方式注入mapper,我现在也不知道是哪儿配置有问题,但是你可以试试这种方式注入:

SpringApplicationContext.getApplicationContext().getBean(CategoryMapper.class)

让我发会呆 | 园豆:2638 (老鸟四级) | 2018-05-07 09:33

@窦光大大存大大凯: https://www.cnblogs.com/acm-bingzi/p/springBean.html,看看这篇文章,应该能理解了。

让我发会呆 | 园豆:2638 (老鸟四级) | 2018-05-07 10:45

@让我发会呆: 谢谢了,好像是这样得,但是你这个SpringApplicationContext,我是没法用的,这是springboot吗,我是根据你的提议,百度如何解决多线程时Autowired自动注入问题,https://blog.csdn.net/u011493599/article/details/78522315,,解决了 谢谢了 ,非常感谢!

窦光大大存大大凯 | 园豆:220 (菜鸟二级) | 2018-05-07 11:32

@让我发会呆: 谢谢

窦光大大存大大凯 | 园豆:220 (菜鸟二级) | 2018-05-07 11:33

@窦光大大存大大凯: 这个SpringApplicationContext就是Spring,不是springboot,我系统中封装了一层,其实就是ApplicationContext.getBean(beanName);

让我发会呆 | 园豆:2638 (老鸟四级) | 2018-05-07 11:51
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册