Reputation: 413
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.
Upvotes: 11
Views: 43824
Reputation: 31
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
Upvotes: 3
Reputation: 71
If you want properly use @Autowired
in your spring-boot application, you must do next steps:
@SpringBootApplication
to your main class@Service
or @Component
annotation to class you want injectUpvotes: 7
Reputation: 3814
@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
Upvotes: 1
Reputation: 17904
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.
Upvotes: 2
Reputation: 5578
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 {
///
}
Upvotes: 0