Autowired instance with null value

Asked

Viewed 117 times

-2

Problem

I am working on a Spring Boot project that depends on other project, these projects are my entities, services and the like.

I use the note Autowired to inject my dependencies, example:

@Autowired
TermService termService;

Works perfectly on mine controller, however the project grew and decided to create new classes in a new package.

And that’s when the problem arises, when I use the Autowired in these new classes no dependency is injected and the object remains null.

I looked it up and all I could find was the note Component in the classes, however it did not work.

Project structure

inserir a descrição da imagem aqui

  • Post more details on the structure of your project, as well as the structure of your dependency

  • It is a conventional Spring Boot design, has no special configuration.

  • Can be several things, without the structure of the project can not help

  • But what do you need? Because if you are going to post the whole structure of the project the question will become giant, as I said earlier is a conventional structure of a Spring Boot project, as it is a standard project I see no need to post this part that every Spring Boot project has.

  • All over again I asked this in question, created a package and created new classes within this package, that’s what’s new in the project.

  • Package structure, the main class annotated with @SpringBootApplication, the package and the class presenting injection problem (all with Imports :) )

  • The class with the annotation "Springbootapplication" there are no changes, it is like in a new project, only has the "main" and inside the command "Springapplication.run(nameDaclas..."

  • Now the class is a new class too, there’s nothing in it, I just created a new class and took the code that was in the controller and put it there.

  • What happens is that when I put the "Autowired" in this new class the injection does not work.

  • I agree with @nullptr about posting more details. Naming and organizing project directories influence a lot of Spring projects.

  • A print of the organization by the IDE already helps.

  • @Gustavo changed the question and put the structure print, the folder "build" was the one I created

  • It looks OK. Somewhere in your code you use without spring injection: new TermService() ?

  • I have to write a lot to send the answer, the answer is: No

  • Already used the Can packageScan?

Show 11 more comments

2 answers

0


Problem

I used "Autowired" in a class that I installed manually, or I used "new" to use it, so Spring does not inject the dependency.

Solution

The class that was instantiated manually put the "@Component" annotation, so all its dependencies have been filled in.

0

It would help to know the version you’re using, but come on.

Use the Spring annotation @ComponentScan next to the @SpringBootApplication and configure the custom base package (you can specify a list of package names or a list of classes whose package will be used), for example:

@SpringBootApplication
@ComponentScan(basePackages = {"outroPacote", "..."})
public class Application

Or

@SpringBootApplication
@ComponentScan(basePackageClasses = {otherpackage.MyClass.class, ...})
public class Application

Or if you are using a version that is from Spring 1.3.0, you can write directly:

@SpringBootApplication(scanBasePackageClasses = {otherpackage.MyClass.class, ...})
public class Application

Note that the 'Component scan' will find classes inside and below the packages you have placed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.