We Are Going To Discuss About The injection point has the following annotations: – @org.springframework.beans.factory.annotation.Autowired(required=true). So lets Start this Java Article.
The injection point has the following annotations: – @org.springframework.beans.factory.annotation.Autowired(required=true)
- The injection point has the following annotations: – @org.springframework.beans.factory.annotation.Autowired(required=true)
The class which is going to be Autowired should be marked with @Service or @Component. Also if the class is in different package then need to add the @ComponentScan annotation in the main class as follows.
@ComponentScan({"com.beta.replyservice", "com.beta.ruleService"}) @SpringBootApplication
- @org.springframework.beans.factory.annotation.Autowired(required=true)
The class which is going to be Autowired should be marked with @Service or @Component. Also if the class is in different package then need to add the @ComponentScan annotation in the main class as follows.
@ComponentScan({"com.beta.replyservice", "com.beta.ruleService"}) @SpringBootApplication
Solution 1
The error seems to indicate that Spring does not know any bean of type com.primesolutions.fileupload.service.FileStorageService
.
As said in the comment, make sure you class FileStorageService
is annotated by @Service
or @Component
:
@Service
public class FileStorageService {
...
}
Make also sure that this class is located in a sub-package of your class FileApplication
. For example, if your FileApplication
class is located in a package com.my.package
, make sure your FileStorageService
is located in the package com.my.package.** (same package or any sub package).
Few notes to improve your code by the way :
-
When your class has only one not default constructor, the use of
@Autowired
on the constructor is optional. -
Do not put too much code in your constructor. Use instead the
@PostConstruct
annotation.
@Service
public class FileStorageService {
private FileStorageProperties props;
// @Autowired is optional in this case
public FileStorageService (FileStorageProperties fileStorageProperties) {
this.props = fileStorageProperties;
this.fileStorageLocation = Paths.get(fileStorageProperties.getUploadDir())
.toAbsolutePath().normalize();
}
@PostConstruct
public void init() {
try {
Files.createDirectories(this.fileStorageLocation);
} catch (Exception ex) {
throw new FileStorageException("Could not create the directory where the uploaded files will be stored.", ex);
}
}
}
- It is better to avoid the
@Autowired
on a field. Use the constructor instead. It is better for your tests, and more maintainable:
public class FileController {
private FileStorageService service;
public FileController(FileStorageService service) {
this.service = service;
}
}
Original Author Of This Content
Solution 2
Tried with removing the (exclude = {DataSourceAutoConfiguration.class }) parameter with @SpringBootApplication:
Before:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
public class SpringBootMain { …
After:
@SpringBootApplication
public class SpringBootMain { …
Worked for me.
Original Author Of This Content
Solution 3
The class which is going to be Autowired should be marked with @Service or @Component. Also if the class is in different package then need to add the @ComponentScan annotation in the main class as follows.
@ComponentScan({"com.beta.replyservice", "com.beta.ruleService"})
@SpringBootApplication
Original Author Of This Content
Solution 4
I solved this problem using where i use @Autowired annotation just replace with this`
@Autowired(required = false)
`
Original Author Of This Content
Conclusion
So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.