스프링 노트 - 자원 자원, 표현 식 언어 SpEL
1.1 소개
보통 텍스트 파일, 바 이 너 리 파일, 예 를 들 어 그림, 속성 파일 properties, 프로필 xml 등 을 가리 키 며 classpath, 파일 시스템, 네트워크 서버 등 위치 에 저장 하고 file:, https:, ftp: 등 을 통 해 가 져 옵 니 다.자바 넷 URL 의 증강 과 확장 입 니 다.
1.2 리 소스 인터페이스 와 내장 실현
Resource 인 터 페 이 스 는 Spring 에서 광범 위 하 게 사용 된다.그 내장 실현 예 를 들 어 다음 과 같다.말 그대로 각자 이름 에서 그 용 도 를 대충 짐작 할 수 있다.
1.3 리 소스 로 더 인터페이스
- 이 인 터 페 이 스 는 getResource 방법 을 설명 하고 AppCtx 는 이 인 터 페 이 스 를 실현 했다.
Resource resource = ctx.getResource("x/y/config.txt");
- 자원 문자열 은 classpath:, file: /, http: / 와 같은 접 두 사 를 추가 할 수 있 습 니 다. 추가 하지 않 으 면 ctx 의 형식 에 따라 결 정 됩 니 다.예 를 들 어 ClassPathXMLapPCtx 는 classpath 에서 자원 을 찾 고 FileSystemXmlAppCtx 는 파일 시스템 에서 자원 을 찾 습 니 다.
- FileSystemXmlAppCtx 라면 자원 문자열 에 file: / 접 두 사 를 추가 하지 않 으 면 상대 경로 이 고 추가 하면 절대 경로 와 상대 경 로 를 구분 합 니 다.
FileSystemXmlApplicationContext ctx = ...;
//
ctx.getResource("path1/userList.txt");
//
ctx.getResource("/path1/userList.txt");
//
ctx.getResource("file:///path1/userList.txt");
- ctx 를 사용 하지 않 으 면 빈 에 리 소스 로 더 를 주입 할 수 있 음
@Autowired
ResourceLoader resourceLoader;
1.4 직접 자원 주입
위탁 관리 Bean 에 리 소스 를 직접 주입 하 는 것 이 더 편리 합 니 다.
<bean id="car" class="x.y.x.Car">
<property name="manual" value="doc/manual.txt" />
</bean>
2. 표현 식 언어 SpEL
2.1 소개
- Spring Expression Language 는 SpEL 이 라 고 부 르 며 실행 시 대상 을 조작 할 수 있 습 니 다.같은 언어 나 도 구 는 OGNL, MVEL, JBoss EL 등 이 있다.SpEL 은 Spring Portfolio 에서 광범 위 하 게 응용 된다.
- 그 기능: 문자 표현 식, 불 과 관계 조작 자, 정규 표현 식, 클래스 표현 식, 접근 properties, arrays, lists, maps, 방법 호출, 관계 조작 자, 할당, 호출 구조 기, 삼원 조작 자, 변수, 사용자 정의 함수, 집합 투영, 집합 선택, 템 플 릿 표현 식
2.2 사용법
// parser
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("'Hello World'.concat('!')");
String message1 = (String) exp.getValue(); //Hello World!
String message2 = (String) exp.getValue(String.class); // ,
// EvaluationContext
car.setName(" ");
EvaluationContext context = new StandardEvaluationContext(car);
String name = (String) parser.parseExpression("name").getValue(context); //
//Parser
SpelParserConfiguration config = new SpelParserConfiguration(true,true); //arg1: null ,arg2: collection collection
ExpressionParser parser = new SpelExpressionParser(config);
class Todo {
public List<String> items;
}
parser.parseExpression("items[3]").getValue(new Todo()); // List<String>, 4
2.3 컴 파일 SpEL
- 표현 식 을 컴 파일 하면 성능 을 향상 시 킬 수 있 습 니 다. Spring reference 는 컴 파일 전 50000 번 의 교체 시간 75ms, 컴 파일 후 3ms 를 언급 합 니 다.
- 컴 파일 옵션: OFF (기본 값), IMMEDIATE, MIXED
//arg2: classLoader child classLoader, ; classLoader 。 null, classLoader 。
SpelParserConfiguration config = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, this.getClass().getClassLoader());
SpelExpressionParser parser = new SpelExpressionParser(config);
2.4 IoC 용기 에 SpEL 사용
<bean id="randomNumber" class="...">
<property name="number" value="#{ T(java.lang.Math).random() }" />
</bean>
<bean id="guessNumber" class="...">
<property name="correctNumber" value="#{ randomNumber.number }" />
</bean>
public class RandomNumber {
@Value ("#{ T(java.lang.Math).random() }")
int number;
@Value("#{ systemProperties['user.region'] }")
public void setDefaultLocale(String defaultLocale) {
this.defaultLocale = defaultLocale;
}
}
2.5 언어 참조
SpEL 은 Spring Reference 를 참고 하여 지원 하 는 표현 식 이 많 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.