【springboot笔记】学习springboot一篇就够,持续更新...

来源:互联网 发布:dbc如何编辑技能数据库 编辑:程序博客网 时间:2024/06/08 08:52

springboot

POM文件

名称 说明 spring-boot-starter 核心 POM,包含自动配置支持、日志库和对 YAML 配置文件的支持。 spring-boot-starter-amqp 通过 spring-rabbit 支持 AMQP。 spring-boot-starter-aop 包含 spring-aop 和 AspectJ 来支持面向切面编程(AOP)。 spring-boot-starter-batch 支持 Spring Batch,包含 HSQLDB。 spring-boot-starter-data-jpa 包含 spring-data-jpa、spring-orm 和 Hibernate 来支持 JPA。 spring-boot-starter-data-mongodb 包含 spring-data-mongodb 来支持 MongoDB。 spring-boot-starter-data-rest 通过 spring-data-rest-webmvc 支持以 REST 方式暴露 Spring Data 仓库。 spring-boot-starter-jdbc 支持使用 JDBC 访问数据库。 spring-boot-starter-security 包含 spring-security。 spring-boot-starter-test 包含常用的测试所需的依赖,如 JUnit、Hamcrest、Mockito 和 spring-test 等。 spring-boot-starter-velocity 支持使用 Velocity 作为模板引擎。 spring-boot-starter-web 支持 Web 应用开发,包含 Tomcat 和 spring-mvc。 spring-boot-starter-websocket 支持使用 Tomcat 开发 WebSocket 应用。 spring-boot-starter-ws 支持 Spring Web Services。 spring-boot-starter-actuator 添加适用于生产环境的功能,如性能指标和监测等功能。 spring-boot-starter-remote-shell 添加远程 SSH 支持。 spring-boot-starter-jetty 使用 Jetty 而不是默认的 Tomcat 作为应用服务器。 spring-boot-starter-log4j 添加 Log4j 的支持。 spring-boot-starter-logging 使用 Spring Boot 默认的日志框架 Logback。 spring-boot-starter-tomcat 使用 Spring Boot 默认的 Tomcat 作为应用服务器。

热启动

注意:该模块在完整的打包环境下运行的时候会被禁用。如果你使用java -jar启动应用或者用一个特定的classloader启动,它会认为这是一个“生产环境”。

`<dependencies>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-devtools</artifactId>        <optional>true</optional>   </dependency></dependencies><build>    <plugins>        <plugin>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-maven-plugin</artifactId>            <configuration>                <fork>true</fork>            </configuration>        </plugin>   </plugins></build>`

常用注解

@ComponentScan

  • 扫描注解,@Component、@Controller、@Service、@Configuration等这些注解的类,注册为bean
  • basePackages = “要扫描的包名”
  • includeFilters = {@ComponentScan.Filter(Aspect.class)}

@EnableAutoConfiguration

  • 这个注解告诉SpringBoot根据添加的jar依赖猜测你想如何配置Spring。例如:依赖spring-boot-starter-web,springboot就帮你配置springmvc和tomcat
  • 如果发现应用不想要的特定自动配置类,可以使用@EnableAutoConfiguration注解的排除属性来禁用它们。@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

@Configuration

在类上配置此注解,等同于与XML中配置beans标签;用@Bean标注方法等价于XML中配置bean。

@Value

@Value注解来读取application.yml里面的配置

`/************** application.properties *************/yzker:  jpush:    customerKey: 93eXXXXXXXXXXXXXec    customerMasterKey: 0399XXXXXXXXXXXXXXX0/************* Java **************/@Value("${yzker.jpush.customerKey}")  private String customerKey;  @Value("${yzker.jpush.customerMasterKey}")  private String customerMasterKey;`

@ImportResource

加载xml配置文件

@SpringBootApplication

由于开发者总是用@Configuration,@EnableAutoConfiguration和@ComponentScan修饰main类,所以有了此注解,它等同于以上三个注解

@ConfigurationProperties

Spring Boot将尝试校验外部的配置,默认使用JSR-303

`@Component  @ConfigurationProperties(prefix="connection")  public class ConnectionSettings {      @NotNull      private InetAddress remoteAddress;          // ... getters and setters  }`

@Profiles

Profiles提供了一种隔离应用程序配置的方式,并让这些配置只能在特定的环境下生效。任何@Component或@Configuration都能被@Profile标记,从而限制加载它的时机。

`@Configuration  @Profile("production")  public class ProductionConfiguration {      // ...  }`

全局异常处理

  • @ControllerAdvice:包含@Component。可以被扫描到。统一处理异常。
  • @ExceptionHandler(Exception.class):用在方法上面表示遇到这个异常就执行以下方法。
`    /**      * 全局异常处理      */      @ControllerAdvice      public class GlobalDefaultExceptionHandler {          public static final String DEFAULT_ERROR_VIEW = "error";          @ExceptionHandler({TypeMismatchException.class,NumberFormatException.class})          public ModelAndView formatErrorHandler(HttpServletRequest req, Exception e) throws Exception {              ModelAndView mav = new ModelAndView();              mav.addObject("error","参数类型错误");              mav.addObject("exception", e);              mav.addObject("url", RequestUtils.getCompleteRequestUrl(req));              mav.addObject("timestamp", new Date());              mav.setViewName(DEFAULT_ERROR_VIEW);              return mav;          }}`

springmvc注解

@ResponseBody

返回值不会进行跳转,而是转成json,写入response body

@Component

组件,当组件不好归类时,可以使用这个注解进行标注。一般公共的方法会用上这个注解。

@RequestParam

修饰变量,@RequestParam String a 等同于 request.getParameter(“a”);
属性:required 默认是 false,true此属性比传

@PathVariable

修饰变量,获取url上的值

`RequestMapping("yoodb/detail/{id}")  public String getByMacAddress(@PathVariable String id){   }`

@Inject

等价于默认的@Autowired,只是没有required属性。

@Bean

相当于XML中的,放在方法的上面,而不是类,意思是产生一个bean,并交给spring管理。

@AutoWired

自动导入依赖的bean。byType方式,当加上(required=false)时,就算找不到bean也不报错。

@Qualifier

当有多个同一类型的Bean时,可以用@Qualifier(“name”)来指定。与@Autowired配合使用

@Resource(name=”name”,type=”type”)

没有括号内内容的话,默认byName。与@Autowired类似。