Spring的xml配置和Java配置

注:关于Spring组合配置的用法,可以直接看文末总结。

环境

  • Ubuntu 22.04
  • IntelliJ IDEA 2022.1.3
  • JDK 17.0.3
  • Spring 5.3.21

准备

创建Maven项目 test0704。

修改 pom.xml 文件,添加依赖:

        ......
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.21</version>
        </dependency>
        ......

创建如下POJO:

  • Person :Person持有Axe;
  • Axe :Axe接口;
  • StoneAxe :Axe实现类;
  • SteelAxe :Axe实现类;
package pojo;

public class Person {
    private String name;
    private Axe axe;

    public void setAxe(Axe axe) {
        this.axe = axe;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void useAxe() {
        System.out.println("I am " + name);
        axe.chop();
    }
}
package pojo;

public interface Axe {
    public void chop();
}
package pojo;

public class StoneAxe implements Axe{
    @Override
    public void chop() {
        System.out.println("Stone axe!");
    }
}
package pojo;

public class SteelAxe implements Axe{
    @Override
    public void chop() {
        System.out.println("Steel axe!");
    }
}

同理,创建如下POJO:

  • Student :Student持有Book;
  • Book :Book接口;
  • PlayBook :Book实现类;
  • StudyBook :Book实现类;
package pojo;

public class Student {
    private String name;
    private Book book;

    public void setName(String name) {
        this.name = name;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    public void readBook() {
        System.out.println("I am " + name);
        book.show();
    }
}
package pojo;

public interface Book {
    public void show();
}
package pojo;

public class PlayBook implements Book{
    @Override
    public void show() {
        System.out.println("Play book!");
    }
}
package pojo;

public class StudyBook implements Book{
    @Override
    public void show() {
        System.out.println("Study book!");
    }
}

接下来,我们将用Spring来配置这些POJO。

配置Spring

1. xml配置

创建 applicationContext1.xml 文件如下:

<?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="stoneAxe" class="pojo.StoneAxe"/>

   <bean id="steelAxe" class="pojo.SteelAxe"/>

   <bean id="person" class="pojo.Person">
      <property name="name" value="Tom"/>
      <property name="axe" ref="stoneAxe"/>
   </bean>

   <bean id="playBook" class="pojo.PlayBook"/>

   <bean id="studyBook" class="pojo.StudyBook"/>

   <bean id="student" class="pojo.Student">
      <property name="name" value="Jerry"/>
      <property name="book" ref="playBook"/>
   </bean>
</beans>

2. Java配置

创建 MyConfig1.java 如下:

package config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import pojo.*;

@Configuration
public class MyConfig1 {
    @Value("Tom")
    String personName;

    @Value("Jerry")
    String studentName;

    @Bean
    public Person person() {
        var person = new Person();
        person.setName(personName);
        person.setAxe(stoneAxe());

        return person;
    }

    @Bean
    public StoneAxe stoneAxe() {
        return new StoneAxe();
    }

    @Bean
    public SteelAxe steelAxe() {
        return new SteelAxe();
    }

    @Bean
    public Student student() {
        var student = new Student();
        student.setName(studentName);
        student.setBook(playBook());

        return student;
    }

    @Bean
    public PlayBook playBook() {
        return new PlayBook();
    }

    @Bean
    public StudyBook studyBook() {
        return new StudyBook();
    }

}

3. xml的组合配置

创建 applicationContext2.xml 文件如下:

<?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">

   <import resource="classpath:applicationContext2_person.xml" />
   <import resource="classpath:applicationContext2_student.xml" />
</beans>

创建 applicationContext2_person.xmlapplicationContext2_student.xml 文件如下:

<?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="stoneAxe" class="pojo.StoneAxe"/>

   <bean id="steelAxe" class="pojo.SteelAxe"/>

   <bean id="person" class="pojo.Person">
      <property name="name" value="Tom"/>
      <property name="axe" ref="stoneAxe"/>
   </bean>

</beans>
<?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="playBook" class="pojo.PlayBook"/>

   <bean id="studyBook" class="pojo.StudyBook"/>

   <bean id="student" class="pojo.Student">
      <property name="name" value="Jerry"/>
      <property name="book" ref="playBook"/>
   </bean>

</beans>

4. Java的组合配置

创建 MyConfig2.java 文件如下:

package config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import({MyConfig2Person.class, MyConfig2Student.class})
public class MyConfig2 {

}

创建 MyConfig2Person.javaMyConfig2Student.java 文件如下:

package config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import pojo.*;

@Configuration
public class MyConfig2Person {
    @Value("Tom")
    String name;

    @Bean
    public Person person() {
        var person = new Person();
        person.setName(name);
        person.setAxe(stoneAxe());

        return person;
    }

    @Bean
    public StoneAxe stoneAxe() {
        return new StoneAxe();
    }

    @Bean
    public SteelAxe steelAxe() {
        return new SteelAxe();
    }

}
package config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import pojo.*;

@Configuration
public class MyConfig2Student {
    @Value("Jerry")
    String name;

    @Bean
    public Student student() {
        var student = new Student();
        student.setName(name);
        student.setBook(playBook());

        return student;
    }

    @Bean
    public PlayBook playBook() {
        return new PlayBook();
    }

    @Bean
    public StudyBook studyBook() {
        return new StudyBook();
    }

}

5. xml配置组合Java配置(入口为xml)

创建 applicationContext3.xml 文件如下:

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

   <bean id="stoneAxe" class="pojo.StoneAxe"/>

   <bean id="steelAxe" class="pojo.SteelAxe"/>

   <bean id="person" class="pojo.Person">
      <property name="name" value="Tom"/>
      <property name="axe" ref="stoneAxe"/>
   </bean>

   <context:annotation-config/>

   <bean class="config.MyConfig3"/>

</beans>

创建 MyConfig3.java 文件如下:

package config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import pojo.*;

@Configuration
public class MyConfig3 {
    @Value("Jerry")
    String name;

    @Bean
    public Student student() {
        var student = new Student();
        student.setName(name);
        student.setBook(playBook());

        return student;
    }

    @Bean
    public PlayBook playBook() {
        return new PlayBook();
    }

    @Bean
    public StudyBook studyBook() {
        return new StudyBook();
    }

}

6. Java配置组合xml配置(入口为Java)

创建 MyConfig4.java 文件如下:

package config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import pojo.Person;
import pojo.SteelAxe;
import pojo.StoneAxe;

@Configuration
@ImportResource("classpath:/applicationContext4.xml")
public class MyConfig4 {
    @Value("Tom")
    String name;

    @Bean
    public Person person() {
        var person = new Person();
        person.setAxe(stoneAxe());
        person.setName(name);

        return person;
    }

    @Bean
    public StoneAxe stoneAxe() {
        return new StoneAxe();
    }

    @Bean
    public SteelAxe steelAxe() {
        return new SteelAxe();
    }
}

创建 applicationContext4.xml 文件如下:

<?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="playBook" class="pojo.PlayBook"/>

   <bean id="studyBook" class="pojo.StudyBook"/>

   <bean id="student" class="pojo.Student">
      <property name="name" value="Jerry"/>
      <property name="book" ref="playBook"/>
   </bean>

</beans>

测试

创建测试如下:

    private void testCtx(ApplicationContext ctx) {
        var person = ctx.getBean("person", Person.class);

        person.useAxe();

        var student = ctx.getBean("student", Student.class);

        student.readBook();
    }

    @Test
    public void test1() {
        var ctx = new ClassPathXmlApplicationContext("applicationContext1.xml");

        testCtx(ctx);
    }

    @Test
    public void test2() {
        var ctx = new AnnotationConfigApplicationContext(MyConfig1.class);

        testCtx(ctx);
    }

    @Test
    public void test3() {
        var ctx = new ClassPathXmlApplicationContext("applicationContext2.xml");

        testCtx(ctx);
    }

    @Test
    public void test4() {
        var ctx = new AnnotationConfigApplicationContext(MyConfig2.class);

        testCtx(ctx);
    }

    @Test
    public void test5() {
        var ctx = new ClassPathXmlApplicationContext("applicationContext3.xml");

        testCtx(ctx);
    }

    @Test
    public void test6() {
        var ctx = new AnnotationConfigApplicationContext(MyConfig4.class);

        testCtx(ctx);
    }

运行测试,一切OK。

组合配置的总结

在xml配置中组合xml配置

   <import resource="classpath:applicationContext2_person.xml" />
   <import resource="classpath:applicationContext2_student.xml" />

在Java配置中组合Java配置

@Import({MyConfig2Person.class, MyConfig2Student.class})

在xml配置中组合Java配置

   <context:annotation-config/>

   <bean class="config.MyConfig3"/>

在Java配置中组合xml配置

@ImportResource("classpath:/applicationContext4.xml")

版权声明:本文为duke_ding2原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。