Spring自动线程bean使用基于java的注释

时间:2016-12-07 08:53:02

标签: java spring spring-mvc dependency-injection autowired

我正在尝试创建一个新的简单的Spring启动应用程序来演示依赖注入。我想使用@Autowired注释导入bean。

这是我的示例代码

---- ---- Example.class

package com.example.project;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
public class Example {


    @Autowired
    public myBean first;

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

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

}

---- ---- myBean.class

package com.example.project;

public class myBean {

    myBean()
    {
        System.out.println("Hi myBean Constructed");
    }
}

--- --- BeanConfiguration.class

package com.example.project;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ComponentScan;

@Configuration
@ComponentScan(basePackages = "com.example.project")
public class BeanConfigurationClass {

    @Bean
    public myBean getBean()
    {
        return new myBean();

    }
}

- 的pom.xml ---

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>example</groupId>
    <artifactId>1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
    </parent>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.2.5.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

然而,当我尝试运行应用程序时,它无法找到bean并给出以下错误 com.example.project.Example中的字段首先需要一个无法找到的“com.example.project.myBean”类型的bean。

我也尝试过使用基于xml的配置,但遇到了同样的错误。 这里有什么根本性的错误。

在期待中感谢你。

3 个答案:

答案 0 :(得分:1)

  1. @EnableAutoConfiguration从控制器移至主应用程序类。
  2. 摆脱BeanConfigurationClass
  3. myBean class:

    上添加一个spring DI class注释
    @Component
    public class myBean {
    
        myBean(){
            System.out.println("Hi myBean Constructed");
        }
    }
    

答案 1 :(得分:0)

课程名称必须以大写字母开头!必须是“我的豆豆”#39; ..

答案 2 :(得分:0)

您应该尝试执行以下操作,

@EnableAutoConfiguration从控制器移至主应用程序类,即BeanConfigurationClass。这是因为BeanConfigurationClass是您的配置文件,所有与配置相关的注释都应该放在此。

同时将您的myBean重命名为MyBean,并使用@Component注释对其进行注释。

编辑:从BeanConfigurationClass删除以下注释,只添加一个@SpringBootApplication,它会处理所有删除的注释。

@Configuration
@ComponentScan
@EnableAutoConfiguration

添加为,

@SpringBootApplication
BeanConfigurationClass
相关问题