2017-09-12 1 views
0

스프링 부트로 스프링 웹 애플리케이션을 만들려고합니다. 의존성 주입이 나를 위해 작동하지 않는다는 나의 첫 번째 문제. 이것은 제가 뒤따른 것입니다 article입니다.스프링 부트에서 종속성 삽입을 정의하는 방법은 무엇입니까?

@RestController 
public class GreetingController { 

    @Autowired 
    WfExampleDao wfExampleDao; 

    private static final String template = "Hello, %s!"; 
    private final AtomicLong counter = new AtomicLong(); 

    @RequestMapping("/greeting") 
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) { 
     wfStartDao.insert(null); 
     return new Greeting(counter.incrementAndGet(), 
          String.format(template, name)); 
    } 
} 

내 exampledao :

public interface WfExampleDao { 
    public void insert(WfExample wfExample); 
} 

및 인터페이스의 내 구현 :

@Component 
public class WfExampleDaoImpl extends JdbcDaoSupport implements WfExampleDao { 

    private Logger logger; 

    public WfExampleDaoImpl() { 
     this.logger = LoggerFactory.getLogger(this.getClass()); 
    } 

    @Override 
    public void insert(WfExample wfExample) { 
     logger.info("Insert is not implemented yet."); 
    } 
} 
내가 컨트롤러를 만든 다음

@SpringBootApplication 
public class Application extends SpringBootServletInitializer { 

    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
     return application.sources(Application.class); 
    } 

    public static void main(String[] args) throws Exception { 
     SpringApplication.run(Application.class, args); 
    } 
} 

:

나는 응용 프로그램 클래스를 생성

내 Gradle을 파일 :

buildscript { 
    repositories { 
     mavenCentral() 
    } 
    dependencies { 
     classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.6.RELEASE") 
    } 
} 

apply plugin: 'java' 
apply plugin: 'eclipse' 
apply plugin: 'idea' 
apply plugin: 'org.springframework.boot' 
apply plugin: 'war' 

war { 
    baseName = 'gs-rest-service' 
    version = '0.1.0' 
} 

jar { 
    baseName = 'gs-rest-service' 
    version = '0.1.0' 
} 

repositories { 
    mavenCentral() 
} 

sourceCompatibility = 1.8 
targetCompatibility = 1.8 

dependencies { 
    compile group: 'org.springframework', name: 'spring-context', version: '4.3.11.RELEASE' 
    compile 'org.springframework:spring-jdbc:4.3.11.RELEASE' 

    compile 'commons-dbcp:commons-dbcp:1.4' 
    compile 'commons-pool:commons-pool:1.6' 


    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.5.6.RELEASE' 
    providedRuntime group: 'org.springframework.boot', name: 'spring-boot-starter-tomcat', version: '1.5.6.RELEASE' 
    testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '1.5.6.RELEASE' 
} 

내가 무엇을 기대 : 나는/인사 페이지를 열 때, 로그가 나타납니다,하지만 난 gradle bootRun의 시작이있어 :

2017-09 -12 10 : 47 : 56.058 WARN 7545 --- [주] ationConfigEmbeddedWebApplicationContext : 컨텍스트 초기화 중에 예외가 발생하여 새로 고침 시도 취소 중 : org.springframework.beans.factory.UnsatisfiedDependencyException : Erro r 'greetingController'라는 이름으로 빈 생성 : 불만족 종속성이 'wfExampleDao'필드를 통해 표현됨. 중첩 예외는 org.springframework.beans.factory.NoSuchBeanDefinitionException : 아니요 유형 'hu.example.dao.WfExampleDao'의 유효한 bean 을 사용할 수 있음 : autowire로 적합한 최소 1 개의 bean이 예상 됨 후보. 종속성 주석 : 내가 종속성을 찾을 수없는 이유를 모르는

{@ org.springframework.beans.factory.annotation.Autowired는 (= TRUE 필수)}. 내가 읽었을 때, bootRun이 의존성을 계산하기 때문에 applicationContext.xml을 생성 할 필요가 없다.

미리 조언 해 주셔서 감사합니다.

+1

'WfExampleDao'는 인터페이스 구현에서'WfStartDao'가됩니다. – Crabime

+0

죄송합니다. 실수를했습니다 – LakiGeri

+0

@Crabime 제가 고쳤습니다. – LakiGeri

답변

1

기본적으로 springboot는 Application 클래스의 하위 패키지에있는 모든 구성 요소를 검색합니다. 구성 요소 검사를 지정하지 않았으므로 모든 클래스가 동일한 패키지 또는 하위 패키지 Application에 있는지 확인하십시오.

+0

맞습니다. 그들은 같은 패키지에 들어 있지 않았습니다. 고마워! :) – LakiGeri

관련 문제