ythy / blog

Give everything a shot
6 stars 0 forks source link

Spring boot Autowired #169

Open ythy opened 5 years ago

ythy commented 5 years ago
@RestController
public class UserController {

    @Autowired
    private Utils utils;

    @Autowired
    private MyComponent tools;

    @RequestMapping("/")
    public String index() {
        return "Greetings from Spring Boot";
    }

    @RequestMapping("/getUserInfo")
    public ModelAndView getUserInfo(@RequestParam(name="name", required=false, defaultValue="World") String name) {
        ModelAndView model = new ModelAndView("userInfo");
        model.addObject("name", utils.getPath() + " - " + tools.provideTools().getName());
        return model;
    }

}

此处警告 Field injection is not recommended,修改为Setters injection正常。

@RestController
public class UserController {

    private Utils utils;
    private MyComponent tools;

    @Autowired
    public void setMyComponent(MyComponent component) {
        this.tools = component;
    }

    @Autowired
    public void setUtils(Utils util) {
        this.utils = util;
    }

    @RequestMapping("/")
    public String index() {
        return "Greetings from Spring Boot";
    }

    @RequestMapping("/getUserInfo")
    public ModelAndView getUserInfo(@RequestParam(name="name", required=false, defaultValue="World") String name) {
        ModelAndView model = new ModelAndView("userInfo");
        model.addObject("name", utils.getPath() + " - " + tools.provideTools().getName());
        return model;
    }

}

或者 constructor injection

@Autowired
public UserController(MyComponent t, Utils u){
    this.tools = t;
    this.utils = u;
}

private Utils utils;
private MyComponent tools;

Using @Autowired

@Component("fooFormatter")
public class FooFormatter {

    public String format() {
        return "foo";
    }
}

@Autowired on Properties

The annotation can be used directly on properties, therefore eliminating the need for getters and setters:

@Component
public class FooService {

    @Autowired
    private FooFormatter fooFormatter;

}

@Autowired on Setters

he @Autowired annotation can be used on setter methods. In the below example, when the annotation is used on the setter method, the setter method is called with the instance of FooFormatter when FooService is created:

public class FooService {

    private FooFormatter fooFormatter;

    @Autowired
    public void setFooFormatter(FooFormatter fooFormatter) {
            this.fooFormatter = fooFormatter;
    }
}

@Autowired on Constructors

The @Autowired annotation can also be used on constructors. In the below example, when the annotation is used on a constructor, an instance of FooFormatter is injected as an argument to the constructor when FooService is created:

public class FooService {

    private FooFormatter fooFormatter;

    @Autowired
    public FooService(FooFormatter fooFormatter) {
        this.fooFormatter = fooFormatter;
    }
}
ythy commented 5 years ago

@SpringBootApplication

Many Spring Boot developers like their apps to use auto-configuration, component scan and be able to define extra configuration on their "application class". A single @SpringBootApplication annotation can be used to enable those three features, that is:

  1. @EnableAutoConfiguration : enable Spring Boot’s auto-configuration mechanism
  2. @ComponentScan : enable @Component scan on the package where the application is located
  3. @Configuration : allow to register extra beans in the context or import additional configuration classes
ythy commented 5 years ago