Springs - SPLessons

Spring Java Configuration

Home > Lesson > Chapter 16
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Spring Java Configuration

Spring Java Configuration

shape Description

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: [java]package splessons; import org.springframework.context.annotation.*; @Configuration public class HelloWorldConfig { @Bean public HelloWorld helloWorld(){ return new HelloWorld(); } }[/java] Above mentioned code will be equivalent to XML code as follows. [java]<beans> <bean id="helloWorld" class="splessons.HelloWorld" /> </beans>[/java] 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. [java]public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(HelloWorldConfig.class); HelloWorld helloWorld = ctx.getBean(HelloWorld.class); helloWorld.setMessage("Hello World!"); helloWorld.getMessage(); }[/java] Developer can include different configuration classes as follows. [java]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(); }[/java]

shape Example

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 [java]package splessons; import org.springframework.context.annotation.*; @Configuration public class HelloWorldConfig { @Bean public HelloWorld helloWorld(){ return new HelloWorld(); } }[/java] 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 [java]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); } }[/java] Here performed SET nad GET methods. MainApp.java file [java]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(); } }[/java] 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 [java]Your Message : Hello World![/java]

Import Annotation

shape Description

It takes into consideration stacking @Bean definitions from another setup class. Presently consider Config A as follows. [java]@Configuration public class ConfigA { @Bean public A a() { return new A(); } }[/java] Import ConfigA in ConfigB as follows. [java]@Configuration @Import(ConfigA.class) public class ConfigB { @Bean public B a() { return new A(); } }[/java] 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. [java]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); }[/java]

Summary

shape Key Points

  • 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.