http://blog.csdn.net/jsyxcjw/article/details/46763639
spring boot
1
父Pom 包含用到的jar包的版本定义, java6, utf-8 等设定, 还有一个boot的打包成可执行jar的plugin, 智能读取配置文件 *application*.properties/yml/yaml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
2
jar包版本定义在
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
中
mvn dependency:tree 可以打出工程中使用的jar包树结构
3
启动boot项目
・run as application
・mvn spring-boot:run
・java -jar target/myproject-0.0.1-SNAPSHOT.jar
4
改变java版本
<properties>
<java.version>1.8</java.version>
</properties>
5
打包可执行jar需要导入plugin
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
6
主程序类 要放在其他类的根目录上,这样可以正确扫描到其他配置定义
@Configuration
@EnableAutoConfiguration
@ComponentScan
或
@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
7
配置类 @Configuration
可以写一个类中 也可以分开写
导入其他的配置的写法
・@Import 导入配置类
・@ImportResource 导入配置XML
@Configuration
@Import(value={ConfigA.class})
@ImportResource(locations={
"classpath:/com/boraji/tutorial/spring/config/configB.xml"
})
public class ConfigC {
@Bean
public BeanC getBeanC() {
return new BeanC();
}
}
8
自动配置 @EnableAutoConfiguration 只需要配置一次
spring boot根据依赖的jar包来判断
可以自定义配置来覆盖自动配置。比如db设置等
要查看自动配置了哪些内容 可以在启动时加 --debug 并查看log
去除自动配置
・@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
・spring.autoconfigure.exclude 资源文件
9
远程调试
服务器端启动
java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n -jar target/myproject-0.0.1-SNAPSHOT.jar
For example, with Cloud Foundry you can add the
following to your manifest.yml:
---
env:
JAVA_OPTS: "-Xdebug -Xrunjdwp:server=y,transport=dt_socket,suspend=n"
spring.devtools.remote.debug.local-port可以修改端口
远程监视
在本地的eclipse里,预先在代码里打上断点,然后debug/debug configurations/Remote Java Application,在Host中输入远程主机的IP,在Port中输入远程主机的监听端口,然后点Debug
suspend=y/n是否在调试客户端建立连接之后启动 VM 。如果设置为y,它会阻塞程序运行,直到有客户端连接到对应的监听端口,程序才真正开始执行。
远程debug时修改debug timeout 设定->java->debug 60000ms
远程更新重启
服务器端
・允许远程
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludeDevtools>false</excludeDevtools>
</configuration>
</plugin>
</plugins>
</build>
・设置密码
spring.devtools.remote.secret=mySecret
客户端
・手动启动 org.springframework.boot.devtools.RemoteSpringApplication
• Select Run Configurations… from the Run menu.
• Create a new Java Application “launch configuration”.
• Browse for the my-app project.
• Use org.springframework.boot.devtools.RemoteSpringApplication as the main class.
• Add https://myapp.cfapps.io to the Program arguments (or whatever your remote URL is).
10
设定程序的结束code
org.springframework.boot.ExitCodeGenerator
@SpringBootApplication
public class ExitCodeApplication {
@Bean
public ExitCodeGenerator exitCodeGenerator() {
return new ExitCodeGenerator() {
@Override
public int getExitCode() {
return 42;
}
};
}
public static void main(String[] args) {
System.exit(SpringApplication
.exit(SpringApplication.run(ExitCodeApplication.class, args)));
}
}
@RequestMapping(value = "/admin/shutdown", method = RequestMethod.GET)
public void shutdown() {
final int exitCode = adminService.savepoint();
ExitCodeGenerator exitCodeGenerator = new ExitCodeGenerator() {
@Override
public int getExitCode() {
return exitCode;
}
};
SpringApplication.exit(Application.context, exitCodeGenerator);
}
11
读取资源文件
·配置 文件地址
@Configuration
@PropertySource(value="classpath:/aaa.properties") // 不能使用通配符*等
·注入bean
@Autowired
Environment env;
·读取value
env.getProperty("server.port2")
或者
@Value("${server.port2}")
String value;
用资源文件给静态变量赋值 直接@Value永远为null 用set方法注入
@Value("${my.name}")
public void setPrivateName(String privateName) {
Sample.name = privateName;
}
或者使用
@Component
@ConfigurationProperties("foo")
public class FooProperties { .... }
@EnableConfigurationProperties(FooProperties.class) // 有@Component可以省略
12
随机值 RandomValuePropertySource
my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.uuid=${random.uuid}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}
@Value("${random.value}")
String value;
13
Profile
@Profile("production")
任何 @Component 和 @Configuration 都可以加 @Profile
spring.profiles.active=dev,hsqldb
SpringApplication.setAdditionalProfiles(…) // before run
SpringApplication.run(...)
13
使用spring boot的时候 jsp功能有限制 应使用war方式打包
14
Security
如果Spring Security在classpath上默认使用‘basic’authentication
要使用方法级别的安全设置 使用@EnableGlobalMethodSecurity
@EnableWebSecurity
实现WebSecurityConfigurerAdapter
15
DB
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
JdbcTemplate 和 NamedParameterJdbcTemplate 对象会自动生成 可以用 @Autowire 注入
16
缓存 Caching
配置 @EnableCaching
在要缓存的方法上加 @Cacheable("xxx")
17
JMS 消息服务
ActiveMQ kafka RabitMq等
18
调用RESTful API
用RestTemplate对象来调用
RestTemplate使用restTemplateBuilder.build()来生成
spring boot自动生成restTemplateBuilder
@Service
public class MyBean {
private final RestTemplate restTemplate;
public MyBean(RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder.build();
}
public Details someRestCall(String name) {
return this.restTemplate.getForObject("/{name}/details", Details.class, name);
}
}
实现RestTemplateCustomizer接口在定义restTemplateBuilder
可以自定义设置代理等
19
Validation
类上 @Validated
参数,属性等用 @NotNull @Size(min = 8, max = 10)等
20
发送邮件
spring-boot-starter-mail 使用JavaMailSender
默认timeout无穷 可设置
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=3000
spring.mail.properties.mail.smtp.writetimeout=5000
21
自定义自动配置 starter
自定义配置类首先是@Configuration
然后是@ConditionalOnClass and @ConditionalOnMissingBean
在classpath上有某类且没有自己定义的时候自动配置
jar包里的文件 META-INF/spring.factories 中列出自动配置类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.mycorp.libx.autoconfigure.LibXAutoConfiguration,
com.mycorp.libx.autoconfigure.LibXWebAutoConfiguration