欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

解决报错 :A component required a bean of type 'gentle.test.Show' that could not be found

程序员文章站 2022-06-02 22:15:46
...

1. 启动工程失败,报错如题:

A component required a bean of type 'gentle.test.Show' that could not be found.

2. 原因:有一个被我注入其它类的 业务类上没有给注解:@service

(PS:还有一种情况为要求 service 和 controller 需要在同一个包下 。)

此类没有在类上给注解:

package gentle.test;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * @author silence
 * @date 2018/7/17 11:37
 */
public class Show implements Job {


    private static Logger _log = LoggerFactory.getLogger(Show.class);

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {

        _log.info("\n\n-------------------------------\n " +
                "It is running and the time is : " + new Date()+
                "\n-------------------------------\n");
    }

}

 在此类注入了上面那个类 Show :

package gentle.test;

import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;


public class UserSyncTask {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    SchedulerTest test;
    @Resource
    Show show;


    public void cronDepartmentsAndUsersJob() {

        logger.info("\n\n 定时--开始,当前时间: " + dateFormat().format(new Date()));
//        test.addJob();

        try {
            show.execute(null);
        } catch (JobExecutionException e) {
            e.printStackTrace();
        }
        logger.info("\n\n 定时--结束,当前时间:" + dateFormat().format(new Date()));
    }

    private SimpleDateFormat dateFormat() {
        return new SimpleDateFormat("HH:mm:ss");
    }
}

3.解决,在 Show 类上加上 service 注解 。

改为:

package gentle.test;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author silence
 * @date 2018/7/17 11:37
 */
@Service("show")
public class Show implements Job {


    private static Logger _log = LoggerFactory.getLogger(Show.class);

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {

        _log.info("\n\n-------------------------------\n " +
                "It is running and the time is : " + new Date()+
                "\n-------------------------------\n");
    }

}

4. 启动并运行定时任务正常:

解决报错 :A component required a bean of type 'gentle.test.Show' that could not be found