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

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