close

[Solved] The injection point has the following annotations: – @org.springframework.beans.factory.annotation.Autowired(required=true)

Hello Guys, How are you all? Hope You all Are Fine. Today I am facing the following error The injection point has the following annotations: – @org.springframework.beans.factory.annotation.Autowired(required=true) in Java. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How The injection point has the following annotations: – @org.springframework.beans.factory.annotation.Autowired(required=true) Error Occurs?

Today I am facing the following error The injection point has the following annotations: – @org.springframework.beans.factory.annotation.Autowired(required=true) in Java.

How To Solve The injection point has the following annotations: – @org.springframework.beans.factory.annotation.Autowired(required=true) Error ?

  1. How To Solve The injection point has the following annotations: – @org.springframework.beans.factory.annotation.Autowired(required=true) Error ?

    To Solve The injection point has the following annotations: @org.springframework.beans.factory.annotation.Autowired(required=true) Error The error seems to indicate that Spring does not know any bean of type com.primesolutions.fileupload.service.FileStorageService.

  2. The injection point has the following annotations: – @org.springframework.beans.factory.annotation.Autowired(required=true)

    To Solve The injection point has the following annotations: @org.springframework.beans.factory.annotation.Autowired(required=true) Error The error seems to indicate that Spring does not know any bean of type com.primesolutions.fileupload.service.FileStorageService.

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 FileStorageServiceis 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;
    }
}

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.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read