Spring-boot

来自ling
跳转至: 导航搜索

重要参数

Common application properties 可用参数列表

--spring.profiles.active=dev --spring.cloud.config.uri=http://localhost:8888/ --server.port=6001 --eureka.client.serviceUrl.defaultZone=http://localhost:${eureka.server.port}/eureka/
--server.context-path=/eureka
--spring.application.name=eit
  • 即使eureka没有context /eureka eureka.client.serviceUrl.defaultZone也要加上/eureka(等价/eureka/)不知道为什么
#这里的配置表示,访问/baidu/** 直接重定向到http://www.baidu.com  
zuul.routes.baidu.path=/baidu/**  
zuul.routes.baidu.url=http://www.baidu.com  
  
#反响代理配置  
#这里的配置类似nginx的反响代理  
#当请求/api/**会直接交给listOfServers配置的服务器处理  
#当stripPrefix=true的时候 (http://127.0.0.1:8181/api/user/list -> http://192.168.1.100:8080/user/list)  
#当stripPrefix=false的时候(http://127.0.0.1:8181/api/user/list -> http://192.168.1.100:8080/api/user/list)  
zuul.routes.api.path=/api/**  
zuul.routes.api.stripPrefix=false  

api.ribbon.listOfServers=192.168.1.100:8080,192.168.1.101:8080,192.168.1.102:8080  
  
#url重写配置  
#这里的配置,相当于访问/index/** 会直接渲染/home的请求内容(和直接请求/home效果一样), url地址不变  
#ribbon.eureka.enabled=false 会使下面的配置失效,不能轻易使用
zuul.routes.index.path=/index/**  
zuul.routes.index.url=forward:/home

参考资料

Spring Boot Reference Guide 官方网站

单元测试

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest()
@ActiveProfiles("dev")

等价

@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("dev")

都依赖于

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

单元测试时环境变量问题

环境变量为spring.application.name=eit;spring.cloud.config.uri=http://localhost:8888/ ;spring.profiles.active=dev

但单元测试激活的配置为 Located environment: name= , profiles=[eit], label=dev, version=null

Spring Boot application starters

spring-boot-starter	The core Spring Boot starter, including auto-configuration support, logging and YAML.
spring-boot-starter-actuator	Production ready features to help you monitor and manage your application.
spring-boot-starter-amqp	Support for the “Advanced Message Queuing Protocol” via?spring-rabbit.
spring-boot-starter-aop	Support for aspect-oriented programming including?spring-aop?and AspectJ.
spring-boot-starter-artemis	Support for “Java Message Service API” via Apache Artemis.
spring-boot-starter-batch	Support for “Spring Batch” including HSQLDB database.
spring-boot-starter-cache	Support for Spring’s Cache abstraction.
spring-boot-starter-cloud-connectors	Support for “Spring Cloud Connectors” which simplifies connecting to services in cloud platforms like Cloud Foundry and Heroku.
spring-boot-starter-data-elasticsearch	Support for the Elasticsearch search and analytics engine including?spring-data-elasticsearch.
spring-boot-starter-data-gemfire	Support for the GemFire distributed data store including?spring-data-gemfire.
spring-boot-starter-data-jpa	Support for the “Java Persistence API” including?spring-data-jpa,?spring-orm?and Hibernate.
spring-boot-starter-data-mongodb	Support for the MongoDB NoSQL Database, including?spring-data-mongodb.
spring-boot-starter-data-rest	Support for exposing Spring Data repositories over REST via?spring-data-rest-webmvc.
spring-boot-starter-data-solr	Support for the Apache Solr search platform, including?spring-data-solr.
spring-boot-starter-freemarker	Support for the FreeMarker templating engine.
spring-boot-starter-groovy-templates	Support for the Groovy templating engine.
spring-boot-starter-hateoas	Support for HATEOAS-based RESTful services via?spring-hateoas.
spring-boot-starter-hornetq	Support for “Java Message Service API” via HornetQ.
spring-boot-starter-integration	Support for common?spring-integration?modules.
spring-boot-starter-jdbc	Support for JDBC databases.
spring-boot-starter-jersey	Support for the Jersey RESTful Web Services framework.
spring-boot-starter-jta-atomikos	Support for JTA distributed transactions via Atomikos.
spring-boot-starter-jta-bitronix	Support for JTA distributed transactions via Bitronix.
spring-boot-starter-mail	Support for?javax.mail.
spring-boot-starter-mobile	Support for?spring-mobile.
spring-boot-starter-mustache	Support for the Mustache templating engine.
spring-boot-starter-redis	Support for the REDIS key-value data store, including?spring-redis.
spring-boot-starter-security	Support for?spring-security.
spring-boot-starter-social-facebook	Support for?spring-social-facebook.
spring-boot-starter-social-linkedin	Support for?spring-social-linkedin.
spring-boot-starter-social-twitter	Support for?spring-social-twitter.
spring-boot-starter-test	Support for common test dependencies, including JUnit, Hamcrest and Mockito along with the?spring-testmodule.
spring-boot-starter-thymeleaf	Support for the Thymeleaf templating engine, including integration with Spring.
spring-boot-starter-velocity	Support for the Velocity templating engine.
spring-boot-starter-web	Support for full-stack web development, including Tomcat and?spring-webmvc.
spring-boot-starter-websocket	Support for WebSocket development.
spring-boot-starter-ws	Support for Spring Web Services.
spring-boot-starter-actuator	Adds production ready features such as metrics and monitoring.
spring-boot-starter-remote-shell	Adds remote?ssh?shell support.
spring-boot-starter-jetty	Imports the Jetty HTTP engine (to be used as an alternative to Tomcat).
spring-boot-starter-log4j	Support the Log4J logging framework.
spring-boot-starter-logging	Import Spring Boot’s default logging framework (Logback).
spring-boot-starter-tomcat	Import Spring Boot’s default HTTP engine (Tomcat).
spring-boot-starter-undertow	Imports the Undertow HTTP engine (to be used as an alternative to Tomcat).

Locating the main application class

We generally recommend that you locate your main application class in a root package above other classes. The @EnableAutoConfiguration annotation is often placed on your main class, and it implicitly defines a base “search package” for certain items. For example, if you are writing a JPA application, the package of the @EnableAutoConfiguration annotated class will be used to search for @Entity items.

Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute. You can also use the @SpringBootApplication annotation if your main class is in the root package.

com

+- example
    +- myproject
        +- Application.java
        |
        +- domain
        |   +- Customer.java
        |   +- CustomerRepository.java
        |
        +- service
        |   +- CustomerService.java
        |
        +- web
            +- CustomerController.java
//@Configuration
//@EnableAutoConfiguration
//@ComponentScan
@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}

@Component
@ConfigurationProperties(prefix="connection")

@Component
@ConfigurationProperties(prefix="connection")
public class ConnectionSettings {

    @NotNull
    private InetAddress remoteAddress;

    // ... getters and setters

}

@Component
@ConfigurationProperties(prefix="connection")
public class ConnectionSettings {

    @NotNull
    @Valid
    private RemoteAddress remoteAddress;

    // ... getters and setters

    public static class RemoteAddress {

        @NotEmpty
        public String hostname;

        // ... getters and setters

    }

}
spring.devtools.restart.exclude=static/**,public/**
spring.devtools.restart.additional-exclude

spring boot 热启动

https://blog.csdn.net/ring300/article/details/82935517

  • 添加依赖和插件,千万不能添加spring-boot-devtools
       <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
            <version>1.2.6.RELEASE</version>
        </dependency>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <dependencies>
                    <!-- spring热部署 -->
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>springloaded</artifactId>
                        <version>1.2.6.RELEASE</version>
                    </dependency>
                </dependencies>
            </plugin>