SpringBoot (一) ---------- XML 和 JavaConfig


Spring 使用 XML 作为容器配置文件, 在 3.0 以后加入了 JavaConfig,使用 Java 类做配置文件使用。

一、什么是 JavaConfig ?

JavaConfig :是 Spring 提供的使用 java 类配置容器。 配置 Spring IOC 容器的纯 Java 方法

优点 :

  • 可以使用面向对象的方式,一个配置类可以继承配置类,可以重写方法
  • 避免繁琐的 xml 配置

二、XML 配置容器

1、新建 pre-boot 项目

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

2、pom.xml

<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>4.11</version>
	<scope>test</scope>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>5.3.1</version>
</dependency>

3、创建数据类 Student

新建 vo 包,在 vo 包下创建学生类

vo :值对象(Value Object)

用new关键字创建,有GC回收通常用于业务层之间的数据传递,一般是抽象出的业务对象,可以和数据表相对应,也可以不。在web层,对应一个web页面或者swt界面,用一个VO对象对应一个界面的值。

package com.fancy.vo;

public class Student {
    private Integer id;
    private String name;
    private Integer age;

    public Student() {
    }
    

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

4、配置文件

resources 目录下创建 Spring 的配置文件 applicationContext.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="myStudent" class="com.fancy.vo.Student">
         <property name="id" value="101"></property>
         <property name="name" value="张三"></property>
         <property name="age" value="18"></property>
     </bean>
</beans>

5、测试方法

@Test
public void test01 () {
    String config = "applicationContext.xml";
    ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
    Student student = (Student) ctx.getBean("myStudent");
    System.out.println("student = " + student);
}

在这里插入图片描述

三、JavaConfig 配置容器

JavaConfig 主要使用的注解 :

  • @Configuration :放在类的上面, 这个类相当于 xml 配置文件,可以在其中声明 bean

  • @Bean :放在方法的上面,方法的返回值是对象类型,这个对象注入到 Spring Ioc 容器

1、创建配置类(等同于 xml 配置文件)

新建 config 包,并把配置类 SpringConfig 放于此包下

package com.fancy.config;

import com.fancy.vo.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Date;


// @Configuration : 表示当前类是 xml 配置文件的作用
// 这个类中有很多方法, 方法的返回值是对象
// 在这个方法上面加入 @Bean, 表示方法返回值的对象放入到容器中
// @Bean == <bean> </bean>

@Configuration
public class SpringConfig {

    // 定义方法, 方法的返回值是对象
    // @Bean : 表示把对象注入到容器中
    // 位置 : 方法的上面
    // @Bean 没有使用属性, 默认对象名称是方法名

    @Bean
    public Student createStudent() {
        Student student = new Student();
        student.setId(1002);
        student.setName("李四");
        student.setAge(18);

        return student;

    }
    // name : 指定对象的名称
    @Bean
    public Student makeStudent() {
        Student student = new Student();
        student.setId(1003);
        student.setName("王五");
        student.setAge(19);

        return student;
    }

    @Bean
    public Date myDate() {
        return new Date();
    }
}

2、测试方法

@Test
public void test02() {
    // 没有 xml 配置文件, 使用 Java 代替 xml 配置文件的使用
    ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
    Student student = (Student) ctx.getBean("createStudent");
    System.out.println("student === " + student);
}
 @Test
 public void test03() {
     ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
     Student student = (Student) ctx.getBean("myStudent2");
     System.out.println("myStudent2 ==== " + student );

     Object myDate = ctx.getBean("myDate");

     System.out.println("myDate = " + myDate);
 }

在这里插入图片描述
在这里插入图片描述

四、@ImportResource

@ImportResource 是导入 xml 配置,等同于 xml 文件的 resources

1、创建数据类

package com.fancy.vo;

public class Cat {

    private String cardId;
    private String name;
    private Integer age;

    public Cat() {
    }

    public Cat(String cardId, String name, Integer age) {
        this.cardId = cardId;
        this.name = name;
        this.age = age;
    }

    public String getCardId() {
        return cardId;
    }

    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "cardId='" + cardId + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

2、创建配置文件 beans.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="myCat" class="com.fancy.vo.Cat">
        <property name="cardId" value="200101"></property>
        <property name="name" value="tomcat"></property>
        <property name="age" value="3"></property>
    </bean>
</beans>

3、创建配置类

package com.fancy.config;

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

@Configuration
@ImportResource(value = {"classpath:beans.xml", "classpath:applicationContext.xml"})
public class SystemConfig {
}

4、创建测试方法

@Test
public void test04() {
    ApplicationContext ctx = new AnnotationConfigApplicationContext(SystemConfig.class);
    Cat cat = (Cat)ctx.getBean("myCat");
    System.out.println("cat == " + cat);
}

在这里插入图片描述

五、@PropertyResource

@PropertyResource 是读取 properties 属性配置文件

1、创建 config.properties

tiger.name=hhh
tiger.age=6

在这里插入图片描述

2、创建数据类 Tiger

package com.fancy.vo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("tiger")
public class Tiger {
    @Value("${tiger.name}") 
    private String name;
    
    @Value("${tiger.age}")
    private Integer age;

    @Override
    public String toString() {
        return "Tiger{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

3、修改 SystemConfig 配置文件

package com.fancy.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource(value = "classpath:config.properties")
@ComponentScan(value = "com.fancy.vo")
public class SystemConfig {

}

4、测试方法

 @Test
 public void test05() {
     ApplicationContext ctx = new AnnotationConfigApplicationContext(SystemConfig.class);
     Tiger tiger = (Tiger) ctx.getBean("tiger");
     System.out.println("Tiger === " + tiger);
 }

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

在森林中麋了鹿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值