1.1 为什么要用 Spring Boot1.2 什么是 JavaConfig1.3 Xml 配置容器
1.3.1 maven依赖1.3.2 创建数据类型Student1.3.3 创建spring配置文件1.3.4 单元测试 1.4 JavaConfig 配置容器
1.4.1 创建配置类(等同于 xml 配置文件)1.4.2 测试方法 1.5 @importResource
1.5.1 创建数据类1.5.2 创建配置文件1.5.3 创建配置类1.5.4 测试方法 1.6 @PropertyResource
1.6.1 创建 config.properties1.6.2 创建数据类1.6.3 创建配置类1.6.4 测试方法
Spring 使用 Xml 作为容器配置文件, 在 3.0 以后加入了 JavaConfig. 使用 java 类做配置文件使用。
1.2 什么是 JavaConfig因为Spring, SpringMVC 需要使用的大量的配置文件,还需要配置各种对象,把使用的对象放入到spring容器中才能使用对象,需要了解其他框架配置规则。
SpringBoot 就相当于不需要配置文件的Spring+SpringMVC。常用的框架和第三方库都已经配置好了,拿来就可以使用。
SpringBoot开发效率高,使用方便简单。
JavaConfig: 是 Spring 提供的使用 java 类配置容器。 配置 Spring IOC 容器的纯 Java 方法,使用java类作为xml配置文件的替代。
优点:
1.可以使用面像对象的方式, 一个配置类可以继承配置类,可以重写方法
2.避免繁琐的 xml 配置
使用两个注解:
1)@Configuration : 放在一个类的上面,表示这个类是作为配置文件使用的。
2)@Bean:声明对象,把对象注入到容器中。
创建普通的Maven项目:ch01-springboot-pre
1.3.1 maven依赖1.3.2 创建数据类型Studentorg.springframework spring-context 5.3.1 junit junit 4.12 maven-compiler-plugin 3.5.1 1.8 1.8 UTF-8
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配置文件
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 主要使用的注解
1.4.1 创建配置类(等同于 xml 配置文件)@Configuration:放在类的上面, 这个类相当于 xml 配置文件,可以在其中声明 bean。
@Bean:放在方法的上面, 方法的返回值是对象类型, 这个对象注入到 spring ioc 容器。
package com.suyv.config;
import com.suyv.vo.Student;
import org.springframework.context.annotation.*;
@Configuration
public class SpringConfig {
@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配置。
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 创建配置文件
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.propertiestiger.name=东北虎 tiger.age=51.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);
}



