Spring Java Based Configuration


So far you have perceived how we design Spring beans utilizing XML arrangement document. In the event that you are OK with XML arrangement, at that point it is truly not required to figure out how to continue with Java-based setup as you will accomplish a similar outcome utilizing both of the designs available.

Java-based arrangement alternative empowers you to compose the vast majority of your Spring design without XML however with the assistance of few Java-based comments clarified in this chapter.

@Configuration & @Bean Annotations

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 discloses to Spring that a technique commented on with @Bean will restore an item that ought to be enlisted as a bean in the Spring application setting. The least complex conceivable @Configuration class would be as per the following −

package com.javacodegeeks; 

import org.springframework.context.annotation.*; 

@Configuration 

public class HelloWorldConfig { 

@Bean 

public HelloWorld helloWorld(){ 

return new HelloWorld(); 

} 

}

The above code will be proportionate to the accompanying XML setup −

<beans> 

<bean id = "helloWorld" class = "com.javacodegeeks.HelloWorld" /> 

</beans>

Here, the technique name is commented on with @Bean fills in as bean ID and it makes and returns the genuine bean. Your design class can have a statement for more than one @Bean. When your setup classes are characterized, you can stack and give them to Spring holder utilizing AnnotationConfigApplicationContext as pursues −

public static void main(String[] args) { 

ApplicationContext ctx = new AnnotationConfigApplicationContext(HelloWorldConfig.class);
    
HelloWorld helloWorld = ctx.getBean(HelloWorld.class); 

helloWorld.setMessage("Hello World!"); 

helloWorld.getMessage(); 

}

You can stack different design classes as pursues −

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(); 

}

Example

Let us have a working Eclipse IDE set up and find a way to make a Spring application −

Steps Description
1 Create a task with a name SpringExample and make a bundle com.javacodegeeks under the src envelope in the made project.
2 Add required Spring libraries utilizing Add External JARs alternative as clarified in the Spring Hello World Example chapter.
3 Because you are utilizing Java-based comments, so you likewise need to include CGLIB.jar from your Java establishment registry and ASM.jar library which can be downloaded from asm.ow2.org.
4 Create Java classes HelloWorldConfig, HelloWorld and MainApp under the com.javacodegeeks package.
5 The last advance is to make the substance of all the Java documents and Bean Configuration record and run the application as clarified below.

Here is the substance of HelloWorldConfig.java file

package com.javacodegeeks; 

import org.springframework.context.annotation.*; 

@Configuration 

public class HelloWorldConfig { 

@Bean 

public HelloWorld helloWorld(){ 

return new HelloWorld(); 

} 

}

Here is the substance of HelloWorld.java file

package com.javacodegeeks;
    
    
    public class HelloWorld { 

private String message; 

public void setMessage(String message){ 

this.message = message; 

} 

public void getMessage(){ 

System.out.println("Your Message : " + message); 

} 

}

Following is the substance of the MainApp.java file

package com.javacodegeeks; 

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(); 

} 

}

Once you are finished making all the source records and including the required extra libraries, let us run the application. You should take note of that there is no design document required. In the event that all is well with your application, it will print the accompanying message −

Your Message : Hello World! 

Injecting Bean Dependencies

When @Beans have conditions on each other, communicating that the reliance is as basic as having one bean strategy calling another as pursues −

package com.javacodegeeks; 

import org.springframework.context.annotation.*; 

@Configuration 

public class AppConfig { 

@Bean 

public Foo foo() { 

return new Foo(bar()); 

} 

@Bean 

public Bar bar() { 

return new Bar(); 

} 

}

Here, the foo bean gets a reference to bar by means of the constructor infusion. Presently given us a chance to take a gander at another working example.

Example

Let us have a working Eclipse IDE set up and find a way to make a Spring application −

Steps Description
1 Create a task with a name SpringExample and make a bundle com.javacodegeeks under the src organizer in the made project.
2 Add required Spring libraries utilizing Add External JARs alternative as clarified in the Spring Hello World Example chapter.
3 Because you are utilizing Java-based comments, so you additionally need to include CGLIB.jar from your Java establishment registry and ASM.jar library which can be downloaded from asm.ow2.org.
4 Create Java classes TextEditorConfig, TextEditor, SpellChecker and MainApp under the com.javacodegeeks package.
5 The last advance is to make the substance of all the Java records and Bean Configuration document and run the application as clarified below.





© Javacodegeeks 2018 -2019
All Right Reserved and you agree to have read and accepted our term and condition.

Python 3