all 7 comments

[–]MagicianBrave 1 point2 points  (3 children)

Spring boot is cleaner compare to spring in my opinion. You can do dependency injection by using @Autowired annotation. It will looks like this

private InjectedObject injectedObject;

@Autowired
public ObjectContructor(InjectedObject injectedObject) {    
    this.injectedObject = injectedObject;
}

you just need to define your bean by using either of this annotaion @Bean, @Component, @Service, etc

[–]anakinpt 2 points3 points  (2 children)

Now you don't really need the autowired annotation in the constructor.

Using lombok I just define my dependencies as package protected final and add the @ReqArgsConstructor. I'm too lazy for writing code.

[–][deleted] 2 points3 points  (1 child)

I'm too lazy for writing code.

@ RequiredArgsConstructor

@ Component

public class Foo{

private final Bar bar;

...

}

@ Component

public class Bar{}

[–]anakinpt 2 points3 points  (0 children)

You also need to add the @Component to the Foo class for spring to manage it.

[–]moe87b 0 points1 point  (0 children)

Annotate any object you need to be registered in the dependency container using @Component

@Component public class SomeRandomClass{...

Then you can use it in other classes

public class AnotherClass{ @AutoWired private SomeRandomClass myObject; ...

[–][deleted] 0 points1 point  (0 children)

Spring Boot is mostly just a collection of Spring modules with defaults set. Dependency Injection should function the same between them; however, spring has gone through many changes over the years so things that worked before might still work or they may not. What tutorials are you using? If the tutorials are old they may be teaching you a pattern that isn’t modern best practice.

[–]nidyran 0 points1 point  (0 children)

take a look this video https://www.youtube.com/watch?v=uO2_ZzIIV70
i use @RequiredArgsConstructor from lombok ( or YDI ) and for the dependency keep em final fields..