前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。
1. 启动工程失败,报错如题:
A component required a bean of type 'gentle.test.Show' that could not be found.
2. 原因:有一个被我注入其它类的 业务类上没有给注解:@service 。
PS:还有2种原因1) 要求 service 和 controller 需要在同一个包下 。2)有可能没有引入依赖的服务bean。( 此条来自评论中好心网友的分享。)
此类没有在类上给注解:
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);@Overridepublic 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());@ResourceSchedulerTest test;@ResourceShow 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);@Overridepublic void execute(JobExecutionContext arg0) throws JobExecutionException {_log.info("\n\n-------------------------------\n " +"It is running and the time is : " + new Date()+"\n-------------------------------\n");}}
4. 启动并运行定时任务正常: