In this article, we will create a Hello World program in Spring4 with the XML configuration based approach.
We will be using Eclipse as the IDE and Maven for creating the project structure and adding needed dependencies.
Here are the steps:
Step 1: (Create a simple maven project)
Run the following command to create a Maven project :
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=com.topjavatutorial.spring -DartifactId=SpringXMLApp -DinteractiveMode=false
This will create the project with name “SpringXMLApp”.
If you go inside the SpringXMLApp folder, you should be able to find the generated pom.xml file.
Here is how the basic pom.xml would like at this point:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.topjavatutorial.spring</groupId> <artifactId>SpringXMLApp</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>SpringXMLApp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
This is just a basic pom.xml. In the next step, we will add spring dependency to it.
Step 2: (Add Spring dependency)
Add following spring-context dependency in the pom xml :
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
Here is how it looks like after this change :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.topjavatutorial.spring</groupId> <artifactId>SpringXMLApp</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>SpringXMLApp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.5.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
Now, the project is ready with spring configuration.
Step 3:(Import the project in Eclipse)
You can import the project in Eclipse in following ways:
- Import the project as a Maven project
(OR)
- Run mvn eclipse command as then import as normal project
If you need help importing the project to eclipse, refer the below article for the same :
Import Maven project in Eclipse
Step 4: (Create a class that you would access as a bean)
Lets create a package com.topjavatutorial.spring if not already present.
Create a class HelloService as follows :
package com.topjavatutorial.spring; public class HelloService { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
Step 5: (Create XML file containing bean definitions)
Create a xml bean definition file beans.xml in src/main/resources folder.
<?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.xsd"> <bean id="helloService" class="com.topjavatutorial.spring.HelloService"> <property name="name" value="TopJavaTutorial" /> </bean> </beans>
If the folder does not exist in your maven project, manually create the folder and update the project. Refer below article for this :
Missing src/main/resources in maven project
Step 6: (Create ApplicationContext from XML bean definitions)
Create an App class with main() method unless its already present in the package.
Inside the main method, instantiate the Spring Container by giving the XML bean definition file as the constructor argument.
Using the application context, you can access the HelloService methods.
package com.topjavatutorial.spring; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main( String[] args ) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); HelloService helloBean = (HelloService) context.getBean("helloService"); System.out.println("Hello " + helloBean.getName()); } }
Step 7: ( Run the program)
Running the App.java file will produce the following output :
Hello TopJavaTutorial
Understanding this program
In step 4, we created a HelloService class with methods getName() and setName().
Then in Step 5, we created an xml file containing the bean definitions. This is the most important file in this approach, as it contains the bean definitions and metadata information.
We added the file in src/main/resources since that is the path where maven expects resource files to be at and the folder is in classpath.
In Step 6, we created the ApplicationContext using the XML bean definition file. The class ClassPathXmlApplicationContext is used to load the xml configuration metadata from the classpath.
Next, using the ApplicationContext, we lookup the “helloService” bean and then execute the getName() method using it.
This program used XML based configuration for the Hello World program.
We can also use java based approach for the same.
Here is the link for the java based approach:
Spring 4 Hello World using java based configuration
© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post
#
#
#