• 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>
            ......
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    创建如下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();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    package pojo;
    
    public interface Axe {
        public void chop();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    package pojo;
    
    public class StoneAxe implements Axe{
        @Override
        public void chop() {
            System.out.println("Stone axe!");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    package pojo;
    
    public class SteelAxe implements Axe{
        @Override
        public void chop() {
            System.out.println("Steel axe!");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    同理,创建如下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();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    package pojo;
    
    public interface Book {
        public void show();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    package pojo;
    
    public class PlayBook implements Book{
        @Override
        public void show() {
            System.out.println("Play book!");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    package pojo;
    
    public class StudyBook implements Book{
        @Override
        public void show() {
            System.out.println("Study book!");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    接下来,我们将用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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    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();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    创建 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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    <?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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    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 {
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    创建 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();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    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();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    创建 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();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    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();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    创建 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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    测试

    创建测试如下:

        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);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    运行测试,一切OK。

    组合配置的总结

    在xml配置中组合xml配置

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

    在Java配置中组合Java配置

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

    在xml配置中组合Java配置

       <context:annotation-config/>
    
       <bean class="config.MyConfig3"/>
    
    • 1
    • 2
    • 3

    在Java配置中组合xml配置

    @ImportResource("classpath:/applicationContext4.xml")
    
    • 1
  • 相关阅读:
    mysql8.0英文OCP考试第141-150题
    HMS Core助力宝宝巴士为全球开发者展现高品质儿童数字内容
    封装vue组件
    Flume基本使用--mysql数据输出
    java.lang.IllegalStateException: Failed to load ApplicationContext
    Springboot集成JDBC
    【Docker安装部署Zookeeper+Kafka集群详细教程、部署过程中遇到问题&解决方案、测试集群是否部署成功】
    Mac电脑风扇控制推荐 Macs Fan Control Pro 中文 for mac
    解决yolov5权重在linux和Windows上不兼容的问题
    2022年最新海南机动车签字授权人模拟考试及答案
  • 原文地址:https://blog.csdn.net/duke_ding2/article/details/125605817