1.简介
1.为什么使用SpringBoot?
-
Spring SpringMVC需要大量的配置,如xml文件
-
还需要配置对象,只有放到容器中才能进行使用
2.什么是Spring?
-
就是Spring Spring框架的集合
1.1什么是JavaConfig
@Configuration和@Bean注解
@Bean注解是将对象注入到容器中
@Autowired是将bean对象组合在一起,完成自动装配
1.javaConfig:使用Java类作为xml配置文件的替代,是配置spring容器的纯Java形式。可以将对象注入到容器中。
使用两个注解:
-
@Configuration,这个类作为配置文件使用,相当于xml文件。
-
@Bean:声明对象,将对象注入到容器中。
2.使用JavaConfig配置SpringBoot的配置
第一步:创建一个Student实体类
public class Student {
private int id;
private String name;
public Student() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
第二步:创建配置文件SpringConfig
-
@Configuration用于声明当前类是一个配置类
-
@Bean将对象注入到容器中,可以指定name属性,就是注入的bean的名称。
-
不指定bean的名称,默认就是方法的名字。
//声明是一个配置类
@Configuration
public class SpringConfig {
/*
*创建方法:方法返回值是对象,在方法的上面加上bean
* 方法的返回值就会注入到容器中
*
*/
@Bean
public Student createStudent(){
Student student=new Student();
student.setId(1);
student.setName("张三");
// 就会把student放到容器中
return student;
}
/*
*指定对象的名称
*/
@Bean(name = "lisiStu")
public Student createStudent2(){
Student student=new Student();
student.setId(2);
student.setName("李四");
// 就会把student放到容器中
return student;
}
}
1.2@ImportResource
1.@ImportResource导入xml配置。相当于写了一个JavaConfig文件。
2.如何导入:
//指定类路径下的配置文件
@ImportResource(value = "classpath:teacherXML.xml")
1.3@PropertySource
@PropertySource指定配置文件的位置。
@Value读取配置文件中的内容,使用的是表达式的方式。
@Component表示类的对象,交给spring容器管理
@Component作用于类,@Bean作用于方法
1.读取properties属性配置文件。使用的是key-value的格式。
2.实现方式
第一步:properties编写
school.id=1
school.name="xx大学"
第二步:编写类
@Component(value = "school")
public class School {
@Value(value = "${school.id}")
private int id;
@Value(value = "${school.name}")
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "School{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
public School() {
}
}
第三步:编写配置,在自定义的配置文件中加入下面两句话。
@PropertySource(value = "classpath:application.properties")
@ComponentScan(basePackages = "com.example.springboot001pre.pojo")
第四步:编写测试类
@Test
public void tes(){
ApplicationContext ctx=new AnnotationConfigApplicationContext(SpringConfig.class);
School school= (School) ctx.getBean("school");
System.out.println(school.toString());
}
2.SpringBoot起步
2.1特性
1.spring的一个成员。简化Spring,Spring MVC的使用。核心就是IOC容器。
-
创建spring项目
-
内嵌tomcat服务器等
-
提供了起步依赖,简化配置
-
尽可能配置spring和第三方的库
-
提供了健康检查和外部配置
-
不用配置xml
2.2创建Spring项目
1.初始化器的创建方式
起好项目名称,选择Jdk8,下一步,勾选创建web项目,finnish就可以创建完成。
2.3基于SpringBoot的web例子
1.案例
@RestController,相当于@ResponseBody + @Controller
@RequestMapping(value = "/hello"):映射的地址,以get方式请求
controller包下创建一个HelloController
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping(value = "/hello")
public String helloWorld(){
return "hello World";
}
}
运行截图:
2.4@SpringBootApplication注解
1.@SpringBootApplication
-
是一个复合注解
-
@SpringBootConfiguration,这个启动类可以当作配置文件用。
-
@EnableAutoConfiguration,可以实现自动配置,例如把mybaties配置好,放到容器中。
-
@ComponentScan,组件扫描器,默认扫描的包就是当前所在的包和子包。能扫描到所有的注解。