Posts

Showing posts with the label Spring

Spring @LookUp Annotation Example

Image
In this section we will learn about @LookUp  Annotation. How to use @Lookup annotation By default, the scope of the bean in a Spring application are singletons , and we use a constructor or setter to implement dependencies.  But there is another situation: there is a singleton u ser , and he needs a new role bean every time. That is, User is a singleton bean, and Role is prototype bean. The life cycles of beans are different. Bean User is created by the container only once, and bean Role is created every time a new one is created – let's say this happens every time you call some method of the User bean. This is where the introduction of a bean using the @ Lookup method comes in handy. It occurs not when the container is initialized, but later, each time the method is called. A method annotated with @Lookup tells Spring to return an instance of the method's return type when we invoke it. What is Lookup method injection? We create a stub method( createRole ) in the User bean and

Spring @ImportResource Annotation Example

Image
In this section we will learn about @ImportResource  Annotation. The  @ImportResource  annotation is used to load beans from an XML configuration file into an Application Context in Java configuration based applications. The class annotated with @Configuration can use @ImportResource annotation to load XML configurations. For example, old application based on XML configuration is being upgraded to Java configuration based application, to keep bean definition at both places, in Java configuration as well as XML configuration , we can use @ImportResource annotation.     @Configuration @ImportResource ( "classpath:/app-config.xml" ) public class AppConfig { .............. } The following example creates a Spring Boot application which uses @ImportResource annotation. Project Directory pom.xml <? xml version ="1.0" encoding ="UTF-8" ?> < project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns: xsi ="http://www.w3.org/20

Spring @Primary Annotation Example

Image
In this section we will learn about @Primary  Annotation. When there are multiple beans of the same data-type  in Spring container ,  all of them are qualified to be autowired to single-valued dependency. That causes ambiguity and leads to throw an exception by framework.  To avoid this issue, we can use the Spring-specific @Primary annotation that automatically gives the higher preference to a particular bean.  This annotation can be used on any class annotated with the @Component , @Service & @Repository annotation or on methods annotated with the @Bean annotation. There should be only one @Primary bean among same type of beans. Using @Primary with @Bean Create  MessageService  interface for message service implementations. public interface MessageService { void printMessaage (); } Create implementations -  SMSService  and  EmailService  classes.    public class SMSService implements MessageService{ @Override public void printMessaage() { System.out.printl