栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring的xml配置和Java配置

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring的xml配置和Java配置

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

环境
  • Ubuntu 22.04
  • IntelliJ IDEA 2022.1.3
  • JDK 17.0.3
  • Spring 5.3.21
准备

创建Maven项目 test0704。

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

        ......
        
        
            junit
            junit
            4.13.2
            test
        

        
        
            org.springframework
            spring-webmvc
            5.3.21
        
        ......

创建如下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 文件如下:




   

   

   
      
      
   

   

   

   
      
      
   

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 文件如下:




   
   

创建 applicationContext2_person.xml 和 applicationContext2_student.xml 文件如下:




   

   

   
      
      
   





   

   

   
      
      
   


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.java 和 MyConfig2Student.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 文件如下:




   

   

   
      
      
   

   

   


创建 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 文件如下:




   

   

   
      
      
   


测试

创建测试如下:

    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配置
   
   
在Java配置中组合Java配置
@Import({MyConfig2Person.class, MyConfig2Student.class})
在xml配置中组合Java配置
   

   
在Java配置中组合xml配置
@ImportResource("classpath:/applicationContext4.xml")
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1006314.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号