boot自动配置实例,组合注解,spring测试,上传文件,读取配置,打印日志,环境选择,keytool,favicon

1. 组合注解

注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
@ComponentScan
public @interface WiselyConfiguration {
    String[] value() default {};
}

配置类 和 service

@WiselyConfiguration("com.xin.demoxin.wisely")
public class DemoConfig {
}
@Service
public class DemoService {

    public void test() {
        System.out.println("test方法被调用了");
    }
}

测试和结果

        AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(DemoConfig.class);


        DemoService bean2 = context.getBean(DemoService.class);

        bean2.test();


test方法被调用了

2. spring测试

pojo和配置类

@Data
public class TestBean {
    private String content;
    public TestBean(String content) {
        super();
        this.content = content;
    }
}
@Configuration
public class TestConfig {
    @Bean
    @Profile("prod")
    public TestBean prodTestBean() {
        return new TestBean("我的测试Bean prod");
    }

    @Bean
    @Profile("dev")
    public TestBean devTestBean() {
        return new TestBean("我的测试Bean dev");
    }
}

测试类

@SpringBootTest
@ContextConfiguration(classes = {TestConfig.class})
@ActiveProfiles("dev")
class DemoXinApplicationTests {
    //可以直接注入
	@Autowired
	private TestBean testBean;
	@Test
	void contextLoads() {
		String content = testBean.getContent();
		System.out.println(content);
	}
}

3. 上传文件

    @PostMapping(value = "/save",
            consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}) //限制前端必须文件
    public String save(
            @RequestPart(name = "file") MultipartFile file) {
        
        System.out.println(file.getOriginalFilename());
        
        return "";
    }

4. 读取配置

@Data
@Component
@ConfigurationProperties(prefix = "author")
public class Author {

    private String name;
    private Long age;
}
author:
  name: zhangsan
  age: 32
  • 自定义 配置文件注入
book.name=spring.boot
@PropertySource("classpath:my.properties")

@Value("${book.name}")
private String bookName;

5. 打印日志和保存

@Slf4j
log.info("我的测试{}",bookName);
  • 开启文件,所有的日志都会被打印到文件里
logging:
  file:
    name: D:/mylog/log.log
  level:
    org.springframework.web: DEBUG
logging.level.org.springframework.web= DEBUG

6. Inherited

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = { 
        @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
}

只有当父类的注解中用@Inherited修饰,

  • 子类的getAnnotations()才能获取得到父亲的注解以及自身的注解,
  • 而getDeclaredAnnotations()只会获取自身的注解,无论如何都不会获取父亲的注解。

7. boot环境选择

spring:
  profiles:
    active: dev
application-dev.yaml

server:
  port: 9898
  • 配置编码
server:
  servlet:
    encoding:
      charset: UTF-8

8. 自动配置实例

1. POM

  • 新建maven quickstart
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-autoconfigure</artifactId>
      <version>2.5.0</version>
    </dependency>

2. properties

@ConfigurationProperties(prefix = "hello")
public class HelloServiceProperties {
    private static final String MSG="world";

    private String msg=MSG;
    //get set省略
}

3. 普通service

public class HelloService {
    private String msg;

    public String sayHello() {
        return "hello" + msg;
    }
    //get set省略
 }

4. 核心配置类

@Configuration
//开启属性注入
@EnableConfigurationProperties(HelloServiceProperties.class)
//当类路径下,有指定的类的条件下。HelloService 在类路径的条件下
@ConditionalOnClass(HelloService.class)
//指定的属性是否有 指定的值。当设置hello为true的情况下,如果没有默认为true
@ConditionalOnProperty(prefix = "hello", value = "enabled", matchIfMissing = true)
public class HelloServiceAutoConfiguration {
    @Autowired
    private HelloServiceProperties helloServiceProperties;

    @Bean
    //当容器中,没有指定的bean的情况下
    @ConditionalOnMissingBean(HelloService.class)
    public HelloService helloService() {
        HelloService h = new HelloService();
        h.setMsg(helloServiceProperties.getMsg());
        return h;
    }
}

5. spring 文件配置

  • 如果多个类,用逗号分隔。 \ 可是换行
src/main/resources/META-INF/spring.factories

org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=com.wisely.HelloServiceAutoConfiguration

6. 其他项目使用

        <dependency>
            <groupId>com.wisely</groupId>
            <artifactId>spring-boot-starter-hello</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
hello:
  msg: zhangsan
    @Resource
    private HelloService helloService;
    
    return helloService.sayHello();

9. keytool

keytool -genkey -alias tomcat
  • 项目配置
server.port = 8443
server.ssl.key-store = .keystore
server.ssl.key-store-password= 123456
server.ssl.keyStoreType= JKS
server.ssl.keyAlias: tomcat

10. favicon

src/main/resources/static/favicon.ico
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值