2

I'm pretty much stucked and I hope you guys can help me out. Somehow I can't manage to get my spring context initialized.

I have these nice Bean Configuration classes:

@Configuration
public class CoreConfig {

    @Bean
    public TeamService createService(TeamPersistenceService teamPersistenceService) {
    return new TeamEventHandler(teamPersistenceService);
    }
}

And this one:

@Configuration
@EnableJpaRepositories(basePackages = "de.ktv.persistence.repository", //
includeFilters = @ComponentScan.Filter(value = { TeamsRepository.class }, type =                            FilterType.ASSIGNABLE_TYPE))
@EnableTransactionManagement
public class PersistenceConfig {

    @Bean
    public TeamPersistenceService createService(TeamsRepository repository) {
    return new TeamPersistenceEventHandler(repository);
    }
}

And in this test I want to use them:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { CoreConfig.class, PersistenceConfig.class })
public class CoreIntegrationTest {

@Autowired
TeamService teamService;

@Test
public void addNewTeamToTheSystem() {

    //some test

}

The PersistenceConfig.class I am using in a different test and it works fine. But somehow here combined with CoreConfig.class it fails to initialize.

That is the error I get:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [de.ktv.core.services.TeamService] 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)}

I would really appreciate any help/hint.Thanks!

2
  • The problem is in testing? Commented Sep 17, 2014 at 10:11
  • Eventually yes, since the UnitUnderTest(TeamService.class) doesn't get initalized. Commented Sep 17, 2014 at 10:13

2 Answers 2

3

SpringContext cannot bind the @autowired if you don't indicate the same name. By default, the bean name will be the same as the method name, in this case, he is different, 2 options : change the method name or add attribut name !

Option 1

@Bean(name = "teamService")
public TeamService createService(TeamsRepository repository) {
return new TeamPersistenceEventHandler(repository);
}

Option 2

@Bean
public TeamService teamService(TeamsRepository repository) {
return new TeamPersistenceEventHandler(repository);
}

Enjoy \o/

Sign up to request clarification or add additional context in comments.

3 Comments

Can you explain why it helps?
SpringContext cannot bind the @autowired if you don't indicate the same name. By default, the bean name will be the same as the method name, in this case, he is different, 2 options : change the method name or add attribut name
I always thought that autowire use type, not name
1
@Configuration
public class CoreConfig {
    @Autowired
    private TeamPersistenceService teamPersistenceService;

    @Bean
    public TeamService teamService() {
    return new TeamEventHandler(teamPersistenceService);
    }
}

And this one:

@Configuration
@EnableJpaRepositories(basePackages = "de.ktv.persistence.repository", //
includeFilters = @ComponentScan.Filter(value = { TeamsRepository.class }, type =                            FilterType.ASSIGNABLE_TYPE))
@EnableTransactionManagement
public class PersistenceConfig {
    @Autowired
    private TeamsRepository repository:

    @Bean
    public TeamPersistenceService teamPersistenceService() {
    return new TeamPersistenceEventHandler(repository);
    }
}

And in this test I want to use them:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { CoreConfig.class, PersistenceConfig.class })
public class CoreIntegrationTest {

@Autowired
TeamService teamService;

@Test
public void addNewTeamToTheSystem() {

    //some test

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.