How can I qualify an autowired setter that I don&#

2019-04-22 06:07发布

问题:

The gist is that the Spring Batch (v2) test framework has JobLauncherTestUtils.setJob with an @Autowired annotation. Our test suite has multiple Job class providers. Since this class is not something I can modify, I'm not sure how I can qualify which job it gets autowired with, which may be different per test.

 STDOUT [WARN ] [2015.04.15 11:14:42] support.GenericApplicationContext - Exception encountered during context initialization - cancelling refresh attempt
 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobLauncherTestUtilsForSnapshot': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.batch.test.JobLauncherTestUtils.setJob(org.springframework.batch.core.Job); nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.batch.core.Job] is defined: expected single matching bean but found 2: coverageRuleBatch,generateMetricsSnapshotJob

I've tried adding this JavaConfig which is recognized, but the error says it's still autocalling setJob

@Configuration
public class SpringTestConfiguration
{
@Bean
public JobLauncherTestUtils jobLauncherTestUtilsForSnapshot( final Job generateMetricsSnapshotJob )
{
    JobLauncherTestUtils jobLauncherTestUtils = new JobLauncherTestUtils();
    jobLauncherTestUtils.setJob( generateMetricsSnapshotJob );
    return jobLauncherTestUtils;
}
}

note: I don't require a JavaConfig solution, but it'd be nice. Also, I'd like, if possible, to still Autowire fields like JobRepository, as there is only one.

回答1:

My solution, when I ran into the same problem, was to restrict the component-scan so that only a single Job bean is created in the test context.

@Configuration
@ComponentScan(basePackages={
    "com.example.batch.jobs.metrics",  //package where generateMetricsSnapshotJob is the only job
    "com.example.batch.common",
    "..."
})
public class SpringTestConfiguration
{
    @Bean
    public JobLauncherTestUtils jobLauncherTestUtils()
    {
        //generateMetricsSnapshotJob and other requirements will be autowired
        return new JobLauncherTestUtils();
    }
}

You might need to adjust your package structure for this to work.



回答2:

The solution I came up with

@Configuration
public class SpringBatchTestConfiguration
{
@Bean
public static JobLauncherTestUtils jobLauncherTestUtilsForSnapshot()
{
    return new SnapshotJobLauncherTestUtils();
}

public static class SnapshotJobLauncherTestUtils extends JobLauncherTestUtils
{
    @Override
    @Qualifier( "generateMetricsSnapshotJob" )
    public void setJob( final Job job )
    {
        super.setJob( job );
    }
}
}

and in the final test

@Autowired
@Qualifier( "jobLauncherTestUtilsForSnapshot" )
protected JobLauncherTestUtils jobLauncherTestUtils;

fairly confident I could just annotate my TestUtils with @Component and name it properly and do the same thing.



回答3:

Other kind solution is to inject it via setter. I prefer this solution because is clearer and easier.

@Configuration
public class SpringTestConfiguration
{
    @Bean
    public JobLauncherTestUtils jobLauncherTestUtilsForSnapshot()
    {
        return new JobLauncherTestUtils() {
            @Override
            @Autowired
            public void setJob(@Qualifier("generateMetricsSnapshotJob") Job job) {
                super.setJob(job);
            }
        };
    }
}


回答4:

Maybe you could use Spring profiles for that. Assign a different profile to every Job provider bean definition (with the annotation @Profile("profileName"), and then activate the profile for the correct provider on the specific test class with the annotation @ActiveProfiles("profileName").



回答5:

  1. You can extend AutowiredAnnotationBeanPostProcessor and override inject method.

  2. Remove <context:scan .. /> entries

  3. Register your bean <bean class ="a.b.CustomAutowiredAnnotationBeanPostProcessor" />