EE颠覆者5,6章boot基础和核心

运行项目:

mvn spring-boot:run

server:
  port: 9090
  contextPath: /helloboot

contextPath

context-path 这种方式是系统提示的

CONTEXT_PATH

 java -jar .\ch5_2_2-0.0.1-SNAPSHOT.jar --server.port=8888
book.name=spring boot
	 @Value("${book.name}")
	 private String bookName;

类型安全的配置(基于 properties)

@ConfigurationProperties 将 配置文件关联
@Component
@ConfigurationProperties(prefix = "author") //1 
public class AuthorSettings {
    private String name;
    private Long age;
    } //get set

application.properties
author.name=wyf
author.age=32
    
    @Autowired
	private AuthorSettings authorSettings; //1
    
    // 或者属性 ,locations = "classpath:config/author.properties"

日志配置

Log4J log4J2 Logback,默认logback

logging.file=D:/mylog/log.log
logging.level.org.springframework.web: DEBUG

Profile配置

application-{profile}.properties

application-prod.properties

application.properties 配置:
spring.profiles.active=prod
application-dev.properties
server.port=8888

spring.profiles.active=prod
server.port=80
debug=true  //java -jar xx.jar --debug

@SpringBootApplication

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Configuration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplication {}

@EnableAutoConfiguration

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({ EnableAutoConfigurationImportSelector.class,
		AutoConfigurationPackages.Registrar.class })
public @interface EnableAutoConfiguration {}

@Inherited 元注解是一个标记注解,@Inherited阐述了某个被标注的类型是被继承的。
如果一个使用了@Inherited修饰的annotation类型被用于一个class,则这个annotation将被用于该class的子类。

@Order(Ordered.LOWEST_PRECEDENCE)
注解@Order或者接口Ordered的作用是定义Spring IOC容器中Bean的执行顺序的优先级,
而不是定义Bean的加载顺序,Bean的加载顺序不受@Order或Ordered接口的影响;

http的编码配置

spring.http.encoding.charset=UTF-8

spring自动配置实战

  HelloServiceAutoConfiguration
      - @ConditionalOnClass classes found: com.wisely.spring_boot_starter_hello.HelloService (OnClassCondition)
      - matched (OnPropertyCondition)

   HelloServiceAutoConfiguration#helloService
      - @ConditionalOnMissingBean (types: com.wisely.spring_boot_starter_hello.HelloService; SearchStrategy: all) found no beans (OnBeanCondition)

  1. spring-boot-start-hello

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.wisely</groupId>
        <artifactId>spring-boot-starter-hello</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>spring-boot-starter-hello</name>
        <url>http://maven.apache.org</url>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-autoconfigure</artifactId>
                <version>1.5.9.RELEASE</version>
                <!--<version>1.3.0.M1</version>-->
            </dependency>
    
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
    </project>
    
  2. 属性配置:类型安全的属性获取

    @ConfigurationProperties(prefix = "hello") //读取配置
    public class HelloServiceProperties {
        private static final String MSG="world";
        private String msg= MSG;//提供get set
    }
    
hello.msg= wangyunfei //如果不设置 默认为world
  1. 判断依据类

    public class HelloService {
        private String msg;
    
        public String sayHello(){
            return "Hello"+msg;
        }
    
    }
    

    根据此类 的存在与否 来创建 这个类的bean

  2. 自动配置类

    @Configuration
    @EnableConfigurationProperties(HelloServiceProperties.class)//自动获取到配置
    @ConditionalOnClass(HelloService.class)//判断这个类在类路径中是否存在
    @ConditionalOnProperty(prefix = "hello",value = "enabled",matchIfMissing = true)
    public class HelloServiceAutoConfiguration {
        @Autowired
        private HelloServiceProperties helloServiceProperties;
    
        @Bean
        @ConditionalOnMissingBean(HelloService.class)//如果容器中没有,自动创建一个bean
        public HelloService helloService(){
            HelloService helloService=new HelloService();
            helloService.setMsg(helloServiceProperties.getMsg());
            return helloService;
        }
    }
    
  3. 配置文件,让自动配置生效

    src/main/resources/META-INF/spring.factories
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=
    com.wisely.spring_boot_starter_hello.HelloServiceAutoConfiguration
    

    若有多个配置,用,隔开

  4. mvn install 并使用

    		<dependency>
    			<groupId>com.wisely</groupId>
    			<artifactId>spring-boot-starter-hello</artifactId>
    			<version>0.0.1-SNAPSHOT</version>
    		</dependency>
    配置:
    hello.msg= wangyunfei
    debug=true
    
    	@Autowired
    	HelloService helloService;
    	
    	@RequestMapping("/")
    	public String index(){
    		return helloService.sayHello();
    	}
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值