我的配置类
package io.ipl.amaresh.ipldashboard.data;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import io.ipl.amaresh.data.JobCompletionNotificationListener;
import io.ipl.amaresh.ipldashboard.model.Match;
@Configuration
@EnableBatchProcessing
public class BatchConfig {
private final String[] FIELD_NAMES = new String[] { "id", "city", "date", "player_of_match", "venue",
"neutral_venue", "team1", "team2", "toss_winner", "toss_decision", "winner", "result", "result_margin",
"eliminator", "method", "umpire1", "umpire2" };
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Bean
public FlatFileItemReader<MatchInput> reader() {
return new FlatFileItemReaderBuilder<MatchInput>().name("MatchItemReader")
.resource(new ClassPathResource("match-data.csv")).delimited().names(FIELD_NAMES)
.fieldSetMapper(new BeanWrapperFieldSetMapper<MatchInput>() {
{
setTargetType(MatchInput.class);
}
}).build();
}
@Bean
public MatchDataProcessor processor() {
return new MatchDataProcessor();
}
@Bean
public JdbcBatchItemWriter<Match> writer(DataSource dataSource) {
return new JdbcBatchItemWriterBuilder<Match>()
.itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
.sql("INSERT INTO match (id, city, date, player_of_match, venue, team1, team2, toss_winner, toss_decision, match_winner, result, result_margin, umpire1, umpire2) "
+ " VALUES (:id, :city, :date, :playerOfMatch, :venue, :team1, :team2, :tossWinner, :tossDecision, :matchWinner, :result, :resultMargin, :umpire1, :umpire2)")
.dataSource(dataSource).build();
}
@Bean
public Job importUserJob(JobCompletionNotificationListener listener, Step step1) {
return jobBuilderFactory
.get("importUserJob")
.incrementer(new RunIdIncrementer())
.listener(listener)
.flow(step1)
.end()
.build();
}
@Bean
public Step step1(JdbcBatchItemWriter<Match> writer) {
return stepBuilderFactory
.get("step1")
.<MatchInput, Match>chunk(10)
.reader(reader())
.processor(processor())
.writer(writer)
.build();
}
}
我的Listener类
package io.ipl.amaresh.data;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.listener.JobExecutionListenerSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import io.ipl.amaresh.ipldashboard.model.Team;
@Component
public class JobCompletionNotificationListener extends JobExecutionListenerSupport {
private static final Logger log = LoggerFactory.getLogger(JobCompletionNotificationListener.class);
private final EntityManager em;
@Autowired
public JobCompletionNotificationListener(EntityManager em) {
this.em = em;
}
@Override
@Transactional
public void afterJob(JobExecution jobExecution) {
if(jobExecution.getStatus() == BatchStatus.COMPLETED) {
log.info("!!! JOB FINISHED! Time to verify the results");
Map<String, Team> teamData = new HashMap<>();
em.createQuery("select m.team1, count(*) from Match m group by m.team1", Object[].class)
.getResultList()
.stream()
.map(e -> new Team((String) e[0], (long) e[1]))
.forEach(team -> teamData.put(team.getTeamName(), team));
em.createQuery("select m.team2, count(*) from Match m group by m.team2", Object[].class)
.getResultList()
.stream()
.forEach(e -> {
Team team = teamData.get((String) e[0]);
team.setTotalMatches(team.getTotalMatches() + (long) e[1]);
});
em.createQuery("select m.matchWinner, count(*) from Match m group by m.matchWinner", Object[].class)
.getResultList()
.stream()
.forEach(e -> {
Team team = teamData.get((String) e[0]);
if (team != null) team.setTotalWins((long) e[1]);
});
teamData.values().forEach(team -> em.persist(team));
teamData.values().forEach(team -> System.out.println(team));
}
}
}
说明:
io中importUserJob方法的参数0。ipl。阿马雷什。IPL仪表板。数据BatchConfig需要“io”类型的bean。ipl。阿马雷什。数据找不到JobCompletionNotificationListener“”。
措施:
考虑在配置中定义io.ipl.amaresh.data.JobCompletionNotificationListener类型的bean。
这是因为您在配置中声明需要以下行中的作业完成通知侦听器(JobCompletionNotificationListener):
@Bean
public Job importUserJob(JobCompletionNotificationListener listener, ...)
但您从未声明如何在配置中构建/检索这样的组件。
根据Spring的使用情况,您需要在上下文中包括JobCompletionNotificationListener:explicit(显式)Bean(包扫描)。。
编辑:请注意,自动检测在这里不起作用,因为与组件bean相比,您的批配置位于子包中。AFAIKSpring自动检测仅在当前软件包中显示。
我试图在Spring Boot中创建一个简单的REST服务。在我使用CrudRepository之前,一切都很好。现在我得到了这个错误- ***应用程序启动失败 描述: 公司中的现场er。Spring靴。io。受雇者EmployeeService需要“company”类型的bean。Spring靴。io。受雇者找不到EmployeeRepo“”。 措施: 考虑定义一个“company”类型的bea
我正在做SpringBoot项目,并遵循一些测试SpringBoot的说明。 当我尝试将mysql DB与项目连接时,服务找不到映射器。 我不知道为什么它找不到映射器... 这是服务代码和 这是映射程序代码 下面的错误是 我将发布我的包裹设置的图片。。。
应用程序启动失败 考虑在您的配置中定义一个类型为'com.service.adminService'的bean。
下午好,我正在尝试使用Spring Boot运行SOAP服务,但出现以下错误: 应用程序启动失败 描述: 我知道也许这应该是个愚蠢的错误,但我看不出来 附件MI代码: SpringBootSoapApp.java ClienteServiceImpl.java client.xsd 应用程序.属性 我的项目结构 我试图遵循以下示例:https://www.javaspringclub.com/pu