Java 在ApplicationContext中找到Bean,但在尝试自动连线时未找到
我有一个SpringJobLauncher实现,我正在尝试自动连接到一个Java 在ApplicationContext中找到Bean,但在尝试自动连线时未找到,java,xml,spring,spring-batch,Java,Xml,Spring,Spring Batch,我有一个SpringJobLauncher实现,我正在尝试自动连接到一个@Component类中,假设它被称为SimpleComponent。此作业启动器名为SomeJobLauncher,通过bean定义加载到Spring上下文中 如果我在Spring应用程序上下文中自动连接到SimpleComponent,并尝试通过getBean(“someLauncher”)获取bean,那么我就获得了bean,并且没有问题 但是,尽管确认bean已100%加载到ApplicationContext中,但
@Component
类中,假设它被称为SimpleComponent
。此作业启动器名为SomeJobLauncher
,通过bean定义加载到Spring上下文中
如果我在Spring应用程序上下文中自动连接到SimpleComponent
,并尝试通过getBean(“someLauncher”)
获取bean,那么我就获得了bean,并且没有问题
但是,尽管确认bean已100%加载到ApplicationContext中,但当我尝试将someLauncher
自动连接到SimpleComponent
时,我得到以下错误:
No matching bean of type [com.somepackage.SomeJobLauncher] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
下面是几个相关的类。由于这个应用程序的巨大规模,我无法显示每个xml配置文件/Java类,但是如果您想看到一些特定的内容,我愿意接受请求
那么,有没有人知道这是怎么可能的
public class SomeJobLauncher extends BatchScheduleJobLauncher {
private static final long serialVersionUID = 89457928374294789723L;
/**
* Spring inject the appropriate Server name from the Batch properties file.
*/
@Value("${dataload.schedule.server}")
private String jobSchedulerServer;
/**
* No-argument constructor.
*/
public SomeJobLauncher() {
}
@Override
protected String supplyScheduleServer() {
return this.jobobSchedulerServer;
}
}
父类:
public abstract class BatchScheduleJobLauncher extends SimpleJobLauncher implements Serializable {
private static final long serialVersionUID = -1138294811959957159L;
private String batchJobIdentifier = "not set";
/**
* Set by Spring property setting from the specific JobConfig.xml file inside the
* WEB project.
*/
private Job job;
/**
* Set by Spring property setting from the specific JobConfig.xml file inside the
* WEB project.
*/
private JobLauncher jobLauncher;
/**
* Set by Spring injection from within the descendent's implementation of the supplyScheduleServer
* method.
*/
private String jobSchedulerServer;
/**
* No argument constructor
* Default Constructor
*/
public BatchScheduleJobLauncher() {
}
/**
* Spring invoked method.
*
* @return org.springframework.batch.core.Job - Interface
*/
public Job getJob() {
return this.job;
}
/**
* Spring invoked method.
*
* @return org.springframework.batch.core.launch.JobLauncher - Interface
*/
public JobLauncher getJobLauncher() {
return this.jobLauncher;
}
/**
* Method invoked from the corresponding Job Config XML file in the WEB
* project or from the "Upon Demand" manual invocation process.
*
* @return JobExecution - see internal "Status" for Job Completion
* indication.</br> Status set by this method is either
* BatchStatus.ABANDONED, BatchStatus.COMPLETED or
* BatchStatus.FAILED.
*/
public JobExecution launch() {
JobExecution returnValue;
String timeParameter = "time";
long systemTime = System.currentTimeMillis();
JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
JobParameters jobParameters;
String ipAddressDefinedServer;
ExitStatus exitStatus;
// Build System Time Job parameter.
jobParametersBuilder.addLong(timeParameter, systemTime);
jobParameters = jobParametersBuilder.toJobParameters();
this.batchJobIdentifier = supplyJobIdentifier();
this.jobSchedulerServer = supplyScheduleServer();
try {
if (Util.isNullOrEmpty(this.jobSchedulerServer)) {
LoggerUtil.error("Batch Job Scheduler Server NOT set on BatchScheduleJobLauncher ancestor class for the [" +
this.batchJobIdentifier + "] Batch Load Job so 'run' could not be executed.");
returnValue = new JobExecution(new JobInstance(new Long(0),
jobParameters,
supplyJobIdentifier()));
exitStatus = new ExitStatus(ExitStatus.FAILED.getExitCode());
returnValue.setExitStatus(exitStatus);
returnValue.setEndTime(new Date());
returnValue.setStatus(BatchStatus.ABANDONED);
} else {
ipAddressDefinedServer = InetAddress.getLocalHost().getHostName();
if (this.jobSchedulerServer.equalsIgnoreCase(ipAddressDefinedServer)) {
// job scheduled server found.
LoggerUtil.info("Executing 'run' from inside the launch method of the BatchScheduleJobLauncher object.");
returnValue = this.jobLauncher.run(this.job, jobParameters);
} else {
LoggerUtil.warn("Batch Job Scheduler Server [" + this.jobSchedulerServer +
"] does NOT match the server name [" + ipAddressDefinedServer +
"] found at IP Address [" + InetAddress.getLocalHost() +
"] so 'run' could not be executed.");
returnValue = new JobExecution(new JobInstance(new Long(0),
jobParameters,
supplyJobIdentifier()));
exitStatus = new ExitStatus(ExitStatus.FAILED.getExitCode());
returnValue.setExitStatus(exitStatus);
returnValue.setEndTime(new Date());
returnValue.setStatus(BatchStatus.ABANDONED);
}
}
} catch (Exception e) {
LoggerUtil.error("ERROR while running the BatchScheduleJobLauncher for the [" +
this.batchJobIdentifier + "] Batch Load Job. Error is [" + e.getMessage()+ "].");
returnValue = new JobExecution(new JobInstance(new Long(0),
jobParameters,
supplyJobIdentifier()));
returnValue.addFailureException(e);
returnValue.setStatus(BatchStatus.FAILED);
}
return returnValue;
}
/**
* Called by Spring based on the property attributes in the corresponding
* Job Config XML file.</br> Sets {@link #job}.
*
* @param org.springframework.batch.core.Job - Interface
*/
public void setJob(Job inJob) {
this.job = inJob;
}
/**
* Called by Spring based on the property attributes in the corresponding
* Job Config XML file.</br> Sets {@link #jobLauncher}.
*
* @param org.springframework.batch.core.Job - Interface
*/
public void setJobLauncher(JobLauncher inJobLauncher) {
this.jobLauncher = inJobLauncher;
}
/**
* Must be implemented by the descendant Job Launcher job to define the type
* of Batch job being run.</br> Sets {@link #batchJobIdentifier}.
*
* @return String - the batch job type.
*/
abstract protected String supplyJobIdentifier();
/**
* Must be implemented by the descendant Job Launcher job to define the
* specific schedule Server name.</br> The appropriate value will be Spring
* injected (see someJobLauncher.java).</br> Sets {@link #jobSchedulerServer}.
*
* @return String - the scheduler server name.
*/
abstract protected String supplyScheduleServer();
}
公共抽象类BatchScheduleJobLauncher扩展SimpleJobLauncher实现可序列化{
私有静态最终长serialVersionUID=-1138294811959957159L;
私有字符串batchJobIdentifier=“未设置”;
/**
*通过Spring属性设置从
*网络项目。
*/
私人工作;
/**
*通过Spring属性设置从
*网络项目。
*/
私有JobLauncher JobLauncher;
/**
*由Spring注入在supplyScheduleServer的子代实现中设置
*方法。
*/
私有字符串jobSchedulerServer;
/**
*无参数构造函数
*默认构造函数
*/
公共BatchScheduleJobLauncher(){
}
/**
*Spring调用了这个方法。
*
*@return org.springframework.batch.core.Job-界面
*/
公共作业getJob(){
归还这份工作;
}
/**
*Spring调用了这个方法。
*
*@return org.springframework.batch.core.launch.JobLauncher-界面
*/
PublicJobLauncher getJobLauncher(){
返回此.jobLauncher;
}
/**
*从WEB中相应的作业配置XML文件调用的方法
*项目或“按需”手动调用过程。
*
*@return JobExecution-有关作业完成情况,请参阅内部“状态”
*指示。此方法设置的状态为
*BatchStatus.Forwarded、BatchStatus.COMPLETED或
*BatchStatus.0失败。
*/
公共工作执行启动(){
作业执行返回值;
字符串timeParameter=“时间”;
long systemTime=System.currentTimeMillis();
JobParametersBuilder JobParametersBuilder=新的JobParametersBuilder();
作业参数作业参数;
字符串ipAddressDefinedServer;
退出状态退出状态;
//生成系统时间作业参数。
jobParametersBuilder.addLong(时间参数,系统时间);
jobParameters=jobParametersBuilder.toJobParameters();
this.batchJobIdentifier=supplyJobIdentifier();
this.jobSchedulerServer=supplyScheduleServer();
试一试{
if(Util.isNullOrEmpty(this.jobSchedulerServer)){
LoggerUtil.error(“未在[]的BatchScheduleJobLauncher祖先类上设置批处理作业计划程序服务器”+
this.batchJobIdentifier+“]批量加载作业,因此无法执行“运行”。);
returnValue=newjobExecution(newjobInstance(new Long)(0),
作业参数,
supplyJobIdentifier());
exitStatus=newexitstatus(exitStatus.FAILED.getExitCode());
returnValue.setExitStatus(exitStatus);
returnValue.setEndTime(新日期());
returnValue.setStatus(批处理状态.已放弃);
}否则{
ipAddressDefinedServer=InetAddress.getLocalHost().getHostName();
如果(this.jobSchedulerServer.equalsIgnoreCase(ipAddressDefinedServer)){
//找到作业计划服务器。
info(“从BatchScheduleJobLauncher对象的启动方法内部执行'run'”);
returnValue=this.jobLauncher.run(this.job,jobParameters);
}否则{
LoggerUtil.warn(“批处理作业计划程序服务器[”+this.jobSchedulerServer+
“]与服务器名称[“+ipAddressDefinedServer”不匹配+
“]在IP地址[”+InetAddress.getLocalHost()处找到+
“]因此无法执行“运行”。);
returnValue=newjobExecution(newjobInstance(new Long)(0),
作业参数,
supplyJobIdentifier());
exitStatus=newexitstatus(exitStatus.FAILED.getExitCode());
returnValue.setExitStatus(exitStatus);
returnValue.setEndTime(新日期());
returnValue.setStatus(批处理状态.已放弃);
}
}
}捕获(例外e){
LoggerUtil.error(“为[]运行BatchScheduleJobLauncher时出错”+
this.batchJobIdentifier+“]批量加载作业。错误为[“+e.getMessage()+”]”;
returnValue=newjobExecution(newjobInstance(new Long)(0),
作业参数,
supplyJobIdentifier());
复述
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<tx:annotation-driven proxy-target-class="true" />
<bean id="transactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
<property name="transactionManager" ref="transactionManager" />
</bean>
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<tx:annotation-driven proxy-target-class="true" />
<!-- Multiple threading not needed so set pool size to one. -->
<task:scheduler id="scheduler" pool-size="1" />
<bean id="someLoadJob" class="com.somepackage.SomeLoadJob" />
<!-- Once Excel File is found on the Load Job landing zone the Batch Load will be run -->
<!-- This Job Instance must always run as a new Job. -->
<batch:job id="processLoadJob" restartable="false">
<batch:step id="processLoadJobStep">
<batch:tasklet ref="LoadJob" />
</batch:step>
</batch:job>
<bean id="someLauncher" class="com.somepackage.SomeJobLauncher">
<property name="jobLauncher" ref="jobLauncher"></property>
<property name="jobRepository" ref="jobRepository"></property>
<property name="job" ref="processLoadJob"></property>
</bean>
<task:scheduled-tasks scheduler="scheduler">
<task:scheduled ref="someLauncher" method="launch" cron="${batch.scheduler.time}"/>
</task:scheduled-tasks>
</beans>
@Component
public class SimpleComponent {
private Map<String, JobCategoryStatus> jobStatuses = new HashMap<String, JobCategoryStatus>();;
private Map<String, List<JobCategoryStatus>> categoryStatuses = new HashMap<String, List<JobCategoryStatus>>();
// public SimpleComponent() { };
@Autowired
private ApplicationContext ctx;
//This autowire doesn't work
@Autowired
private SomeJobLauncher someLauncher;
//This method works
public SomeJobLauncher getSomeLauncher() {
Object someLauncher = ctx.getBean("someLauncher");
return someLauncher;
}
}