spring boot使用基础小计
2018-02-25 13:50
543 查看
spring-boot很强大,也可以集成很多东西,Mybatis,properties,Mail,test{支持controller层测试},tomcat之类的等等。就说下就我用到的一些皮毛做些总结:
A.<启动,核心配置文件 application.yml>
这个是支持树形结构的配置文件
spring:
datasource:
driver-class-name: oracle.jdbc.driver.OracleDriver
/**spring.datasource.driver-class-name: aaa*/也可以都换成这样
list:
-name: a
-name: b /**多个值(这个是看网上的)*/
——需要注意的是 每个冒号":"后面 一定要有个空格。。。上次也是够痛苦的。
B.BootStrap 启动类 还有其他注解方式这个不太懂,网上找
@SpringBootApplication(scanBasePackages = {"com.huawei.universe.ci"})
public class Bootstrap extends SpringBootServletInitializer
{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
{
return application.sources(Bootstrap.class);
}
C.properties使用,我是直接在application.yml中配置的(这里不再写出)
@Component
public class MailProperties
{
@Value(value = "${spring.mail.mailServerHost}")
private String mailServerHost;
@Value(value = "${spring.mail.mailServerPort}")
private String mailServerPort;
@Value(value = "${spring.mail.username}")
private String username;
使用时 ,只需要Autowired一下,然后直接get
当然也可以新建properties文件然后 新建properties-Java文件:提供set、get方法
1、添加properties文件,配置好
2、建立对应的properties类,类上加注解[这里不需要加上 @component注解]
@ConfigurationProperties(prefix="status",locations="classpath:config/EN-CN.properties")
prefix是配置文件中属性前缀(如 status.success),locations是配置文件路径
3、Bootstrap中添加注解
@EnableConfigurationProperties(StatusTranslaterProperties.class)
4、使用时 一样在类中添加注入 @Autowired 使用get获取值
D.Test测试
1、POM中一如jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2、Test类<eclipse中可以会报super warming>src/test/java
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes=Bootstrap.class)
@WebAppConfiguration<controller层>
public class MailTest
{
private MockMvc mvc;
@Before
public void builMvc()
{
mvc=MockMvcBuilders.standaloneSetup(new xxController()).build();
}
@Test
public void testXx() throws Exception
{
MockHttpServletRequestBuilder request=null;
request=MockMvcRequestBuilders.get("/xx/x");
ResultActions resultActions=mvc.perform(request);/**这里都可以百度的到*/
}
——需要注意的是 src/test/resource下面也要有个application.yml核心启动文件
E.定时Mail
1、Pom添加 <jar包版本低可能会报错>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
2、Triggle
@Component
@Configuration
@Configurable /**这两个应该跟properties有关*/
@EnableScheduling
public class UapEngineerMailTrigger
{
@Autowired
MailProperties mp;
@Scheduled(cron="0 50 8 * * ?")/**cron表达式可以baidu*/
public void sendMailAuto()
{...}
}
3、Spring的Controller层--@RequestBody/@RequestParam/@PathVariable的简单说明
前台 (anglarJS):
$http( {url:"/imocc"}, method:"POST" , params:{info:"info"})
.success(function(data,status,headers,config)) params 请求中的参数
C
4000
ontroller层:
@RequestMapping(value="/select/{size}/{currentPage}",method=RequestMethod.POST)
public BeanResponse getBeanByPage(@RequestParam(value="info",required=false,defaultValue="") String info
,@PathVariable int size,@PathVariable int currentPage)
$http( {url:"/imocc"}, method:"POST" , {info:"info"})
.success(function(data,status,headers,config)) params 请求中的参数
Controller层:
@RequestMapping(value="/select/{size}/{currentPage}",method=RequestMethod.POST)
public BeanResponse getBeanByPage(@RequestBody String info
,@PathVariable int size,@PathVariable int currentPage)
A.<启动,核心配置文件 application.yml>
这个是支持树形结构的配置文件
spring:
datasource:
driver-class-name: oracle.jdbc.driver.OracleDriver
/**spring.datasource.driver-class-name: aaa*/也可以都换成这样
list:
-name: a
-name: b /**多个值(这个是看网上的)*/
——需要注意的是 每个冒号":"后面 一定要有个空格。。。上次也是够痛苦的。
B.BootStrap 启动类 还有其他注解方式这个不太懂,网上找
@SpringBootApplication(scanBasePackages = {"com.huawei.universe.ci"})
public class Bootstrap extends SpringBootServletInitializer
{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
{
return application.sources(Bootstrap.class);
}
C.properties使用,我是直接在application.yml中配置的(这里不再写出)
@Component
public class MailProperties
{
@Value(value = "${spring.mail.mailServerHost}")
private String mailServerHost;
@Value(value = "${spring.mail.mailServerPort}")
private String mailServerPort;
@Value(value = "${spring.mail.username}")
private String username;
使用时 ,只需要Autowired一下,然后直接get
当然也可以新建properties文件然后 新建properties-Java文件:提供set、get方法
1、添加properties文件,配置好
2、建立对应的properties类,类上加注解[这里不需要加上 @component注解]
@ConfigurationProperties(prefix="status",locations="classpath:config/EN-CN.properties")
prefix是配置文件中属性前缀(如 status.success),locations是配置文件路径
3、Bootstrap中添加注解
@EnableConfigurationProperties(StatusTranslaterProperties.class)
4、使用时 一样在类中添加注入 @Autowired 使用get获取值
D.Test测试
1、POM中一如jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2、Test类<eclipse中可以会报super warming>src/test/java
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes=Bootstrap.class)
@WebAppConfiguration<controller层>
public class MailTest
{
private MockMvc mvc;
@Before
public void builMvc()
{
mvc=MockMvcBuilders.standaloneSetup(new xxController()).build();
}
@Test
public void testXx() throws Exception
{
MockHttpServletRequestBuilder request=null;
request=MockMvcRequestBuilders.get("/xx/x");
ResultActions resultActions=mvc.perform(request);/**这里都可以百度的到*/
}
——需要注意的是 src/test/resource下面也要有个application.yml核心启动文件
E.定时Mail
1、Pom添加 <jar包版本低可能会报错>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
2、Triggle
@Component
@Configuration
@Configurable /**这两个应该跟properties有关*/
@EnableScheduling
public class UapEngineerMailTrigger
{
@Autowired
MailProperties mp;
@Scheduled(cron="0 50 8 * * ?")/**cron表达式可以baidu*/
public void sendMailAuto()
{...}
}
3、Spring的Controller层--@RequestBody/@RequestParam/@PathVariable的简单说明
前台 (anglarJS):
$http( {url:"/imocc"}, method:"POST" , params:{info:"info"})
.success(function(data,status,headers,config)) params 请求中的参数
C
4000
ontroller层:
@RequestMapping(value="/select/{size}/{currentPage}",method=RequestMethod.POST)
public BeanResponse getBeanByPage(@RequestParam(value="info",required=false,defaultValue="") String info
,@PathVariable int size,@PathVariable int currentPage)
$http( {url:"/imocc"}, method:"POST" , {info:"info"})
.success(function(data,status,headers,config)) params 请求中的参数
Controller层:
@RequestMapping(value="/select/{size}/{currentPage}",method=RequestMethod.POST)
public BeanResponse getBeanByPage(@RequestBody String info
,@PathVariable int size,@PathVariable int currentPage)
相关文章推荐
- springboot基础总结以及Java系统部署脚本使用
- Spring Boot2.x-04Spring Boot基础-使用注解装配bean
- Spring boot——logback 基础使用篇(一)
- Spring基础:快速入门spring boot(8):使用Junit进行测试
- Spring Boot2.x-05Spring Boot基础-使用注解完成依赖注入
- 使用Spring Boot搭建应用开发框架(一) —— 基础架构(转载)
- Spring boot——logback 基础使用
- 市长信箱邮件查询服务: 使用SpringBoot搭建基础
- [原创]Spring Boot + Mybatis 简易使用指南(一)基础环境搭建
- SpringBoot基础-如何在springboot中使用jsp
- spring boot 基础之使用AOP统一处理请求日志使用方法
- Spring boot——logback 基础使用篇(一)
- Spring Boot2.x-06Spring Boot基础-使用@Conditional注解根据特定的条件装配bean
- Spring boot——logback 基础使用篇(一)
- SpringBoot基础教程2-1-6 日志规范-使用AOP统一处理Web日志
- Spring基础:快速入门spring boot(4):使用slf4j输出日志
- 市长信箱邮件查询服务: 使用SpringBoot搭建基础
- 使用Spring Boot快速构建应用
- 值得使用的Spring Boot