Sunday, September 15, 2019

Spring & Guice DI


Spring


The way to use @Autowired and register beans:


 @Component simple bean
 @Controller servlet control ben
 @Repository DAO classes
 @Configuration general bean
 XML file define bean from xml 

Also use @Bean to register a bean on method level.

@ComponentScan to detects and instantiates annotated components automatically




Guice


@Inject instead of @Autowired

@Provides instead of @Bean

Governator plugin instead of @ComponentScan


Guice Injection Styles
LOCATION
INJECTION ORDER
MOTIVATION
COMMENT
Constructor
First
Class immutability1 Mandatory dependencies
Only one allowed with @Inject.
Field
Second
Quick prototyping Code that doesn’t need testing
Injection order is random.
Setter
Third
Dealing with legacy classes
Optional dependencies
2
Injection


More info:

https://www.baeldung.com/guice-spring-dependency-injection
https://www.amazon.com/Google-Guice-Lightweight-Dependency-Firstpress/dp/1590599977


No comments:

Post a Comment