簡體   English   中英

如何在spring-boot中定義依賴項注入?

[英]How define the dependency injections in spring-boot?

我嘗試用spring-boot創建一個spring Web應用程序。 我的第一個問題是依賴項注入對我不起作用。 這是我關注的文章

我創建了一個Application類:

@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);
    }
}

然后我做了一個控制器:

@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));
    }
}

我的例子:

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.");
    }
}

我的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'
} 

我的期望:當我打開/ greeting頁面時,將出現日志,但是我在gradle bootRungradle bootRun

2017-09-12 10:47:56.058 WARN 7545 --- [main] ationConfigEmbeddedWebApplicationContext:上下文初始化期間遇到異常-取消刷新嘗試:org.springframework.beans.factory.UnsatisfiedDependencyException:創建名稱為'greetingController'的bean時出錯:不滿意通過字段'wfExampleDao'表示的依賴關系; 嵌套的異常是org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有類型為“ hu.example.dao.WfExampleDao”的合格Bean:期望至少有1個合格為自動裝配候選的Bean。 依賴項注釋:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

我不知道為什么找不到依賴項。 如我所讀,我不必創建applicationContext.xml因為bootRun可以找出依賴性。

預先感謝您的建議!

默認情況下,springboot將掃描Application類的子包中的所有組件。 您沒有指定組件掃描,因此請確保所有類都在Application同一程序包或子程序包中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM