Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在spring boot中定义依赖注入?_Java_Spring_Spring Boot_Gradle_Dependency Injection - Fatal编程技术网

Java 如何在spring boot中定义依赖注入?

Java 如何在spring boot中定义依赖注入?,java,spring,spring-boot,gradle,dependency-injection,Java,Spring,Spring Boot,Gradle,Dependency Injection,我尝试用SpringBoot创建一个SpringWeb应用程序。我的第一个问题是依赖注入对我不起作用。这就是我遵循的原则 我创建了一个应用程序类: @SpringBootApplication public class Application extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder

我尝试用SpringBoot创建一个SpringWeb应用程序。我的第一个问题是依赖注入对我不起作用。这就是我遵循的原则

我创建了一个应用程序类:

@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 bootRun
的开头看到的:

2017-09-12 10:47:56.058警告7545---[主要] ationConfigEmbeddedWebApplicationContext:遇到异常 在上下文初始化期间-取消刷新尝试: org.springframework.beans.factory.unsatifiedDependencyException: 创建名为“greetingController”的bean时出错:未满足 通过字段“wfExampleDao”表示的依赖关系;嵌套异常是 org.springframework.beans.factory.noSuchBean定义异常:否 限定“hu.example.dao.WfExampleDao”类型的bean 可用:至少需要1个符合autowire条件的bean 候选人依赖项批注: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

我不知道为什么找不到依赖关系。正如我所读到的,我不必创建
applicationContext.xml
,因为引导运行会找出依赖项


提前感谢您的建议

默认情况下,springboot将扫描应用程序类的子包中的所有组件。您没有指定组件扫描,因此请确保所有类都在
应用程序的相同包或子包中

WfExampleDao
在其接口实现中变为
WfStartDao
后者。抱歉,我做了一个错误mistake@Crabime我改正了,谢谢!其他类是否在同一个包中或
应用程序的子包中?您是对的,它们不在同一个包中。Thx!:)