728x90
반응형

 

Spring Boot 에서는 @ 로 시작하는 어노테이션을 많이 사용하게 됩니다.

 

자동으로 설정을 위해서인데 내부 소스에 축약되다보니 이해하는데 많은 불편함은 있습니다.

 

하지만 한두번 사용하다 보면 정말 편하구나라는게 더 크죠.

 

Spring Boot 는 XML의 설정보다 Java 소스 설정을 선호한다고합니다.

 

이번엔 @Cofiguration 을 알아보겠습니다.

 

 

 

 

 

 

Eclipse 프로그램을 사용하여 Spring Stater Project 를 생성했습니다.

 

config 라는 package를 생성해주고 안에 ExampleService.java 를 만들었습니다.

 

package com.gigas.config;

public class ExampleService {

}

 

 

 

다른 설정없으 src/test/java 안에있는 SpringBootTestApplicationTests.java 로 테스트 해보겠습니다.

 

 

 

@Autowired 를 통해 만들어둔 ExampleService를 의존주입 했습니다.

 

contextLoads() 안에서 assertNotNull() 함수의 인자로 exalpleService 변수를 넣고 값이 비었는지 테스트를 진행합니다.

package com.gigas;

import static org.junit.Assert.assertNotNull;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.gigas.config.ExampleService;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootTestApplicationTests {

    @Autowired
    ExampleService exampleService;
    
    @Test
    public void contextLoads() {
        
        assertNotNull(exampleService);
    }

}
 

 

 

 

 

SpringBootTestApplicationTests.java 마우스 우클릭하여 JUnit Test 를 실행합니다.

 

 

 

 

테스트 결과는 당연히 Error 입니다.

 

 

 

오류 내용을 보면 유효한 Bean이 없다고 나옵니다.

 

즉 Bean이라고 설정한 파일이 아닌데 @AutoWired 어노테이션을 통해 의존주입을 하려고 해서입니다.

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.gigas.SpringBootTestApplicationTests': Unsatisfied dependency expressed through field 'exampleService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.gigas.config.ExampleService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:391)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:119)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:44)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.gigas.config.ExampleService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1654)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1213)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1167)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
    ... 29 more
 
 

 

 

ExampleService 에 @Configuration 어노테이션을 추가합니다.

 

이 어노테이션으로 Bean을 자바에서 등록해줄수 있습니다.

package com.gigas.config;

import org.springframework.context.annotation.Configuration;

@Configuration
public class ExampleService {

}
 

 

 

 

다시 JUnit Test 를 진행합니다.

 

정상적으로 Bean이 등록되어있는 ExampleService 는 @AutoWired 를 통해 의존주입니 정상적으로 된것을 확인할 수 있습니다.

 

 

728x90
반응형

+ Recent posts