spring 주해 소스 코드 분석 - 주해 설정 자원 분석 및 주입
74326 단어 스프링
Spring 의 주석 기능 을 사용 할 때,
< context:component-scan >
위의 설정 은 Spring 용기 에 암시 적 으로 등록 합 니 다: CommonAnnotationBeanPostProcessor, PersistenceAnnotationBeanPostProcessor 및 이 4 개의 주 해 를 처리 하 는 Bean 백 엔 드 프로세서 입 니 다.
AutowiredAnnotationBeanPostProcessor
spring 용기 전문 처리 입 니 다.
(1) 구조 함수
public AutowiredAnnotationBeanPostProcessor() {
// @Autowire
this.autowiredAnnotationTypes.add(Autowired.class);
// @Value
this.autowiredAnnotationTypes.add(Value.class);
//
ClassLoader cl = AutowiredAnnotationBeanPostProcessor.class.getClassLoader();
try {
// javax.inject.Inject JSR-330
this.autowiredAnnotationTypes.add((Class extends Annotation>) cl.loadClass("javax.inject.Inject"));
logger.info("JSR-330 'javax.inject.Inject' annotation found and supported for autowiring");
}
catch (ClassNotFoundException ex) {
// JSR-330 API not available - simply skip.
}
}
(2) 지정 류 는 적당 한 구조 방법 을 선택한다.
// Bean
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {
// Bean
Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);
//
if (candidateConstructors == null) {
//
synchronized (this.candidateConstructorsCache) {
candidateConstructors = this.candidateConstructorsCache.get(beanClass);
if (candidateConstructors == null) {
// JDK ,
Constructor[] rawCandidates = beanClass.getDeclaredConstructors();
//
List candidates = new ArrayList(rawCandidates.length);
//autowire required
Constructor requiredConstructor = null;
//
Constructor defaultConstructor = null;
// , autowire ,
// required
for (Constructor> candidate : rawCandidates) {
// autowire (Annotation)
Annotation annotation = findAutowiredAnnotation(candidate);
// antowire
if (annotation != null) {
// antowire required
if (requiredConstructor != null) {
throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// autowire
if (candidate.getParameterTypes().length == 0) {
throw new IllegalStateException(
"Autowired annotation requires at least one argument: " + candidate);
}
// autowire required
boolean required = determineRequiredStatus(annotation);
// autowire required
if (required) {
//
if (!candidates.isEmpty()) {
throw new BeanCreationException(
"Invalid autowire-marked constructors: " + candidates +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// required
requiredConstructor = candidate;
}
//
candidates.add(candidate);
}
// autowire ,
else if (candidate.getParameterTypes().length == 0) {
//
defaultConstructor = candidate;
}
}
//
if (!candidates.isEmpty()) {
// required ,
if (requiredConstructor == null && defaultConstructor != null) {
//
candidates.add(defaultConstructor);
}
//
candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);
}
// ,
else {
candidateConstructors = new Constructor[0];
}
//
this.candidateConstructorsCache.put(beanClass, candidateConstructors);
}
}
}
// , null
return (candidateConstructors.length > 0 ? candidateConstructors : null);
}
용기 가 지정 한 클래스 에 자동 으로 주입 조립 에 의존 할 때 용 기 는 Bean 에 적합 한 구조 방법 을 호출 하여 실례 대상 을 만 들 고 AutowiredAnnotationBeanPostProcessor 는 지정 한 클래스 에 해당 하 는 구조 방법 을 선택해 야 한다.
(3) 속성 에 대한 의존 주입
// Bean
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {
// Bean
Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);
//
if (candidateConstructors == null) {
//
synchronized (this.candidateConstructorsCache) {
candidateConstructors = this.candidateConstructorsCache.get(beanClass);
if (candidateConstructors == null) {
// JDK ,
Constructor[] rawCandidates = beanClass.getDeclaredConstructors();
//
List candidates = new ArrayList(rawCandidates.length);
//autowire required
Constructor requiredConstructor = null;
//
Constructor defaultConstructor = null;
// , autowire ,
// required
for (Constructor> candidate : rawCandidates) {
// autowire (Annotation)
Annotation annotation = findAutowiredAnnotation(candidate);
// antowire
if (annotation != null) {
// antowire required
if (requiredConstructor != null) {
throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// autowire
if (candidate.getParameterTypes().length == 0) {
throw new IllegalStateException(
"Autowired annotation requires at least one argument: " + candidate);
}
// autowire required
boolean required = determineRequiredStatus(annotation);
// autowire required
if (required) {
//
if (!candidates.isEmpty()) {
throw new BeanCreationException(
"Invalid autowire-marked constructors: " + candidates +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// required
requiredConstructor = candidate;
}
//
candidates.add(candidate);
}
// autowire ,
else if (candidate.getParameterTypes().length == 0) {
//
defaultConstructor = candidate;
}
}
//
if (!candidates.isEmpty()) {
// required ,
if (requiredConstructor == null && defaultConstructor != null) {
//
candidates.add(defaultConstructor);
}
//
candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);
}
// ,
else {
candidateConstructors = new Constructor[0];
}
//
this.candidateConstructorsCache.put(beanClass, candidateConstructors);
}
}
}
// , null
return (candidateConstructors.length > 0 ? candidateConstructors : null);
}
(4) 필드 주입
// Bean
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {
// Bean
Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);
//
if (candidateConstructors == null) {
//
synchronized (this.candidateConstructorsCache) {
candidateConstructors = this.candidateConstructorsCache.get(beanClass);
if (candidateConstructors == null) {
// JDK ,
Constructor[] rawCandidates = beanClass.getDeclaredConstructors();
//
List candidates = new ArrayList(rawCandidates.length);
//autowire required
Constructor requiredConstructor = null;
//
Constructor defaultConstructor = null;
// , autowire ,
// required
for (Constructor> candidate : rawCandidates) {
// autowire (Annotation)
Annotation annotation = findAutowiredAnnotation(candidate);
// antowire
if (annotation != null) {
// antowire required
if (requiredConstructor != null) {
throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// autowire
if (candidate.getParameterTypes().length == 0) {
throw new IllegalStateException(
"Autowired annotation requires at least one argument: " + candidate);
}
// autowire required
boolean required = determineRequiredStatus(annotation);
// autowire required
if (required) {
//
if (!candidates.isEmpty()) {
throw new BeanCreationException(
"Invalid autowire-marked constructors: " + candidates +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// required
requiredConstructor = candidate;
}
//
candidates.add(candidate);
}
// autowire ,
else if (candidate.getParameterTypes().length == 0) {
//
defaultConstructor = candidate;
}
}
//
if (!candidates.isEmpty()) {
// required ,
if (requiredConstructor == null && defaultConstructor != null) {
//
candidates.add(defaultConstructor);
}
//
candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);
}
// ,
else {
candidateConstructors = new Constructor[0];
}
//
this.candidateConstructorsCache.put(beanClass, candidateConstructors);
}
}
}
// , null
return (candidateConstructors.length > 0 ? candidateConstructors : null);
}
(5) 방법 주입
// Bean
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {
// Bean
Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);
//
if (candidateConstructors == null) {
//
synchronized (this.candidateConstructorsCache) {
candidateConstructors = this.candidateConstructorsCache.get(beanClass);
if (candidateConstructors == null) {
// JDK ,
Constructor[] rawCandidates = beanClass.getDeclaredConstructors();
//
List candidates = new ArrayList(rawCandidates.length);
//autowire required
Constructor requiredConstructor = null;
//
Constructor defaultConstructor = null;
// , autowire ,
// required
for (Constructor> candidate : rawCandidates) {
// autowire (Annotation)
Annotation annotation = findAutowiredAnnotation(candidate);
// antowire
if (annotation != null) {
// antowire required
if (requiredConstructor != null) {
throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// autowire
if (candidate.getParameterTypes().length == 0) {
throw new IllegalStateException(
"Autowired annotation requires at least one argument: " + candidate);
}
// autowire required
boolean required = determineRequiredStatus(annotation);
// autowire required
if (required) {
//
if (!candidates.isEmpty()) {
throw new BeanCreationException(
"Invalid autowire-marked constructors: " + candidates +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// required
requiredConstructor = candidate;
}
//
candidates.add(candidate);
}
// autowire ,
else if (candidate.getParameterTypes().length == 0) {
//
defaultConstructor = candidate;
}
}
//
if (!candidates.isEmpty()) {
// required ,
if (requiredConstructor == null && defaultConstructor != null) {
//
candidates.add(defaultConstructor);
}
//
candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);
}
// ,
else {
candidateConstructors = new Constructor[0];
}
//
this.candidateConstructorsCache.put(beanClass, candidateConstructors);
}
}
}
// , null
return (candidateConstructors.length > 0 ? candidateConstructors : null);
}
CommonAnnotationBeanPostProcessor
JavaEE 에서 자주 사용 하 는 주석 처리 로 @ PostConstruct, @ PreDestroy 등 을 처리 할 수 있 으 며, 처리 의 가장 핵심 은 @ Resource 주석 입 니 다.
// Bean
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {
// Bean
Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);
//
if (candidateConstructors == null) {
//
synchronized (this.candidateConstructorsCache) {
candidateConstructors = this.candidateConstructorsCache.get(beanClass);
if (candidateConstructors == null) {
// JDK ,
Constructor[] rawCandidates = beanClass.getDeclaredConstructors();
//
List candidates = new ArrayList(rawCandidates.length);
//autowire required
Constructor requiredConstructor = null;
//
Constructor defaultConstructor = null;
// , autowire ,
// required
for (Constructor> candidate : rawCandidates) {
// autowire (Annotation)
Annotation annotation = findAutowiredAnnotation(candidate);
// antowire
if (annotation != null) {
// antowire required
if (requiredConstructor != null) {
throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// autowire
if (candidate.getParameterTypes().length == 0) {
throw new IllegalStateException(
"Autowired annotation requires at least one argument: " + candidate);
}
// autowire required
boolean required = determineRequiredStatus(annotation);
// autowire required
if (required) {
//
if (!candidates.isEmpty()) {
throw new BeanCreationException(
"Invalid autowire-marked constructors: " + candidates +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// required
requiredConstructor = candidate;
}
//
candidates.add(candidate);
}
// autowire ,
else if (candidate.getParameterTypes().length == 0) {
//
defaultConstructor = candidate;
}
}
//
if (!candidates.isEmpty()) {
// required ,
if (requiredConstructor == null && defaultConstructor != null) {
//
candidates.add(defaultConstructor);
}
//
candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);
}
// ,
else {
candidateConstructors = new Constructor[0];
}
//
this.candidateConstructorsCache.put(beanClass, candidateConstructors);
}
}
}
// , null
return (candidateConstructors.length > 0 ? candidateConstructors : null);
}
// Bean
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {
// Bean
Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);
//
if (candidateConstructors == null) {
//
synchronized (this.candidateConstructorsCache) {
candidateConstructors = this.candidateConstructorsCache.get(beanClass);
if (candidateConstructors == null) {
// JDK ,
Constructor[] rawCandidates = beanClass.getDeclaredConstructors();
//
List candidates = new ArrayList(rawCandidates.length);
//autowire required
Constructor requiredConstructor = null;
//
Constructor defaultConstructor = null;
// , autowire ,
// required
for (Constructor> candidate : rawCandidates) {
// autowire (Annotation)
Annotation annotation = findAutowiredAnnotation(candidate);
// antowire
if (annotation != null) {
// antowire required
if (requiredConstructor != null) {
throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// autowire
if (candidate.getParameterTypes().length == 0) {
throw new IllegalStateException(
"Autowired annotation requires at least one argument: " + candidate);
}
// autowire required
boolean required = determineRequiredStatus(annotation);
// autowire required
if (required) {
//
if (!candidates.isEmpty()) {
throw new BeanCreationException(
"Invalid autowire-marked constructors: " + candidates +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// required
requiredConstructor = candidate;
}
//
candidates.add(candidate);
}
// autowire ,
else if (candidate.getParameterTypes().length == 0) {
//
defaultConstructor = candidate;
}
}
//
if (!candidates.isEmpty()) {
// required ,
if (requiredConstructor == null && defaultConstructor != null) {
//
candidates.add(defaultConstructor);
}
//
candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);
}
// ,
else {
candidateConstructors = new Constructor[0];
}
//
this.candidateConstructorsCache.put(beanClass, candidateConstructors);
}
}
}
// , null
return (candidateConstructors.length > 0 ? candidateConstructors : null);
}
// Bean
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {
// Bean
Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);
//
if (candidateConstructors == null) {
//
synchronized (this.candidateConstructorsCache) {
candidateConstructors = this.candidateConstructorsCache.get(beanClass);
if (candidateConstructors == null) {
// JDK ,
Constructor[] rawCandidates = beanClass.getDeclaredConstructors();
//
List candidates = new ArrayList(rawCandidates.length);
//autowire required
Constructor requiredConstructor = null;
//
Constructor defaultConstructor = null;
// , autowire ,
// required
for (Constructor> candidate : rawCandidates) {
// autowire (Annotation)
Annotation annotation = findAutowiredAnnotation(candidate);
// antowire
if (annotation != null) {
// antowire required
if (requiredConstructor != null) {
throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// autowire
if (candidate.getParameterTypes().length == 0) {
throw new IllegalStateException(
"Autowired annotation requires at least one argument: " + candidate);
}
// autowire required
boolean required = determineRequiredStatus(annotation);
// autowire required
if (required) {
//
if (!candidates.isEmpty()) {
throw new BeanCreationException(
"Invalid autowire-marked constructors: " + candidates +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// required
requiredConstructor = candidate;
}
//
candidates.add(candidate);
}
// autowire ,
else if (candidate.getParameterTypes().length == 0) {
//
defaultConstructor = candidate;
}
}
//
if (!candidates.isEmpty()) {
// required ,
if (requiredConstructor == null && defaultConstructor != null) {
//
candidates.add(defaultConstructor);
}
//
candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);
}
// ,
else {
candidateConstructors = new Constructor[0];
}
//
this.candidateConstructorsCache.put(beanClass, candidateConstructors);
}
}
}
// , null
return (candidateConstructors.length > 0 ? candidateConstructors : null);
}
// Bean
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {
// Bean
Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);
//
if (candidateConstructors == null) {
//
synchronized (this.candidateConstructorsCache) {
candidateConstructors = this.candidateConstructorsCache.get(beanClass);
if (candidateConstructors == null) {
// JDK ,
Constructor[] rawCandidates = beanClass.getDeclaredConstructors();
//
List candidates = new ArrayList(rawCandidates.length);
//autowire required
Constructor requiredConstructor = null;
//
Constructor defaultConstructor = null;
// , autowire ,
// required
for (Constructor> candidate : rawCandidates) {
// autowire (Annotation)
Annotation annotation = findAutowiredAnnotation(candidate);
// antowire
if (annotation != null) {
// antowire required
if (requiredConstructor != null) {
throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// autowire
if (candidate.getParameterTypes().length == 0) {
throw new IllegalStateException(
"Autowired annotation requires at least one argument: " + candidate);
}
// autowire required
boolean required = determineRequiredStatus(annotation);
// autowire required
if (required) {
//
if (!candidates.isEmpty()) {
throw new BeanCreationException(
"Invalid autowire-marked constructors: " + candidates +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// required
requiredConstructor = candidate;
}
//
candidates.add(candidate);
}
// autowire ,
else if (candidate.getParameterTypes().length == 0) {
//
defaultConstructor = candidate;
}
}
//
if (!candidates.isEmpty()) {
// required ,
if (requiredConstructor == null && defaultConstructor != null) {
//
candidates.add(defaultConstructor);
}
//
candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);
}
// ,
else {
candidateConstructors = new Constructor[0];
}
//
this.candidateConstructorsCache.put(beanClass, candidateConstructors);
}
}
}
// , null
return (candidateConstructors.length > 0 ? candidateConstructors : null);
}
// Bean
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {
// Bean
Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);
//
if (candidateConstructors == null) {
//
synchronized (this.candidateConstructorsCache) {
candidateConstructors = this.candidateConstructorsCache.get(beanClass);
if (candidateConstructors == null) {
// JDK ,
Constructor[] rawCandidates = beanClass.getDeclaredConstructors();
//
List candidates = new ArrayList(rawCandidates.length);
//autowire required
Constructor requiredConstructor = null;
//
Constructor defaultConstructor = null;
// , autowire ,
// required
for (Constructor> candidate : rawCandidates) {
// autowire (Annotation)
Annotation annotation = findAutowiredAnnotation(candidate);
// antowire
if (annotation != null) {
// antowire required
if (requiredConstructor != null) {
throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// autowire
if (candidate.getParameterTypes().length == 0) {
throw new IllegalStateException(
"Autowired annotation requires at least one argument: " + candidate);
}
// autowire required
boolean required = determineRequiredStatus(annotation);
// autowire required
if (required) {
//
if (!candidates.isEmpty()) {
throw new BeanCreationException(
"Invalid autowire-marked constructors: " + candidates +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// required
requiredConstructor = candidate;
}
//
candidates.add(candidate);
}
// autowire ,
else if (candidate.getParameterTypes().length == 0) {
//
defaultConstructor = candidate;
}
}
//
if (!candidates.isEmpty()) {
// required ,
if (requiredConstructor == null && defaultConstructor != null) {
//
candidates.add(defaultConstructor);
}
//
candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);
}
// ,
else {
candidateConstructors = new Constructor[0];
}
//
this.candidateConstructorsCache.put(beanClass, candidateConstructors);
}
}
}
// , null
return (candidateConstructors.length > 0 ? candidateConstructors : null);
}
// Bean
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {
// Bean
Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);
//
if (candidateConstructors == null) {
//
synchronized (this.candidateConstructorsCache) {
candidateConstructors = this.candidateConstructorsCache.get(beanClass);
if (candidateConstructors == null) {
// JDK ,
Constructor[] rawCandidates = beanClass.getDeclaredConstructors();
//
List candidates = new ArrayList(rawCandidates.length);
//autowire required
Constructor requiredConstructor = null;
//
Constructor defaultConstructor = null;
// , autowire ,
// required
for (Constructor> candidate : rawCandidates) {
// autowire (Annotation)
Annotation annotation = findAutowiredAnnotation(candidate);
// antowire
if (annotation != null) {
// antowire required
if (requiredConstructor != null) {
throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// autowire
if (candidate.getParameterTypes().length == 0) {
throw new IllegalStateException(
"Autowired annotation requires at least one argument: " + candidate);
}
// autowire required
boolean required = determineRequiredStatus(annotation);
// autowire required
if (required) {
//
if (!candidates.isEmpty()) {
throw new BeanCreationException(
"Invalid autowire-marked constructors: " + candidates +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// required
requiredConstructor = candidate;
}
//
candidates.add(candidate);
}
// autowire ,
else if (candidate.getParameterTypes().length == 0) {
//
defaultConstructor = candidate;
}
}
//
if (!candidates.isEmpty()) {
// required ,
if (requiredConstructor == null && defaultConstructor != null) {
//
candidates.add(defaultConstructor);
}
//
candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);
}
// ,
else {
candidateConstructors = new Constructor[0];
}
//
this.candidateConstructorsCache.put(beanClass, candidateConstructors);
}
}
}
// , null
return (candidateConstructors.length > 0 ? candidateConstructors : null);
}
RequiredAnnotationBeanPostProcessor
@ Required 주 해 를 처리 합 니 다. @ Required 주 해 는 Bean 속성 을 설정 해 야 합 니 다. Spring 용기 가 Bean 속성 에 의존 하여 주입 할 때 @ Required 주해 의 속성 을 설정 합 니 다. Spring 용 기 는 의존 관계 설정 여 부 를 검사 합 니 다.
// Bean
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {
// Bean
Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);
//
if (candidateConstructors == null) {
//
synchronized (this.candidateConstructorsCache) {
candidateConstructors = this.candidateConstructorsCache.get(beanClass);
if (candidateConstructors == null) {
// JDK ,
Constructor[] rawCandidates = beanClass.getDeclaredConstructors();
//
List candidates = new ArrayList(rawCandidates.length);
//autowire required
Constructor requiredConstructor = null;
//
Constructor defaultConstructor = null;
// , autowire ,
// required
for (Constructor> candidate : rawCandidates) {
// autowire (Annotation)
Annotation annotation = findAutowiredAnnotation(candidate);
// antowire
if (annotation != null) {
// antowire required
if (requiredConstructor != null) {
throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// autowire
if (candidate.getParameterTypes().length == 0) {
throw new IllegalStateException(
"Autowired annotation requires at least one argument: " + candidate);
}
// autowire required
boolean required = determineRequiredStatus(annotation);
// autowire required
if (required) {
//
if (!candidates.isEmpty()) {
throw new BeanCreationException(
"Invalid autowire-marked constructors: " + candidates +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// required
requiredConstructor = candidate;
}
//
candidates.add(candidate);
}
// autowire ,
else if (candidate.getParameterTypes().length == 0) {
//
defaultConstructor = candidate;
}
}
//
if (!candidates.isEmpty()) {
// required ,
if (requiredConstructor == null && defaultConstructor != null) {
//
candidates.add(defaultConstructor);
}
//
candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);
}
// ,
else {
candidateConstructors = new Constructor[0];
}
//
this.candidateConstructorsCache.put(beanClass, candidateConstructors);
}
}
}
// , null
return (candidateConstructors.length > 0 ? candidateConstructors : null);
}
PersistenceAnnotationBeanPostProcessor
JPA 관련 주 해 를 처리 하 는 Bean 백 엔 드 프로세서 입 니 다. @ PersistenceUnit @ PersistenceContext 주 해 를 분석 하고 처리 합 니 다. 그 주요 역할 은 JPA 의 실체 관리자 공장 과 실체 관리자 에 해당 하 는 지구 화 유닛 이나 컨 텍스트 를 주입 하 는 것 입 니 다.
// Bean
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {
// Bean
Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);
//
if (candidateConstructors == null) {
//
synchronized (this.candidateConstructorsCache) {
candidateConstructors = this.candidateConstructorsCache.get(beanClass);
if (candidateConstructors == null) {
// JDK ,
Constructor[] rawCandidates = beanClass.getDeclaredConstructors();
//
List candidates = new ArrayList(rawCandidates.length);
//autowire required
Constructor requiredConstructor = null;
//
Constructor defaultConstructor = null;
// , autowire ,
// required
for (Constructor> candidate : rawCandidates) {
// autowire (Annotation)
Annotation annotation = findAutowiredAnnotation(candidate);
// antowire
if (annotation != null) {
// antowire required
if (requiredConstructor != null) {
throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// autowire
if (candidate.getParameterTypes().length == 0) {
throw new IllegalStateException(
"Autowired annotation requires at least one argument: " + candidate);
}
// autowire required
boolean required = determineRequiredStatus(annotation);
// autowire required
if (required) {
//
if (!candidates.isEmpty()) {
throw new BeanCreationException(
"Invalid autowire-marked constructors: " + candidates +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// required
requiredConstructor = candidate;
}
//
candidates.add(candidate);
}
// autowire ,
else if (candidate.getParameterTypes().length == 0) {
//
defaultConstructor = candidate;
}
}
//
if (!candidates.isEmpty()) {
// required ,
if (requiredConstructor == null && defaultConstructor != null) {
//
candidates.add(defaultConstructor);
}
//
candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);
}
// ,
else {
candidateConstructors = new Constructor[0];
}
//
this.candidateConstructorsCache.put(beanClass, candidateConstructors);
}
}
}
// , null
return (candidateConstructors.length > 0 ? candidateConstructors : null);
}
// Bean
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {
// Bean
Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);
//
if (candidateConstructors == null) {
//
synchronized (this.candidateConstructorsCache) {
candidateConstructors = this.candidateConstructorsCache.get(beanClass);
if (candidateConstructors == null) {
// JDK ,
Constructor[] rawCandidates = beanClass.getDeclaredConstructors();
//
List candidates = new ArrayList(rawCandidates.length);
//autowire required
Constructor requiredConstructor = null;
//
Constructor defaultConstructor = null;
// , autowire ,
// required
for (Constructor> candidate : rawCandidates) {
// autowire (Annotation)
Annotation annotation = findAutowiredAnnotation(candidate);
// antowire
if (annotation != null) {
// antowire required
if (requiredConstructor != null) {
throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// autowire
if (candidate.getParameterTypes().length == 0) {
throw new IllegalStateException(
"Autowired annotation requires at least one argument: " + candidate);
}
// autowire required
boolean required = determineRequiredStatus(annotation);
// autowire required
if (required) {
//
if (!candidates.isEmpty()) {
throw new BeanCreationException(
"Invalid autowire-marked constructors: " + candidates +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// required
requiredConstructor = candidate;
}
//
candidates.add(candidate);
}
// autowire ,
else if (candidate.getParameterTypes().length == 0) {
//
defaultConstructor = candidate;
}
}
//
if (!candidates.isEmpty()) {
// required ,
if (requiredConstructor == null && defaultConstructor != null) {
//
candidates.add(defaultConstructor);
}
//
candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);
}
// ,
else {
candidateConstructors = new Constructor[0];
}
//
this.candidateConstructorsCache.put(beanClass, candidateConstructors);
}
}
}
// , null
return (candidateConstructors.length > 0 ? candidateConstructors : null);
}
// Bean
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {
// Bean
Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);
//
if (candidateConstructors == null) {
//
synchronized (this.candidateConstructorsCache) {
candidateConstructors = this.candidateConstructorsCache.get(beanClass);
if (candidateConstructors == null) {
// JDK ,
Constructor[] rawCandidates = beanClass.getDeclaredConstructors();
//
List candidates = new ArrayList(rawCandidates.length);
//autowire required
Constructor requiredConstructor = null;
//
Constructor defaultConstructor = null;
// , autowire ,
// required
for (Constructor> candidate : rawCandidates) {
// autowire (Annotation)
Annotation annotation = findAutowiredAnnotation(candidate);
// antowire
if (annotation != null) {
// antowire required
if (requiredConstructor != null) {
throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// autowire
if (candidate.getParameterTypes().length == 0) {
throw new IllegalStateException(
"Autowired annotation requires at least one argument: " + candidate);
}
// autowire required
boolean required = determineRequiredStatus(annotation);
// autowire required
if (required) {
//
if (!candidates.isEmpty()) {
throw new BeanCreationException(
"Invalid autowire-marked constructors: " + candidates +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// required
requiredConstructor = candidate;
}
//
candidates.add(candidate);
}
// autowire ,
else if (candidate.getParameterTypes().length == 0) {
//
defaultConstructor = candidate;
}
}
//
if (!candidates.isEmpty()) {
// required ,
if (requiredConstructor == null && defaultConstructor != null) {
//
candidates.add(defaultConstructor);
}
//
candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);
}
// ,
else {
candidateConstructors = new Constructor[0];
}
//
this.candidateConstructorsCache.put(beanClass, candidateConstructors);
}
}
}
// , null
return (candidateConstructors.length > 0 ? candidateConstructors : null);
}
// Bean
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {
// Bean
Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);
//
if (candidateConstructors == null) {
//
synchronized (this.candidateConstructorsCache) {
candidateConstructors = this.candidateConstructorsCache.get(beanClass);
if (candidateConstructors == null) {
// JDK ,
Constructor[] rawCandidates = beanClass.getDeclaredConstructors();
//
List candidates = new ArrayList(rawCandidates.length);
//autowire required
Constructor requiredConstructor = null;
//
Constructor defaultConstructor = null;
// , autowire ,
// required
for (Constructor> candidate : rawCandidates) {
// autowire (Annotation)
Annotation annotation = findAutowiredAnnotation(candidate);
// antowire
if (annotation != null) {
// antowire required
if (requiredConstructor != null) {
throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// autowire
if (candidate.getParameterTypes().length == 0) {
throw new IllegalStateException(
"Autowired annotation requires at least one argument: " + candidate);
}
// autowire required
boolean required = determineRequiredStatus(annotation);
// autowire required
if (required) {
//
if (!candidates.isEmpty()) {
throw new BeanCreationException(
"Invalid autowire-marked constructors: " + candidates +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
// required
requiredConstructor = candidate;
}
//
candidates.add(candidate);
}
// autowire ,
else if (candidate.getParameterTypes().length == 0) {
//
defaultConstructor = candidate;
}
}
//
if (!candidates.isEmpty()) {
// required ,
if (requiredConstructor == null && defaultConstructor != null) {
//
candidates.add(defaultConstructor);
}
//
candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);
}
// ,
else {
candidateConstructors = new Constructor[0];
}
//
this.candidateConstructorsCache.put(beanClass, candidateConstructors);
}
}
}
// , null
return (candidateConstructors.length > 0 ? candidateConstructors : null);
}
주 해 는 JDK 의 반사 메커니즘 을 이용 하여 컴 파일 할 때 나 실행 할 때 설 정 된 정 보 를 동적 으로 얻 는 것 에 불과 하 다. 주해 의 진정한 의 미 는 주해 표 지 를 통 해 주해 가 있 는 대상 의 정보 와 주해 에 설 정 된 정 보 를 얻 는 것 이다.
마지막 으로 제 개인 홈 페이지 방문 을 환영 합 니 다: 1024 s
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[토비의 스프링] - 예외체크 예외가 발생할 수 있는 메소드를 사용할 경우 반드시 예외를 처리하는 코드를 함께 작성해야 한다. 콜백과 템플릿처럼 긴밀하게 역할을 분담하고 있는 관계가 아니라면 자신의 코드에서 발생하는 예외를 그냥 던져버리는 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.