简体   繁体   中英

Spring auto wire bean using java based annotation

I am trying to make a new simple Spring boot application to demo dependency injection. I would like to import beans using @Autowired annotation.

Here is my sample piece of code

----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>

However when i try to run the application it is unable to find the bean and gives the following error Field first in com.example.project.Example required a bean of type 'com.example.project.myBean' that could not be found.

I also tried using xml based configuration but faced the same error. Is there something fundamentally wrong going over here.

Thanking you in anticipation.

  1. Move @EnableAutoConfiguration from controller to your main application class.
  2. Get rid of BeanConfigurationClass
  3. Add one of spring DI class annotations on myBean class:

     @Component public class myBean { myBean(){ System.out.println("Hi myBean Constructed"); } } 

Name of the classes must starts with CAPITAL letter ! Must be 'My Bean' ..

You should try to do following,

Move @EnableAutoConfiguration from controller to your main application class ie BeanConfigurationClass . This is because BeanConfigurationClass is your configuration file and all configurations related annotations should placed on this.

Also rename your myBean to MyBean and annotate that with @Component annotation.

EDIT : Remove following annotations from your BeanConfigurationClass and add only this one @SpringBootApplication it will take care of all those removed.

@Configuration
@ComponentScan
@EnableAutoConfiguration

Add as ,

@SpringBootApplication
BeanConfigurationClass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM