简体   繁体   中英

How to @Autowire services in SpringBoot

Good day, guys. I have a question about autowiring services into my classes when using Springboot. All of the examples I have seen on the Internet as well as in the Springboot specification do something of the like (taking an excerpt from the Springboot version 1.5.7 specification):

package com.example.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DatabaseAccountService implements AccountService {
private final RiskAssessor riskAssessor;

@Autowired
public DatabaseAccountService(RiskAssessor riskAssessor) {
this.riskAssessor = riskAssessor;
}
// ...
}

This is a class that injects a property through its constructor, by means of @Autowiring the constructor. Another form is to @Autowire the property like this:

@Autowired
private final RiskAssessor riskAssessor

But, where I work, for these two methods to work, I have been told that I need to use this method:

 applicationContext.getAutowireCapableBeanFactory().autowireBean(Object.class)

They have told me that I need this in order for the @Autowired annotation to work.

Now my question to you is: why is there no simple annotation that allows the @Autowire to function correctly? (Something like @AutowiredClass). The above method is too verbose and hard to remember, so surely there must be a better way to make @Autowired work on classes in order to inject services, just like we do in Grails where we just say def someService and it is automatically injected.

If you want properly use @Autowired in your spring-boot application, you must do next steps:

  1. Add @SpringBootApplication to your main class
  2. Add @Service or @Component annotation to class you want inject
  3. Use one of two ways that you describe in question, to autowire

If you don't have any wiered package structure and the main class package includes all other classes you want spring to instantiate (directly or in the subpackages) a simple annotation @ComponentScan on your main class will help you save all those boiler plate code. Then spring will do the magic, it will go and scan the package(and subpackages) and look for classes annotated with @Service , @Component etc and instantiate it.

Even better, use @SpringBootApplication in your main class, this will cover @Configuration as well. If it is a green field project , I would encourage to start from start.spring.io - a template generation/scaffolding tool for spring

Now my question to you is: why is there no simple annotation that allows the @Autowire to function correctly?

There is: @SpringBootApplication

If you put this at the root of your application (file that contains the main class) and as long as your services are at the same package or a sub-package, Spring will auto-discover, instantiate, and inject the proper classes.

There's an example in this walk-through: REST Service with Spring Boot

As described in that page:

@SpringBootApplication is a convenience annotation that adds all of the following: @Configuration tags the class as a source of bean definitions for the application context. @EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings. @ComponentScan tells Spring to look for other components, configurations, and services in the hello package, allowing it to find the controllers.

You need to annotate the implementation of RestService as a @Service or @Component so Spring would pick it up.

@Service
public class MyRiskAssessorImpl implements RiskAssessor {
///
}

@Autowired almost works out of the box. Just do your component scanning of the class you want to autowire and you are done. Just make sure your main class (or main configuration class) uses @ComponentScan("{com.example.app}") or @SpringBootApplication (main class). The docs explain this stuff pretty good

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