springboot_简单注解笔记_day01

1.boot文档

springboot入门文档:https://www.yuque.com/atguigu/springboot/lcfeme#dP5mj
springboot高级文档:https://niceseason.github.io/2020/04/18/springboot/

2.boot注解

@SpringBootApplication:这是一个SpringBoot应用
@RestController:相当于@ResponseBody + @Controller


想要改变扫描路径,@SpringBootApplication(scanBasePackages="com.atguigu")
或者@ComponentScan 指定扫描路径

@SpringBootApplication
等同于
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("com.atguigu.boot")


@Configuration(proxyBeanMethods = true) //告诉springboot这是一个配置类 == 配置文件

* Full(proxyBeanMethods = true)、【保证每个@Bean方法被调用多少次返回的组件都是单实例的】
* Lite(proxyBeanMethods = false)【每个@Bean方法被调用多少次返回的组件都是新创建的】
* 组件依赖必须使用Full模式默认。其他默认是否Lite模式

@Import({User.class, DBHelper.class})
* 给容器中自动创建出这两个类型的组件、默认组件的名字就是全类名

@Conditional
条件装配:满足Conditional指定的条件,则进行组件注入

@ConditionalOnBean(name = "tom"):如果配置类中包含tom对象,进行组件注入
@ConditionalOnMissingBean(name = "tom"):如果配置类中不包含tom对象,进行组件注入

@ImportResource("classpath:beans.xml")
原生配置文件引入


配置绑定
第一种在JavaBean里面写
@Component
@ConfigurationProperties(prefix = "mycar")

第二种在配置类里面写
@EnableConfigurationProperties(Car.class)
在JavaBean写
@ConfigurationProperties(prefix = "mycar")

Lombok简化JavaBean开发
引入lombok依赖
@NoArgsConstructor 无参
@AllArgsConstructor 有参
@Data getter and setter
@ToString toString
@EqualsAndHashCode requals

配置application.yml文件
单引号会将 作为字符串输出 ,双引号将 作为转义字符输出
在JavaBean中加上注解
@Component
@ConfigurationProperties(prefix = "person")

使用rest风格需要在yaml开启rest功能
spring:
  mvc:
    hiddenmethod:
      filter:
        enabled: true #开启页面表单的Rest功能


@GetMapping("/user")
@PostMapping("/user")
@DeleteMapping("/user")
@PutMapping("/user")
<input type="hidden" name="_method" value="PUT">

@Bean //给容器中添加组件。以方法名作为组件的id.返回类型就是组件类型。返回的值,就是组件在容器中的实例
public User user01(){
return new User("张三",18);
}


三、
在main方法中运行:
SpringApplication.run(MainApplication.class,args);

//1.返回我们IOC容器
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
//2.查看容器里的组件
String[] names = run.getBeanDefinitionNames();


简化配置:创建 application.properties 文件

原文地址:https://www.cnblogs.com/LEPENGYANG/p/15321145.html