您的位置:首页 > 编程语言 > Java开发

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

2017-12-15 18:00 555 查看

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类似。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  springboot ssm java 入门