Spring Container Configuration using pure Java class
Instead of configuring Spring container using XML we'll use pure java class to configure.Three ways to configure Spring container
- Full XML Config
- XML Component Scan
- Java Configuration Class
Java Configuration Class for configuring Spring Container
Configuring Java class instead of xml
@Configuration
@ComponentScan("com.demo.spring")
---------------------------------------------------------------------------------------------------------------------
Movie.java
---------------------------------------------------------------------------------------------------------------------
package com.demo.spring;
public interface Movie {
public String getMovie();
public String getCategory();
}
---------------------------------------------------------------------------------------------------------------------
Category.java
---------------------------------------------------------------------------------------------------------------------
package com.demo.spring;
public interface Category {
public String getCategory();
}
--------------------------------------------------------------------------------------------------------------------
Comedy.java
---------------------------------------------------------------------------------------------------------------------
package com.demo.spring;
import org.springframework.stereotype.Component;
@Component
public class Comedy implements Category {
@Override
public String getCategory() {
return "Life is a comedy";
}
}
---------------------------------------------------------------------------------------------------------------------
Bollywood.java
---------------------------------------------------------------------------------------------------------------------
package com.demo.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component("bollywood")
public class Bollywood implements Movie {
@Autowired
Category cat;
@Override
public String getMovie() {
return "Bollywood movies have thousands of varities";
}
@Override
public String getCategory() {
return cat.getCategory();
}
}
---------------------------------------------------------------------------------------------------------------------
Comedy.java
---------------------------------------------------------------------------------------------------------------------
package com.demo.spring;
import org.springframework.stereotype.Component;
@Component
public class Comedy implements Category {
@Override
public String getCategory() {
return "Life is a comedy";
}
}
---------------------------------------------------------------------------------------------------------------------
MyApp.java
---------------------------------------------------------------------------------------------------------------------
package com.demo.spring;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyApp {
public static void main(String args[])
{
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(MovieConfig.class);
Movie obj=context.getBean("bollywood",Movie.class);
System.out.println(obj.getMovie());
System.out.println(obj.getCategory());
context.close();
}
}
---------------------------------------------------------------------------------------------------------------------
MovieConfig.java
---------------------------------------------------------------------------------------------------------------------
package com.demo.spring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ComponentScan("com.demo.spring")
public class MovieConfig {
}
---------------------------------------------------------------------------------------------------------------------
OUTPUT
---------------------------------------------------------------------------------------------------------------------
Feb 27, 2018 3:07:54 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1963006a: startup date [Tue Feb 27 03:07:54 IST 2018]; root of context hierarchy
Bollywood movies have thousands of varities
Life is a comedy
Feb 27, 2018 3:07:54 AM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1963006a: startup date [Tue Feb 27 03:07:54 IST 2018]; root of context hierarchy
Configuring bean in java config without using @ComponentScan
---------------------------------------------------------------------------------------------------------------------
Action.java // no @Component annotation
---------------------------------------------------------------------------------------------------------------------
package com.demo.spring;
public class Action implements Category {
@Override
public String getCategory() {
return "Coding is the only Action i believe in";
}
}
---------------------------------------------------------------------------------------------------------------------
Hollywood.java - // basic constructor injection
---------------------------------------------------------------------------------------------------------------------
package com.demo.spring;
import org.springframework.beans.factory.annotation.Value;
public class Hollywood implements Movie {
private Category cat;
public Hollywood(Category thecat){
cat=thecat;
}
@Override
public String getMovie() {
return "Graphics in Hollywood movies is a real-feel kind";
}
@Override
public String getCategory() {
return cat.getCategory();
}
}
---------------------------------------------------------------------------------------------------------------------
MovieConfig.java -// defining the individual bean without @ComponentScan
---------------------------------------------------------------------------------------------------------------------
package com.demo.spring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
public class MovieConfig {
//defining bean for action category note: getCategory() is the bean id
@Bean
public Category getCategory(){
return new Action();
}
//define bean for Hollywood and inject dependency // hollywood is the bean id
@Bean
public Movie hollywood(){
return new Hollywood(getCategory());
}
}
---------------------------------------------------------------------------------------------------------------------
HollywoodConfigJavaDemo.java
---------------------------------------------------------------------------------------------------------------------
package com.demo.spring;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HollywoodConfigJavaDemo {
public static void main(String args[])
{
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(MovieConfig.class);
Movie obj=context.getBean("hollywood",Movie.class);
System.out.println(obj.getMovie());
System.out.println(obj.getCategory());
}
}
---------------------------------------------------------------------------------------------------------------------
OUTPUT
---------------------------------------------------------------------------------------------------------------------
Feb 27, 2018 3:14:45 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1963006a: startup date [Tue Feb 27 03:14:45 IST 2018]; root of context hierarchy
Graphics in Hollywood movies is a real-feel kind
Coding is the only Action i believe in
Feb 27, 2018 3:14:45 AM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1963006a: startup date [Tue Feb 27 03:14:45 IST 2018]; root of context hierarchy
Reading variables form a property file
literal injection using @PropertySource("classpath:bikesh.properties") &
@Value
---------------------------------------------------------------------------------------------------------------------
Hollywood.java
---------------------------------------------------------------------------------------------------------------------
package com.demo.spring;
import org.springframework.beans.factory.annotation.Value;
public class Hollywood implements Movie {
private Category cat;
@Value("${foo.name}")
private String name;
public String getName() {
return name;
}
public String getEmail() {
return email;
}
@Value("${foo.email}")
private String email;
public Hollywood(Category thecat){
cat=thecat;
}
@Override
public String getMovie() {
return "Graphics in Hollywood movies is a real-feel kind";
}
@Override
public String getCategory() {
return cat.getCategory();
}
}
---------------------------------------------------------------------------------------------------------------------
MovieConfig.java
---------------------------------------------------------------------------------------------------------------------
package com.demo.spring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:bikesh.properties")
public class MovieConfig {
//defining bean for action category
@Bean
public Category getCategory(){
return new Action();
}
//define bean for Hollywood and inject dependency
@Bean
public Movie hollywood(){
return new Hollywood(getCategory());
}
}
---------------------------------------------------------------------------------------------------------------------
Hollywood.java
---------------------------------------------------------------------------------------------------------------------
package com.demo.spring;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HollywoodConfigJavaDemo {
public static void main(String args[])
{
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(MovieConfig.class);
Movie obj=context.getBean("hollywood",Movie.class);
System.out.println(obj.getMovie());
System.out.println(obj.getCategory());
Hollywood obj1=context.getBean("hollywood",Hollywood.class);
System.out.println(obj1.getName());
System.out.println(obj1.getEmail());
context.close();
}
}
---------------------------------------------------------------------------------------------------------------------
OUTPUT
---------------------------------------------------------------------------------------------------------------------
Feb 27, 2018 3:15:26 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1963006a: startup date [Tue Feb 27 03:15:26 IST 2018]; root of context hierarchy
Graphics in Hollywood movies is a real-feel kind
Coding is the only Action i believe in
Bikesh Kumar
bikesh.kumar@tiwari.com
Feb 27, 2018 3:15:27 AM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1963006a: startup date [Tue Feb 27 03:15:26 IST 2018]; root of context hierarchy
No comments:
Post a Comment