Xml Based Spring Configuration Example

This example will tell you how to use STS ( Spring Tool Suite based on Eclipse ) to create a spring project use xml configuration.

1. Create Spring Maven Project Use STS.

  1. Open STS, click File —> New —> Others menu item. Input Spring in the popup dialog search text box and select Spring Legacy Project.
    sts spring legacy project wizard
  2. Click Next button, select Simple Spring Maven in the next dialog. Click Finish button, now the spring maven project has been created.
    sts spring legacy project simple spring maven

2. Create Java Bean And Spring Bean Configuration Xml File.

Now the legacy spring maven project has been created, below is the project files structures, we will introduce them one by one.

xml based spring project files structure

2.1 Edit Pom.xml

First edit pom.xml file with below code.

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.springframework.samples</groupId>
  <artifactId>XmlBasedSpringProject</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <!-- Spring framework used constants variable  -->
  <properties>
     <spring.version>5.0.0.BUILD-SNAPSHOT</spring.version>
     <servlet.api.version>3.1.0</servlet.api.version>
  </properties>

  <repositories>
    <!-- This repository is where the spring framework dependencies jar file download. -->
    <repository>
      <id>spring-snapshots</id>
      <name>Spring Snapshots</name>
      <url>https://repo.spring.io/libs-snapshot</url>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
     </repository>
  </repositories>

  <dependencies>
     <!-- Below are the basic spring container dependencies library. -->
     <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
     </dependency>

     <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
     </dependency>

     <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
     </dependency>

  </dependencies>
</project>

2.2 Create Java Class UserAccountBean In Package com.dev2qa

package com.dev2qa;

public class UserAccountBean {

   private String userName;
   private String password;
   private String email;

   public String getUserName() {
      return userName;
   }

   public void setUserName(String userName) {
      this.userName = userName;
   }

   public String getPassword() {
      return password;
   }

   public void setPassword(String password) {
      this.password = password;
   }

   public String getEmail() {
      return email;
   }

   public void setEmail(String email) {
      this.email = email;
   }

   public UserAccountBean(String userName) {
      super();
      this.userName = userName;
   }

   public String sayHello(){
       String ret = "Hello " + userName + ", Password is " + password + ", Email is " + email;
       return ret;
   }
}

2.3 Create Bean Configuration Xml File in Class Path ( src/main/resources folder).

  1. Right click src/main/resources folder.
  2. Click News —> Others menu item, input Spring in the search text box. Select Spring Bean Configuration File.
    create spring bean configuration file dialog
  3. Input BeansConfiguration in the bean configuration file name text box.
    input spring bean configuration file name BeansConfiguration
  4. Select beans xml namespace in the next dialog.
    select beans xml namespace for spring beans configuration file
  5. Click Next and Finish to complete the wizard.
READ :   Spring Bean Initialization And Destruction Examples

3. Add Java Beans In Bean Configuration Xml File.

We have created both a java bean class and the spring bean configuration xml file in step 2. Now we will add the java bean to the bean configuration file to manage it with spring container.

  1. Double click BeansConfiguration.xml file to open it in right panel.
  2. Click beans tab at right panel bottom.
    manage beans in spring beans configuration xml file
  3. Click New Bean button to add an exist java bean as the spring container managed java bean.
    sts add new spring bean definition
  4. Click Next button to go to spring bean constructor arguments and properties default value edit dialog.
    sts add new spring bean constructor parameters and properties default values
  5. Click Finish button, now the spring container managed java bean has been created.

4. Invoke Spring Container Managed Java Bean.

Now create a java class com.dev2qa.TestUserAccountBean and write below code in it.

package com.dev2qa;

   import org.springframework.context.ApplicationContext;
   import org.springframework.context.support.ClassPathXmlApplicationContext;
   
   public class TestUserAccountBean {

      public static void main(String[] args) {

      // Initialize spring beans from bean configuration xml file.
      ApplicationContext context = new ClassPathXmlApplicationContext("BeansConfiguration.xml");
      // Get user accout bean.
      UserAccountBean userAccountBean = (UserAccountBean)context.getBean("UserAccountBean");

      // Invoke spring managed bean method
      String ret = userAccountBean.sayHello();
      System.out.println(ret);

      // Change spring bing property valu.
      userAccountBean.setUserName("Richard");
      userAccountBean.setEmail("[email protected]");
      userAccountBean.setPassword("111111");
      ret = userAccountBean.sayHello();
      System.out.println(ret);
   }
}

5. Run Example.

Right click TestUserAccount.java file, click Run As —> Java Application. You can get below results.

Jul 28, 2018 8:34:36 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org[email protected]45ff54e6: startup date [Sat Jul 28 20:34:36 CST 2018]; root of context hierarchy
Jul 28, 2018 8:34:36 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [BeansConfiguration.xml]
Hello Jerry, Password is 666666, Email is [email protected]
Hello Richard, Password is 111111, Email is [email protected]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.