This document provides guidelines for replacing an XML configuration file with a Java configuration file in a Spring application. It discusses using annotations like @Configuration, @ComponentScan, @Bean, and @PropertySource to configure beans and read property values from an external properties file in a Java configuration. It also provides an example Java configuration class and a sample application to demonstrate its capabilities.
1. How to replace an XML Configuration file with a
Java Configuration file in a Spring application
This is a guideline/reference document that can be used to understand how to replace
an XML Spring Bean Configuration file with a Java Spring Bean Configuration file in a
Spring application. This is a reference document for developers.
Jayasree Perilakkalam
2. Introduction
• This is a reference document that addresses the details regarding how
to replace an XML configuration file with a Java configuration file in a
Spring application.
• This document can also be referenced to understand the steps for
configuration using a Java Configuration file for a Spring application.
• This document does not indicate that pure Java Configuration for a
Spring application per se is better than XML configuration. It will
depend on the scope of the application whether to choose one and
not the other.
• Any questions/suggestions/comments are welcome via the comments
section below.
4. Read “properties” from a file
• What is a property?
- Property is a key/value pair and is available in a file in the “resources” folder
in a Java/Spring based project
- It is entered in the format -> <key>=<value>
5. Read “properties” from a file
• Annotations used
- @PropertySource –> This is placed on classes annotated with @Configuration
and defines the “properties” file.
- @Value –> This is used to specify an expression on fields or methods. This
expression can be a placeholder and a property’s key can be specified here. We
can also specify default values for the property.
6. Read “properties” from a file
• A sample is as follows:
@Component(“examp”)
public class Example {
…
…
@Value(“${exampInt:111}”)
private int exampleInt;
@Value(“${nameProperty}”)
private String nameProp;
…
public String getNameProp() {
return nameProp;
}
…
}
7. Bean configuration
• Annotations used
- @Configuration -> The Java Configuration file will have @Configuration annotation.
- @ComponentScan -> This tells Spring to look for other configurations, services,
components in the package specified here. Spring will auto detect and register the
beans from the package specified. If the package specified does not exist, Spring will
scan the package of the class that has this annotation.
- @Bean -> This is placed on a method that instantiates the concrete class. It’s usually
a better practice to have return type as an interface type for this method. This serves
as the bean definition. The name as defined in the @Bean notation is same as “id” as
defined in the XML Configuration file.
- @Component -> This is used to enable auto-discovery of the particular bean (Java
object). When applied on a POJO class, Spring framework will auto-detect this class
for dependency injection.
- @EnableAspectJAutoProxy -> This is used on @Configuration classes. This enables
support for @Aspect annotation, similar to functionality found in Spring’s
<aop:aspectj-autoproxy> XML element.
8. Bean configuration
• A sample bean configuration is as follows:
@Configuration
@ComponentScan(basePackages=“com.examplestudy”)
@PropertySource(value={“classpath:properties/examp.properties”})
public class ExampleConfig {
…
…
@Bean(“name”)
private Name getName() {
return new Name(“Hello”, “Hello”);
}
…
}
9. A sample Spring Bean Configuration file
@Configuration
@ComponentScan(basePackages=“com.examplestudy”)
@PropertySource(value={“classpath:properties/examp.properties”})
public class ExampleConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
…
…
} This is a standard Spring bean. This is equivalent to <context:property-placeholder/> in a XML Config file. This is used
to resolve placeholders against the properties from a local file/environment variable/system properties.
10. A test app using the concepts learnt
• Run the following Java app
public class TestApp {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(ExampleConfig.class);
Name name = (Name) context.getBean(“name”);
System.out.println(name);
Example examp = (Example) context.getBean(“examp”);
System.out.println(examp.getNameProp();
((AnnotationConfigApplicationContext)context).close();
}
}
// a sample “Name” class is as follows:
public class Name {
private String firstName;
private String lastName;
public Name (String firstName. String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String toString() {
return firstName + “ “ + lastName;
}
}
11. Conclusion
• This document is meant to be a reference guide for developing re-
usable software components and loosely coupled applications.
• References I used to create this presentation are wiki pages/internet,
my enrolled programming related courses and my past work
experience.
Thank you