Springs - SPLessons

Chapter 15

Spring Java Configuration

Spring Java Configuration

Spring Java Configuration, this tutorial covers how Spring beans utilizing XML Configuration. If everything is comfortable with XML configuration, then no need to have Java based configuration, because it also gives the same result as XML configuration. Spring Java Configuration enables developer to compose the greater part of Spring layout without using an XML layout with the assistance of annotations as shown as follows.

Configuration & Bean Annotations:

Many of the developers will use XML based configuration in spring, because it is easy to configure. A developer can write less code in the XML configuration file. The important contrast between XML and Java based configuration is recompile the project if any changes occur. Configuration class is as follows:

package splessons;
import org.springframework.context.annotation.*;

@Configuration
public class HelloWorldConfig {

   @Bean 
   public HelloWorld helloWorld(){
      return new HelloWorld();
   }
}

Above mentioned code will be equivalent to XML code as follows.

<beans>
  <bean id="helloWorld" class="splessons.HelloWorld" />
</beans>

Every XML will consists of one bean id (attribute), the ID should be unique in a framework where the bank will be hosted. After characterization of configuration classes provides them to Spring IOC utilizing AnnotationConfigApplicationContext as follows.

public static void main(String[] args) {
   ApplicationContext ctx = 
   new AnnotationConfigApplicationContext(HelloWorldConfig.class);
   
   HelloWorld helloWorld = ctx.getBean(HelloWorld.class);

   helloWorld.setMessage("Hello World!");
   helloWorld.getMessage();
}

Developer can include different configuration classes as follows.

public static void main(String[] args) {
   AnnotationConfigApplicationContext ctx = 
   new AnnotationConfigApplicationContext();

   ctx.register(AppConfig.class, OtherConfig.class);
   ctx.register(AdditionalConfig.class);
   ctx.refresh();

   MyService myService = ctx.getBean(MyService.class);
   myService.doStuff();
}

Here include required Spring libraries using Add External JARs option. Add CGLIB.jar from your Java installation directory and ASM.jar library which can be downloaded from below link.

Jar files

HelloWorldConfig.java file

package splessons;
import org.springframework.context.annotation.*;

@Configuration
public class HelloWorldConfig {

   @Bean 
   public HelloWorld helloWorld(){
      return new HelloWorld();
   }
}

Annotating a class with the @Configuration demonstrates that the class can be utilized by the Spring IoC compartment as a wellspring of bean definitions. The @Bean explanation tells Spring that a technique annotated on with @Bean will give back an object that ought to be enrolled as a bean in the Spring application setting.
HelloWorld.java file

package splessons;

public class HelloWorld {
   private String message;

   public void setMessage(String message){
      this.message  = message;
   }

   public void getMessage(){
      System.out.println("Your Message : " + message);
   }
}

Here performed SET nad GET methods.
MainApp.java file

package splessons;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;

public class MainApp {
   public static void main(String[] args) {
      ApplicationContext ctx = 
      new AnnotationConfigApplicationContext(HelloWorldConfig.class);
   
      HelloWorld helloWorld = ctx.getBean(HelloWorld.class);

      helloWorld.setMessage("Hello World!");
      helloWorld.getMessage();
   }
}

The Application Context is spring’s more best in class holder. Like BeanFactory it can stack bean definitions, wire beans together and administer beans upon solicitation. Also it includes more enterprise-specific usefulness, for example, the capacity to determine literary messages from a properties document and the capacity to distribute application events to interested event listeners. This container is characterized by the org.springframework.context.ApplicationContext interface.
Output

Your Message : Hello World!

Import Annotation

It takes into consideration stacking @Bean definitions from another setup class. Presently consider Config A as follows.

@Configuration
public class ConfigA {
   @Bean
   public A a() {
      return new A(); 
   }
}

Import ConfigA in ConfigB as follows.

@Configuration
@Import(ConfigA.class)
public class ConfigB {
   @Bean
   public B a() {
      return new A(); 
   }
}

A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. These beans are created with the configuration metadata that you supply to the container. While startup  the context, just ConfigB should be supplied as below.

public static void main(String[] args) {
   ApplicationContext ctx = 
   new AnnotationConfigApplicationContext(ConfigB.class);
   // now both beans A and B will be available...
   A a = ctx.getBean(A.class);
   B b = ctx.getBean(B.class);
}

Summary

  • Spring Java Configuration – Java based configuration gives better configuration support to Spring framework with Java based annotations.
  • Spring Java Configuration – It needs CGLIB. jar file into the Spring framework.