XML方式配置Spring--Spring入门教程01

XML方式配置Spring

目标

本篇博客的主要目标是使用xml方式配置spring,由spring的IOC容器来实例化对象并设置对应的属性.
使用最简单直观的例子学习spring,了解spring的基本工作原理.

项目结构

测试效果

关键代码

Person.java

public class Person {
    private String name;
    private Integer age;
    private Car car;
    private List<Cat> cats;
    private Dog dog;

    public Person() {
        System.out.println("调用 Person()...");
    }

    public Person(String name, Integer age) {
        System.out.println(String.format(
        "调用 Person(String name, Integer age)[name=%s,age=%s]",
        name,age));
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println("调用 setName()...");
        this.name = name;
    }
    // TODO: 为了避免代码占用过多篇幅,省略了getter,setter和toString()
}

Car.java

public class Car {
    private String brand;
    private Integer maxSpeed;
    private Double price;
    private String color;

    public Car() {

    }

    public Car(String brand, Integer maxSpeed, Double price, String color) {
        this.brand = brand;
        this.maxSpeed = maxSpeed;
        this.price = price;
        this.color = color;
    }
    // TODO: 为了避免代码占用过多篇幅,省略了getter,setter和toString()
}

ApplicationContext_01.xml spring配置文件

<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- Person 相关对象注册 -->
    <!-- 调用无参构造器 -->
    <bean class="com.csdn.blog.bean.Person" id="person1"></bean>

    <!-- 调用无参构造器 -->
    <bean class="com.csdn.blog.bean.Person" id="person2">
        <!-- 调用setter方法 -->
        <property name="name" value="Jack2"/>
        <property name="age" value="30"/>
        <!--引用spring容器内都其他对象-->
        <property name="car" ref="car1"/>
    </bean>

    <!-- 调用无参构造器,调用setter方法-->
    <bean class="com.csdn.blog.bean.Person" id="person3" p:name="Jack3" p:age="30" p:car-ref="car1"></bean>

    <!-- 调用有参构造器-->
    <bean class="com.csdn.blog.bean.Person" id="person4">
        <!-- 按照有参构造器的参数顺序填写参数值 -->
        <constructor-arg value="Jack4"/>
        <constructor-arg value="30"/>
    </bean>

    <!--Car 相关对象注册-->
    <bean class="com.csdn.blog.bean.Car" id="car1" p:brand="BMW" p:color="red" p:maxSpeed="200" p:price="800000.00"></bean>
</beans>

pom.xml maven配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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>com.csdn.blog</groupId>
    <artifactId>spring01</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <spring.version>4.3.13.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

TestIOC.java junit测试类

public class TestIOC {

    @Test
    public void test01(){
        System.out.println("开始创建ApplicationContext,初始化IOC容器");
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("ApplicationContext_01.xml");

        System.out.println("开始根据spring 配置文件中注册的id获取对象");
        Person person1= (Person) applicationContext.getBean("person1");
        Person person2= (Person) applicationContext.getBean("person2");
        Person person3= (Person) applicationContext.getBean("person3");
        Person person4= (Person) applicationContext.getBean("person4");

        System.out.println("输出获得的对象");
        System.out.println(person1);
        System.out.println(person2);
        System.out.println(person3);
        System.out.println(person4);
    }
}

测试效果

这里写图片描述

重点内容

  1. ApplicationContext 可以理解为IOC容器,使用 new 关键字创建此对象就相当于初始化了IOC容器.
  2. 由于使用XML配置Spring,所以使用ApplicationContext的实现类 ClassPathXmlApplicationContext 作为具体的实例化类
  3. ClassPathXmlApplicationContext 实例化需要classpath下的spring配置文件路径作为参数.
  4. 根据运行效果可以看出,ApplicationContext 实例化完成后,配置文件 ApplicationContext_01.xml的对象也都调用了构造方法和对应的setter.
  5. ApplicationContext的对象中获得bean时,使用配置文件中的id作为参数
  6. ApplicationContext_01.xml定义了4个Person对象
    • person1 最简单bean配置,只需要配置id,class 相当于Person person1=new Person();
    • person2 是在person1的基础上设置属性,<property name="name" value="Jack2"/> 相当于person2.setName("Jack2");
    • person3 与person2作用原理是一致的,只是将<property name="name" value="Jack2"/> 简写为 p:name="Jack3"
    • person4 的配置相当于是 Person person4=new Person("Jack4",30);,每一个<constructor-arg value="Jack4"/> 就是一个构造方法的一个参数
    • 所用涉及的赋值的地方都有 value和ref两种,value用于赋值基本类型,ref用于将IOC容器内配置好的对象赋值给相应属性.
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 配置处理器(spring-boot-configuration-processor)是 Spring Boot 应用程序中使用的一个注释处理器,它可以帮助我们自动生成配置文件的帮助信息和代码提示。 下面是使用 Spring Boot 配置处理器的快速入门指南。 1. 添加依赖 在 `pom.xml` 文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> ``` 2. 添加注解 在需要生成配置文件帮助信息和代码提示的配置类上添加以下注解: ```java import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "myapp") public class MyAppProperties { // ... } ``` 这里的 `@ConfigurationProperties` 注解用于指定配置类的属性前缀,这样在配置文件中使用该前缀就可以自动完成属性的注入。 3. 生成帮助信息 在项目编译时,Spring Boot 配置处理器会自动扫描 `@ConfigurationProperties` 注解,并生成一个 `META-INF/spring-configuration-metadata.json` 文件。该文件包含了配置属性的帮助信息和默认值,可以被 IDE 用于代码提示和自动完成。 4. 使用代码提示 完成以上步骤后,在配置文件中输入前缀后,IDE 会自动提示可用的配置属性和默认值。 如,在 `application.properties` 文件中输入 `myapp.` 后,IDE 会提示可用的属性和默认值。 ```properties myapp.name=MyApp myapp.version=1.0.0 myapp.debug=false ``` 以上就是使用 Spring Boot 配置处理器的快速入门指南。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值