软件编程
位置:首页>> 软件编程>> java编程>> springBoot系列常用注解(小结)

springBoot系列常用注解(小结)

作者:喜羊羊love红太狼  发布时间:2023-12-17 23:26:45 

标签:springBoot,常用注解

@PropertySource

作用是:对自定义的properties文件加载

使用:@PropertySource(value={"classpath:people.properties"})或者@PropertySource(value="classpath:people.properties")

springBoot系列常用注解(小结)

properties文件,获取到值乱码问题

springBoot系列常用注解(小结)

乱码解决:

file ->settings -->file encoding--> 勾选Transparent native-to-ascill conversion

springBoot系列常用注解(小结)

@ImportResource

作用:可以让spring的配置文件生效

springBoot系列常用注解(小结)

使用:在启用类上加ImportResource注解,如@ImportResource(value = "classpath:person.xml")或者@ImportResource(locations ={"classpath:person.xml"})

@Conditional

作用:必须是@Conditional指定的条件成立,才给容器中添加组件或者是让自动配置类生效。

springBoot系列常用注解(小结)

以 HttpEncodingAutoConfiguration 自动配置类举例


@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(ServerProperties.class)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnClass(CharacterEncodingFilter.class)
@ConditionalOnProperty(prefix = "server.servlet.encoding", value = "enabled", matchIfMissing = true)
public class HttpEncodingAutoConfiguration {

private final Encoding properties;

public HttpEncodingAutoConfiguration(ServerProperties properties) {
this.properties = properties.getServlet().getEncoding();
}

@Bean
@ConditionalOnMissingBean
public CharacterEncodingFilter characterEncodingFilter() {
CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
filter.setEncoding(this.properties.getCharset().name());
filter.setForceRequestEncoding(this.properties.shouldForce(Encoding.Type.REQUEST));
filter.setForceResponseEncoding(this.properties.shouldForce(Encoding.Type.RESPONSE));
return filter;
}

@ConditionalOnMissingBean:作用是如果容器中不存在CharacterEncodingFilter 这个bean则实例化创建一个,存在就不走下面的代码

@ConditionalOnClass(CharacterEncodingFilter.class):作用是如果容器中存在CharacterEncodingFilter 这个bean(其中一个条件)则才能够实例化者个自动配置类HttpEncodingAutoConfiguration 

@ConditionalOnProperty(prefix = "server.servlet.encoding", value = "enabled", matchIfMissing = true)作用是如果配置文件中配置了server.servlet.encoding=enabled则才能够实例化者这个个自动配置类HttpEncodingAutoConfiguration 

springBoot中所有的自动配置类位置:

org\springframework\boot\spring-boot-autoconfigure\2.4.5\spring-boot-autoconfigure-2.4.5.jar!\META-INF\spring.factories文件中

springBoot系列常用注解(小结)

如何判断spring.factories这个文件中那些自动配置类是生效的?

在yml或者applicaton.properties中配置,会在控制台打印自动配置类生效报告:


########打印自动配置类生效的报告##########
debug =true

其中:Negative match:表示未生效;Positive match:表示生效的

springBoot系列常用注解(小结)

来源:https://blog.csdn.net/qq_38423256/article/details/115799792

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com