vamshi

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.

  1. Using @Primary annotation on the class makes it the first choice
  2. @Qualifier annotation on the class as well as the @Autowired field
  3. 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 be bubbleSortAlgo

Bean scope:

There are 4 bean scopes:

  1. singleton (default)
  2. Prototype - new object on every getBean()
  3. request - new object on every http request
  4. 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.

Lifecyle of a bean

CDI - Contexts and dependency injection:

Spring without SpringBoot:

Terms (IOC, ApplicationContext, Beanfactory):

Annotations:

Environment vars:

@Value("${server.url}")
private String someUrl;

#Java #Spring #Notes