springboot2——7.总结

01、Spring与SpringBoot · 语雀

1. 打包时在pom.xml中加入

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
</build>

再利用maven点击package,就会打包好jar,在target文件夹,

用cmd运行java -jar boot-01-helloworld-1.0-SNAPSHOT.jar,既可以运行helloworld工程项目。

将jar包直接在目标服务器执行即可。


2. 注解:

① @Bean:给容器中添加组件,以方法名作为组件的id,返回类型是组件类型,返回的值就是组件在容器的实例。

② @Configuration:告诉springboot这是一个配置类 == 配置文件

其中的属性proxyBeanMethods:代理bean的方法

Full(proxyBeanMethods = true)【保证每个@Bean方法被调用多少次返回的组件都是单实例的】 

Lite(proxyBeanMethods = false)【每个@Bean方法被调用多少次返回的组件都是新创建的】

组件依赖(组件之间存在相互调用)必须使用Full模式默认,其他默认是Lite模式(启动更快)

③ @ImportResource("classpath:beans.xml")

原始的spring是xml文件形式,直接添加以上语句能够导入spring配置文件从而生效

④ @ConfigurationProperties(prefix = "")配置绑定

原来的如果application.properties文件中有很多行配置文件,原来的配置需要利用正则表达式去遍历,很不方便,这里的springboot利用以上注解,能够自动从文件中提取前缀为prefix的配置

⑤ @SpringBootConfiguration

代表当前是一个配置类(点进去是@Configuration)

⑥ @ComponentScan(“”)

扫描包

⑦ @Controller, @Service, @Repository,@Component

目前4种注解意思是一样,并没有什么区别,区别只是名字不同。】

⑧ @EnableAutoConfiguration非常重要,整个springboot自动配置的汇总

        其中包括:

        @AutoConfigurationPackage自动配置包,指定了默认的包规则。

        @Import(AutoConfigurationImportSelector.class)批量导入一些组件


3. 自动配置原理:

  • SpringBoot先加载所有的自动配置类 xxxxxAutoConfiguration
  • 每个自动配置类按照条件进行生效,默认都会绑定配置文件指定的值。xxxxProperties里面拿。xxxProperties和配置文件进行了绑定
  • 生效的配置类就会给容器中装配很多组件
  • 只要容器中有这些组件,相当于这些功能就有了
  • 定制化配置
  • 用户直接自己@Bean替换底层的组件
  • 用户去看这个组件是获取的配置文件什么值就去修改。

xxxxxAutoConfiguration ---> 组件 ---> xxxxProperties里面拿值 ----> application.properties


4. 开发简单工具:

Lombok

===============================简化JavaBean开发===================================
@NoArgsConstructor
//@AllArgsConstructor
@Data
@ToString
@EqualsAndHashCode
public class User {
    private String name;
    private Integer age;
    private Pet pet;
    public User(String name,Integer age){
        this.name = name;
        this.age = age;
    }
}
================================简化日志开发===================================
@Slf4j
@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String handle01(@RequestParam("name") String name){
        log.info("请求进来了....");
        return "Hello, Spring Boot 2!"+"你好:"+name;
    }
}

dev-tools:热部署 ctrl+F9 Build即可应用更改

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

5. 静态资源如何绑定的?

  • SpringBoot启动默认加载 xxxAutoConfiguration 类(自动配置类)
  • SpringMVC已经被WebMvcAutoConfiguration类自动配置了,并且自动绑定了配置
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
		ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {}

在其中存在着一个静态类,一定注意到@EnableConfigurationPropoties注解,这个注解规定了“配置文件的相关属性和WebMvcProperties.class, ResourceProperties.class 进行了绑定”。点进去,就能看见规定的前缀

	@Configuration(proxyBeanMethods = false)
	@Import(EnableWebMvcConfiguration.class)
	@EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class })
	@Order(0)
	public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer {}

@EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class })

  • WebMvcProperties==spring.mvc、ResourceProperties==spring.resources
  • 即可在application.yaml中进行修改初始参数
public WebMvcAutoConfigurationAdapter(ResourceProperties resourceProperties, WebMvcProperties mvcProperties,
        ListableBeanFactory beanFactory, ObjectProvider<HttpMessageConverters> messageConvertersProvider,
        ObjectProvider<ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizerProvider,
        ObjectProvider<DispatcherServletPath> dispatcherServletPath,
        ObjectProvider<ServletRegistrationBean<?>> servletRegistrations) {
      this.resourceProperties = resourceProperties;
      this.mvcProperties = mvcProperties;
      this.beanFactory = beanFactory;
      this.messageConvertersProvider = messageConvertersProvider;
      this.resourceHandlerRegistrationCustomizer = resourceHandlerRegistrationCustomizerProvider.getIfAvailable();
      this.dispatcherServletPath = dispatcherServletPath;
      this.servletRegistrations = servletRegistrations;
    }

 再注意到这个WebMvcAutoConfigurationAdapter仅有一个有参构造器,参数列表为:

  • ResourceProperties resourceProperties 获取和spring.resources绑定的所有的值的对象
  • WebMvcProperties mvcProperties 获取和spring.mvc绑定的所有的值的对象
  • ListableBeanFactory beanFactory Spring的beanFactory(IOC容器)
  • HttpMessageConverters 找到所有的HttpMessageConverters
  • ResourceHandlerRegistrationCustomizer 找到资源处理器的自定义器。
  • DispatcherServletPath
  • ServletRegistrationBean 给应用注册Servlet、Filter....

6. 提取网址中的参数:

@GetMapping("/car/{id}/owner/{username}")
public Map<String,Object> getCar(@PathVariable("id") Integer id,
                                 @PathVariable("username") String name,
                                 @PathVariable Map<String,String> pv)

假设网址是 localhost:8080/car/3/owner/lisi?age=18&inters=basketball&inters=game

获取?后的参数:

    @GetMapping("/car/{id}/owner/{username}")
    public Map<String,Object> getCar(@RequestParam("age") Integer age,
                                     @RequestParam("inters") List<String> inters,
                                     @RequestParam Map<String,String> params)

7. thymeleaf语法

05、Web开发 · 语雀


8. 整合mybatis(包括JDBC场景,一起引了)

        1. 在application.properties中添加:

mybatis:
  mapper-locations: classpath:mybatis/mapper/*.xml

        2. 编写接口文件,标注@Mapper注解

        3. 编写xml文件


9. 整合mybatis—plus(包括JDBC场景,一起引了)

        1. mybatis-plus:XXX就是对mybatis-plus的定制

        2. sqlSessionFactory自动配置好,mapperLocations自动配置好,默认值classpath*:/mapper/**/*.xml

         3. 比如写UserMapper接口时,需要继承BaseMapper<T>,好处是能够直接利用其中的CRUD方法,无需编写xml文件了。

举例:

        UserMapper.java只写如下信息,中间方法不写,xml方法也不写。

 因为baseMappper已经有了方法如下:

直接在需要使用的地方:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值