初学SpringBoot--ch01-Xml和JavaConfig


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

1.1 为什么要用 Spring Boot

因为Spring, SpringMVC 需要使用的大量的配置文件,还需要配置各种对象,把使用的对象放入到spring容器中才能使用对象,需要了解其他框架配置规则。
SpringBoot 就相当于不需要配置文件的Spring+SpringMVC。常用的框架和第三方库都已经配置好了,拿来就可以使用。
SpringBoot开发效率高,使用方便简单。

1.2 什么是 JavaConfig

JavaConfig: 是 Spring 提供的使用 java 类配置容器。 配置 Spring IOC 容器的纯 Java 方法,使用java类作为xml配置文件的替代。

优点:
1.可以使用面像对象的方式, 一个配置类可以继承配置类,可以重写方法
2.避免繁琐的 xml 配置

使用两个注解:
1)@Configuration : 放在一个类的上面,表示这个类是作为配置文件使用的。
2)@Bean:声明对象,把对象注入到容器中。

1.3 Xml 配置容器

创建普通的Maven项目:ch01-springboot-pre

1.3.1 maven依赖

<dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.1</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
    </plugins>
  </build>

1.3.2 创建数据类型Student

package com.suyv.vo;

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

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

    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;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

1.3.3 创建spring配置文件

	<bean id="myStudent" class="com.suyv.vo.Student">
        <property name="name" value="李四"/>
        <property name="age" value="18"/>
        <property name="sex" value=""/>
    </bean>

1.3.4 单元测试

	//使用xml配置文件
    @Test
    public void test01(){
        String config = "beans.xml";
        ApplicationContext cx = new ClassPathXmlApplicationContext(config);
        Student stu = (Student) cx.getBean("myStudent");
        System.out.println(stu);
    }

1.4 JavaConfig 配置容器

JavaConfig 主要使用的注解

@Configuration:放在类的上面, 这个类相当于 xml 配置文件,可以在其中声明 bean。
@Bean:放在方法的上面, 方法的返回值是对象类型, 这个对象注入到 spring ioc 容器。

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

package com.suyv.config;

import com.suyv.vo.Student;
import org.springframework.context.annotation.*;

/**
 * @Configuration :表示当前类是  xml  配置文件的作用
 * 在这个类中有很多方法, 方法的返回值是对象。
 * 在这个方法的上面加入@Bean, 表示方法返回值的对象放入到容器中。
 * @Bean == <bean></bean>
 */
@Configuration
public class SpringConfig {
    /**
     * @Bean :表示把对象注入到容器中。
     * 		位置: 方法的上面
     * @Bean  没有使用属性,默认对象名称是方法名
     */
    @Bean
    public Student createStudent(){
        Student student =  new Student();
        student.setName("张三");
        student.setAge(29);
        student.setSex("男");
        return student;
    }

    // name :指定对象的名称
    @Bean(name = "myStudent2")
    public Student makeStudent(){
        Student student =  new Student();
        student.setName("李四");
        student.setAge(30);
        student.setSex("男");
        return student;
    }
}

1.4.2 测试方法

	//使用 JavaConfig
    @Test
    public void test02(){
        //没有 xml 配置文件,使用 java 类代替 xml 配置文件 的作用
        ApplicationContext cx =  new AnnotationConfigApplicationContext(SpringConfig.class);
        Student student = (Student) cx.getBean("createStudent");
        System.out.println(student);
    }

    @Test
    public void test03(){
        ApplicationContext cx =  new AnnotationConfigApplicationContext(SpringConfig.class);
        Student student = (Student) cx.getBean("myStudent2");
        System.out.println(student);
    }

1.5 @ImportResource

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

<import resources="其他配置文件"/>

1.5.1 创建数据类

package com.suyv.vo;

public class Cat {

    private String CatId;
    private String name;
    private Integer age;

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

    public String getCatId() {
        return CatId;
    }

    public void setCatId(String catId) {
        CatId = catId;
    }

    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;
    }
}

1.5.2 创建配置文件

    <bean id="myCat" class="com.suyv.vo.Cat">
        <property name="catId" value="T123654889"/>
        <property name="name" value="Tom"/>
        <property name="age" value="2"/>
    </bean>

1.5.3 创建配置类

package com.suyv.config;

import org.springframework.context.annotation.*;

@Configuration
@ImportResource(value = "classpath:applicationCpntext.xml")
public class SpringConfig {

}

1.5.4 测试方法

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

1.6 @PropertyResource

@PropertyResource: 读取properties属性配置文件。 使用属性配置文件可以实现外部化配置 ,在程序代码之外提供数据。

1.6.1 创建 config.properties

tiger.name=东北虎
tiger.age=5

1.6.2 创建数据类

package com.suyv.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 +
                '}';
    }

}

1.6.3 创建配置类

package com.suyv.config;

import org.springframework.context.annotation.*;

@Configuration
@ComponentScan(basePackages = "com.suyv.vo")
@PropertySource(value = "classpath:config.properties")
public class SpringConfig {

}

1.6.4 测试方法

    @Test
    public void test05(){
        ApplicationContext cx =  new AnnotationConfigApplicationContext(SpringConfig.class);
        Tiger tiger = (Tiger) cx.getBean("tiger");
        System.out.println(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、付费专栏及课程。

余额充值