Java Spring
Note: This is not a structured article.
While resolving beans, if there is a conflict(more than 1 bean of the same type in IOC container) then there are 3 ways to resolve this issue.
- Using
@Primary
annotation on the class makes it the first choice @Qualifier
annotation on the class as well as the@Autowired
field- Auto wire by name. In this case, we need to use the name of the class as the variable name. Eg. Class name =
BubbleSortAlgo
then var name should bebubbleSortAlgo
Bean scope:
There are 4 bean scopes:
- singleton (default)
- Prototype - new object on every getBean()
- request - new object on every http request
- session - new object on every http session
We can set the scope using @Scope
annotation and specify the value like ConfigurableBeanFactory.SCOPE_PROTOTYPE
or just "prototype"
If we have a default scope component with a prototype scoped dependency, we have to set the proxy on the prototyped scoped component using scopedProxy=
otherwise we will get a same instance of the prototype scoped dependency.
@SpringBootApplication
by default searches the package in subpackages in which is lies- If we have our beans in a different package then we use
@ComponentScan
annotation to specify the package name
Lifecyle of a bean
@PostConstruct
: a method inside a bean class annotated with postcontruct will run after all the dependencies are initialized@PreDestroy
likewise, this runs before the bean is taken out of context
CDI - Contexts and dependency injection:
- This is a JavaEE standard for annotations.
- Just like how JPA is a standard and Hibernate is one of the implementation
- CDI is standard and spring is one implementation
- Eg.
@Component
→@Named
@Autowired
→@Inject
Spring without SpringBoot:
- Spring-starter dependency has spring-core spring-context and other things by default which saves us time
@SpringBootApplication
is a compound annotation which encloses@Configuration
and@ComponentScan
- So if we want to create spring app without boot, we will have to do the above things and create a context using AnnotationConfigApplicationContext in the main method
- If we want to use xml-based configuration, we will create a new xml file in the resources and create context using ClasspathXmlApplicationContext by passing in the xml file name. Annotations are not required in this case.
Terms (IOC, ApplicationContext, Beanfactory):
- IOC Container - this is a generic name - which means that the framework handles the dependency injection instead of us doing it manually and making it tightly couples
- Spring has two implementations for IOC container
- BeanFactory
- ApplicationContext
- In 99% cases, we use ApplicationContext. Incase of memory constraints like IOT devices, we may use beanfactory
Annotations:
- We have these core annotations:
@Component
@Controller
@Service
@Repository
- They functionally do the exact same thing. But having them helps us to differentiate what a particular class is doing
- Also, in AOP we can do extra things based on the annotation. Like enable debug level logging for all services
Environment vars:
- We load env-vars from properties files using
@Value
annotation on a field
@Value("${server.url}")
private String someUrl;