无法在扩展的java类中自动装配服务,抛出NullPointerException

时间:2016-02-24 17:53:41

标签: java spring

我正在尝试AutowireTimerTask扩展的java类中提供服务。尝试在java类中返回服务时返回null值。这是我正在尝试Autowire服务的课程:

@Component
public class Task extends TimerTask

{
    @Autowired
    FileDetailsService fileDetailsService;

    int count = 1;

    @Override
    public void run()
    {   
        fileDetailsService.updateProcessingStatus(fileAudit);
}

配置类:没有web.xml .....我使用spring 4在java中配置

// DataConfig.java

 @Configuration
    @MapperScan("com.fileC.mapper")
    public class DataConfig {
        @Bean
        public DataSource dataSource() {
            SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
            dataSource.setDriverClass(oracle.jdbc.driver.OracleDriver.class);
            dataSource.setUsername("username");
            dataSource.setUrl("jdbc***thin**sample **url");
            dataSource.setPassword("****");
            return dataSource;
        }

        @Bean
        public DataSourceTransactionManager transactionManager() {
            return new DataSourceTransactionManager(dataSource());
        }

        @Bean
        public SqlSessionFactoryBean sqlSessionFactory() throws Exception {
            SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
            sessionFactory.setDataSource(dataSource());
            sessionFactory.setTypeAliasesPackage("com.fileC.model");
            return sessionFactory;
        }

// ApplConfig.java

 @Configuration
    @ComponentScan(basePackages="com.filec")
    public class ApplConfig {
        @Bean
        public CommonsMultipartResolver multipartResolver(){
            CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
            commonsMultipartResolver.setDefaultEncoding("utf-8");
            commonsMultipartResolver.setMaxUploadSize(50000000);
            return commonsMultipartResolver;
        }
    }

我正在使用Spring4,java1.8,ibatis,SQL数据库。

这是服务类

@Service("fileDetailsService")
@Transactional
public class FileDetailsServiceImpl implements FileDetailsService{
    private static Logger logger = LoggerFactory.getLogger(FileDetailsServiceImpl.class);

    @Autowired
    FileDetailsMapper fileDetailsMapper;

    @Override
    public void insertFileInfo(Details details){
        fileDetailsMapper.insertDetails(details);
    }

以下是例外情况,

fileAuditMapper>>>>空
com.filecompare.service.Task.run中线程“Timer-9”java.lang.NullPointerException中的异常(Task.java:117) 在java.util.TimerThread.mainLoop(Timer.java:555) 在java.util.TimerThread.run(Timer.java:505)

请告诉我是否需要在配置中添加内容。

0 个答案:

没有答案