springBoot 시리즈 상용 주해(소결)

@PropertySource
역할:사용자 정의 properties 파일 불 러 오기
사용:@PropertySource(value={"classpath:people.properties"})또는@PropertySource(value="classpath:people.properties")

properties 파일,값 난 장 판 문제 가 져 오기

난 코드 해결:
file ->settings -->file encoding--> 투명 한 native-to-ascill 변환 선택

@ImportResource
역할:spring 의 프로필 을 적용 할 수 있 습 니 다.

사용:사용 클래스 에 ImportResource 주 해 를 추가 합 니 다.예 를 들 어@ImportResource(value="classpath:person.xml")또는@ImportResource(locations={"classpath:person.xml"})
@Conditional
역할:@conditional 이 지정 한 조건 이 성립 되 어야 용기 에 구성 요 소 를 추가 하거나 자동 설정 클래스 를 적용 합 니 다.

이로써 HttpEncoding AutoConfiguration 자동 설정 클래스 예

@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:용기 에 CharacterEncoding Filter 가 존재 하지 않 는 다 면 이 bean 은 실례 적 으로 하 나 를 만 들 고 존재 하면 아래 코드 를 가지 않 는 역할 을 합 니 다.
@ConditionalOnClass(CharacterEncoding Filter.class):용기 에 CharacterEncoding Filter 라 는 bean(그 중의 한 조건)이 존재 해 야 자동 설정 클래스 HttpEncoding AutoConfiguration 을 실례 화 할 수 있 습 니 다. 
@ConditionalOnProperty(prefix="server.servlet.encoding",value="enabled",matchIfMissing=true)는 설정 파일 에 server.servlet.encoding=enabled 가 설정 되 어 있어 야 이 자동 설정 클래스 HttpEncoding AutoConfiguration 을 실례 화 할 수 있 습 니 다. 
springBoot 의 모든 자동 설정 클래스 위치:
org\springframework\boot\spring-boot-autoconfigure\2.4.5\spring-boot-autoconfigure-2.4.5.jar!\META-INF\spring.factories 파일 중

spring.factories 이 파일 의 자동 설정 클래스 가 유효 하 다 고 어떻게 판단 합 니까?
yml 또는 application.properties 에서 설정 하면 콘 솔 에서 자동 설정 류 를 인쇄 합 니 다.

########            ##########
debug =true
그 중:Negative match:유효 하지 않 음 을 표시 합 니 다.Positive match:적용 되 는

springBoot 시리즈 에 자주 사용 되 는 주해(소결)에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 springBoot 에 자주 사용 되 는 주해 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 응원 바 랍 니 다!

좋은 웹페이지 즐겨찾기