赞
踩
springboot是用于整合springframwork以及其他框架,避免版本不兼容,开发速度慢等问题的一个高级框架。
可以通过官方文档,配置文档来学习springboot,也可应通过github在查看springboot的版本更新内容
参考尚硅谷笔记
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>

<properties>
<mysql.version>5.1.43</mysql.version>
</properties>
<!--这是实现web功能的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.4.RELEASE</version>
<scope>compile</scope>
</dependency>
@Configuration(proxyBeanMethods = false)
public class MyConfig {
@Bean
public User user01(){
User user01=new User("张三",1,true);
user01.setPet(tomcat());
return user01;
}
@Bean
public Pet tomcat(){
return new Pet("tomcat",3);
}
}
这些组件都可像Spring中那样使用,被@Bean注解的方法的参数会从容器中找
@Import(User.class)
@Import({User.class, EvaluatorTemplate.class})
@Import({xxxSelector.class})
@Import(xxxxRegistrar)
@ImportResource("classpath:bean.xml")
SpringBoot的主类也就是xxxxxApplication上面有一个很重要的注解:@SpringBootApplication
而这个注解被三个主要注解注释:SpringBootConfiguration,EnableAutoConfiguration,ComponentScan
该注解被Configuration注解,也就是说xxxApplication是一个配置类
上面说到过,这是一个设置Springboot扫描包的一个注解,他指定SpringBoot要扫描主类所在包的
所有注解
SpringBoot的自动配置原理就在这个注解中,他有两个注解@AutoConfigurationPackage @Import({AutoConfigurationImportSelector.class})
其中AutoConfigurationPackage其实就是一个Import注解,用于给SpringBoot注册主类所在包中所有组件
剩下的Import({AutoConfigurationImportSelector.class})用于导入该选择器的所有组件,
这个组件没有在我们的包下,是我们导入的包。
getAutoConfigurationEntry(AnnotationMetadata annotationMetadata)
this.getCandidateConfigurations(annotationMetadata, attributes);
SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
(List)loadSpringFactories(classLoaderToUse).getOrDefault(factoryTypeName, Collections.emptyList());
classLoader.getResources("META-INF/spring.factories");
经过一通调用,我们发现他是从META-INF/spring.factories这个包下导入的
而这个包都是我们导的jar包,我们在spring-boot-aotuconfigure这个包下发现了spring.factories
其中有大量的自动配置类,用于自动配置我们的环境


<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
按ctrl+f9,重启项目

同以前的properties用法
yaml非常适合用来做以数据为中心的配置文件
#行内写法:
k: [v1,v2,v3]
#多行写法
k:
- v1
- v2
- v3
#行内写法:
k: {k1:v1,k2:v2,k3:v3}
#或
k:
k1: v1
k2: v2
k3: v3
具体例子参考尚硅谷笔记
自定义的类和配置文件绑定一般没有提示。
<!--使得在编写自定义类的配置有提示--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!--在打包时把以下包去掉,这些包可能是只在编写程序时需要使用--> <configuration> <excludes> <exclude> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build>
#更改静态资源的访问路径
spring:
mvc:
static-path-pattern: /res/**
#更改静态资源放置路径
resources:
static-locations: [classpath:/haha/]
写在static里
@GetMapping("/car/{id}/owner/{username}") public Map<String,Object> getCar(@PathVariable Integer id,//请求行 @PathVariable String username,//请求行 @PathVariable Map<String,String> pv,//请求行 @RequestHeader("User-Agent") String userAgent,//请求头 @RequestHeader Map<String,String> headers,//请求头 @RequestParam("name") String name,//请求参数 @RequestParam("age") Integer age,//请求参数 @RequestParam Map<String,String> params,//请求参数 @CookieValue Cookie cookie,//cookie @CookieValue("_ge") String _ge//cookie ){ Map<String,Object> map=new HashMap<String,Object>(); // map.put("id", id); // map.put("username", username); // map.put("pv", pv); // map.put("userAgent", userAgent); // map.put("headers", headers); // map.put("name", name); // map.put("age", age); // map.put("params", params); map.put("cookie", cookie); map.put("_ge", _ge); return map; } @PostMapping("/car") public Map<String,Object> postCar(@RequestBody String body){ HashMap<String, Object> map = new HashMap<>(); map.put("body", body); return map; }
请求路径 /cars/path;brand=benchi;price=123 获取 @GetMapping("/cars/{path}") public Map matriVariable(@MatrixVariable("brand") String brand, @MatrixVariable("price") Integer price, @PathVariable() String path){ @GetMapping("/boss/{bossId}/{empId}") public Map boss(@MatrixVariable(value = "age",pathVar = "bossId") Integer bossAge, @MatrixVariable(value = "age",pathVar = "empId") Integer empAge){ Map<String,Object> map = new HashMap<>(); map.put("bossAge",bossAge); map.put("empAge",empAge); return map; }
public class LoginInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HttpSession session = request.getSession(); User user =(User) session.getAttribute("user"); log.info("我拦截到的请求是:"+request.getRequestURI()); if(!StringUtils.hasLength(user.getUsername())){ request.setAttribute("errMsg", "请登录"); request.getRequestDispatcher("/").forward(request, response); return false; } //放行 return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { }
@Configuration
public class AdminWebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/","/login","/css/**","/js/**","/fonts/**","/images/**");
}
}
详细内容可参考官方文档
| 表达式名字 | 语法 | 用途 |
|---|---|---|
| 变量取值 | ${} | 获取请求域 |
| 选择变量 | *{…} | 获取上下文对象值 |
| 消息 | #{…} | 获取国际化等值 |
| 链接 | @{…} | 生成链接//…里写链接,不是变量名 |
| 片段表达式 | ~{…} | jsp:include 作用,引入公共页面片段 |
<!--定义-->
<div th:fragment="head">...</div>
<!--引用-->
<div th:replace="~{common::head}"></div>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。