Live2D

springboot学习第一天

自动配置原理

//@ImportResource(locations = {"classpath:beans.xml"}) 可以加入配置文件
// springboot 不建议 建议全注解
// 来标注这是springboot的主程序
@SpringBootApplication
public class helloworldmain {
/*
* 自动配置原理 加载主运行类后 开启自动配置功能
* @EnableAutoConfiguration
开启自动配置
里面有自动导入包的配置 autopconfigurationpackage 将springbootapplication所在包极其子包全部扫扫描
*properties配置文件配置特定类的值,因为配置类(xxxxProperties)上标注着@ConfigurationProperties(prefix = "spring.http.encoding")
* @EnableConfigurationProperties(HttpEncodingProperties.class) 意为启动特定类的(ConfigurationProperties)配置功能,将配置文件中的值映射注入
* 通过构造器将配置类注入到当前类中
*根据当前类的@ConditionalOnProperty(prefix = "spring.http.encoding", value = "enabled", matchIfMissing = true)
* 验证条件满足的情况下,就会将容器中添加组件
*
* */
public static void main(String[] args) {
//必须要运行springbootapplication注解的类 springboot应用
SpringApplication.run(helloworldmain.class,args);
}
}



通过配置文件将类注册到bean
@Component
//@PropertySource(value={"classpath:depart.properties"}) 从特殊文件中获取值
//@ConfigurationProperties(prefix = "Department")
@Configuration
public class Department {

/*
* @value("12344")
* @value("${配置文件中的值}")
* */
private Integer id;
private String departmentName;

通过配置类的方式
@Configuration // 这是一个配置类
public class MyConfig {

@Bean // 给容器添加组件,并且id为方法名
public Department hellodepart01(){
System.out.println("配置类添加了——————————");
return new Department();
}
}

controller和restcontroller
//@Controller
@RestController // 此注解即可不写responsenody
public class firstdaycontroller {
@GetMapping("/query")
public String helloworld(){
return "helloworld";
}
}
posted @ 2020-03-02 21:44  丁丁丁冲鸭丶  阅读(172)  评论(0)    收藏  举报