我有一些方法可以使用我的公共界面
@Repository
public interface JobHistoryRepository extends CrudRepository<JobHistory, Long> {
List<JobHistory> findByJobGroup(String jobGroup);
JobHistory findById (Integer id);
List<JobHistory> findByStateAndJobGroup(String state, String jobGroup);
JobHistory findByJobNameAndJobGroupAndIdTrigger (String jobName, String jobGroup, String idTrigger);
}
我想在myclass服务中使用它
@Service
public class JobHistoryService {
@Autowired
private JobHistoryRepository jobHistoryRepository;
public void setReadyJob(String jobId, String tenant, String idTrigger) {
JobHistory jobHistory = new JobHistory();
jobHistory.setJobName(jobId);
jobHistory.setJobGroup(tenant);
jobHistory.setIdTrigger(idTrigger);
jobHistory.setIdUser("12345");
jobHistory.setState("Ready");
jobHistory.setDateCreation(new Date());
jobHistoryRepository.save(jobHistory); <--- at this point jobHistoryrespository is null why?
}
编辑:
我已经在类中添加了@service
和@repository
注释,但这仍然无法正常工作。每次我使用JobHistoryService方法时,jobRepository依赖项仍为null。
让我知道是否需要添加更多信息。 谢谢。
答案 0 :(得分:0)
JobHistoryService
也应该是Spring bean。对其进行注释,例如与@Service
。
@Service
public class JobHistoryService {
...
}
答案 1 :(得分:0)
假设您的界面是这样的:
public interface JobHistoryRepository extends CrudRepository<JobHistory, Long> {
List<JobHistory> findByJobGroup(String jobGroup);
JobHistory findById (Integer id);
List<JobHistory> findByStateAndJobGroup(String state, String jobGroup);
JobHistory findByJobNameAndJobGroupAndIdTrigger (String jobName, String jobGroup, String idTrigger);
}
然后创建一个服务类,如:
@Service
public class JobHistoryService {
private final JobHistoryRepository jobHistoryRepository;
public JobHistoryService (final JobHistoryRepository jobHistoryRepository)
{
this.jobHistoryRepository = jobHistoryRepository;
}
public void setReadyJob(String jobId, String tenant, String idTrigger) {
JobHistory jobHistory = new JobHistory();
jobHistory.setJobName(jobId);
jobHistory.setJobGroup(tenant);
jobHistory.setIdTrigger(idTrigger);
jobHistory.setIdUser("12345");
jobHistory.setState("Ready");
jobHistory.setDateCreation(new Date());
jobHistoryRepository.save(jobHistory);
}
希望这会有所帮助。
答案 2 :(得分:0)
尝试使用此注释
@ComponentScan(basePackages = {"com.blabla.example.*"})
在Spring Boot Application Class上,并检查该路径上是否包含您的存储库接口。
我的例子:
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan(basePackages = {"com.blabla.gg.*"})
public class MyApplication extends SpringBootServletInitializer implements WebApplicationInitializer{
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}