Spring Framework | XML vs Java Configuration
Before you start any Spring Framework project, you should consider using Spring Boot to simplify configuration.
https://start.spring.io is the place to start. Many IDEs also integrate with start.spring.io.
If you have legacy applications or otherwise like to torture yourself play with xml configuration then here is an example setup.
- Use the Spring BOM (Bill of Materials) using a dependency tool like Maven or Gradle to manage versions of various dependencies.
- Add your xml configuration file in the classpath (depends on whether you are building Web or regular Java jar application).
- Example class Greeting.java
class Greeting {
public String greeting(String name) {
return String.format("Hello %s", name);
}
}
XML configuration;
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<bean id="greeting" class="com.efeyopixel.Greeting">
<property name="name" value="spring fan!"></property>
</bean>
</beans>
Comments
Post a Comment