栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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
    
      org.springframework.boot
      spring-boot-autoconfigure
      2.5.0
    
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. 其他项目使用
        
            com.wisely
            spring-boot-starter-hello
            0.0.1-SNAPSHOT
        
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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/631872.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号