나는 스프링 부트 응용 프로그램이 Repository
(s)입니다.왜 내 서비스에서 @Autowired 서비스를 직접 사용할 수 없으며 왜 @Autowired 리포 지 토리가 필요합니까?
@Service
도 사용하고 repository
을 확장하십시오. 나는 서비스를 @Autowired하려고하면
나는이 없다 : org.springframework.beans.factory.NoSuchBeanDefinitionException :
에 의한 유형 'com.api.core.service.CountryService'의 어떠한 자격 콩을 available : 적어도 하나의 bean이 autowire 후보가 될 것으로 예상된다. 종속성 주석 : 나는 저장소를
@Autowired
경우 잘 작동
{org.springframework.beans.factory.annotation.Autowired @는 (= TRUE 필수)}.
내 시험은 다음과 같이해야합니다
@SpringBootTest
@ActiveProfiles("core")
@ContextConfiguration(classes = { UserManagementConfig.class, UserManagementServiceConfig.class })
@UserManagementTx
@RunWith(SpringRunner.class)
public class BlogPostContentImplTest {
@Autowired
private CountryService countryService;
@Autowired
private BlogPostContentRepository blogPostContentRepository;
private BlogPostContentServiceImpl blogPostContentService;
private BlogPostContent entity = new BlogPostContent();
@Before
public void setUp() throws Exception {
blogPostContentService = new BlogPostContentServiceImpl(blogPostContentRepository);
List<Country> countryList = countryService.findAll(null);
entity = new BlogPostContent();
entity.setId(1L);
entity.setDescription("test");
entity.setCountry(countryList.get(0));
blogPostContentService.insert(entity);
}
@After
public void tearDown() throws Exception {
blogPostContentService.delete(entity.getId());
}
@Test
public void findAll() throws Exception {
assertThat(blogPostContentService.findAll(null).size()).isGreaterThan(0);
}
}
이것은 내가 컨텍스트 구성하는 방법입니다 : 당신이 게시 된 코드를 기반으로
@Configuration
@ComponentScan({
UserManagementConfig.CONTEXT_CLASSPATH
})
public class UserManagementConfig {
public static final String CONTEXT_CLASSPATH = "com.api.userManagement.config.context.**";
}
@Configuration
@ComponentScan({
UserManagementServiceConfig.CLASSPATH,
})
public class UserManagementServiceConfig {
public static final String CLASSPATH = "com.api.userManagement.service.**";
}
annotate'CountryService' 클래스 다음과 같이 테스트에 구성되어 아래처럼
TestContext
클래스를 만들 수 있습니다, 테스트하려면' @ 컴포넌트 '? – flakes@flakes 트랜잭션 문서 인'@Service'와'@Service'도 스프링 문서에 따라'@ ComponentScan'에 잡혀 있습니다. – BigDong