spring IOC annotation
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Component {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any
*/
String value() default "";
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any
*/
String value() default "";
}
@ Component ("userDao") 주 해 는 클래스 에 있어 서 하나의 bean, id 는 userDao 로 정의 되 었 습 니 다. 이 주해 와 유사 한 것 은 @ Repository, @ Service, @ Controller 는 dao 층, 업무 논리 층 과 통제 층 을 주석 하 는 데 사 용 됩 니 다. 그들 은 모두 bean 으로 해석 되 지만 뒤의 몇 개의 spring 은 특수 한 기능 을 부여 하기 때문에 쓸 때 유형의 역할 에 따라 서로 다른 주 해 를 씁 니 다.
bean 을 정의 할 때 bean 에 scope 를 정의 하려 면 주석 @ Scope 를 추가 하 십시오.
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Scope {
/**
* Specifies the scope to use for the annotated component/bean.
* @return the specified scope
*/
String value() default BeanDefinition.SCOPE_SINGLETON;
/**
* Specifies whether a component should be configured as a scoped proxy
* and if so, whether the proxy should be interface-based or subclass-based.
* <p>Defaults to {@link ScopedProxyMode#NO}, indicating that no scoped
* proxy should be created.
* <p>Analogous to {@literal <aop:scoped-proxy/>} support in Spring XML.
*/
ScopedProxyMode proxyMode() default ScopedProxyMode.DEFAULT;
}
bean 의 조립 을 실현 하려 면 @ Autowired, @ Qualifier, @ Resource 를 사용 하 십시오.
@ Autowired 는 기본적으로 유형 별로 조립 합 니 다. 찾 지 못 하면 이상 이 발생 합 니 다. required 속성 은 false 로 정의 하면 이상 이 발생 하지 않 습 니 다.
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD})
public @interface Autowired {
/**
* Declares whether the annotated dependency is required.
* <p>Defaults to <code>true</code>.
*/
boolean required() default true;
}
용기 에 하나 이상 의 bean 이 일치 하면 @ Qualifier 를 사용 하여 Bean 의 이름 을 제한 하고 @ Autowired 와 함께 사용 합 니 다.
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Inherited
@Documented
public @interface Qualifier {
String value() default "";
}
@ Resource 는 @ Autowired 와 유사 하지만 @ Resource 는 javax. annotation 패키지 에 있 습 니 다.
@Target({TYPE, FIELD, METHOD})
@Retention(RUNTIME)
public @interface Resource {
@ Resource ("car") 는 id 를 car 의 bean 에 주입 합 니 다. 이 주 해 는 bean 이름 의 속성 을 제공 해 야 합 니 다. 지정 되 지 않 으 면 기본적으로 레이 블 에 있 는 변수 이름 이나 방법 이름 을 bean 의 이름 으로 사용 합 니 다.@ Resource 와 같은 것 은 @ Inject 도 있 습 니 다. 거의 사용 하지 않 습 니 다. @ Autowired 를 사용 하 는 것 을 권장 합 니 다.
@ Resource 조립 순서 name 과 type 을 동시에 지정 하면 Spring 컨 텍스트 에서 유일 하 게 일치 하 는 bean 을 찾 아 조립 하고 찾 지 못 하면 이상 을 던 집 니 다.
name 을 지정 하면 컨 텍스트 에서 이름 (id) 과 일치 하 는 bean 을 찾 아 조립 합 니 다. 찾 지 못 하면 이상 을 던 집 니 다.
type 을 지정 하면 컨 텍스트 에서 형식 이 일치 하 는 유일한 bean 을 찾 아 조립 합 니 다. 찾 지 못 하거나 여러 개 를 찾 으 면 이상 을 던 집 니 다.
name 도 지정 되 지 않 고 type 도 지정 되 지 않 으 면 자동 으로 by Name 방식 으로 조립 합 니 다 (2 참조).일치 하지 않 으 면 원본 형식 (UserDao) 으로 되 돌아 가 일치 하고 일치 하면 자동 으로 조립 합 니 다.
클래스 에서 이 주 해 를 정의 한 후 설정 파일 에서 context 네 임 스페이스 의 component - can 의 base - package 속성 을 사용 하여 스 캔 경 로 를 지정 하면 이 가방 과 아래 의 모든 하위 가방 의 종 류 를 스 캔 합 니 다.
<context:component-scan base-package="com.baobaotao.anno"/>
이 요소 에 서 는 어떤 종 류 를 스 캔 하고 어떤 종 류 를 제거 하 는 지 설정 할 수 있 습 니 다.
<context:component-scan base-package="com.auscend">
<!-- , , -->
<context:exclude-filter type="regex" expression="com.auscend.secure.*"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<context:component-scan base-package="com.baobaotao">
<context:include-filter type="regex" expression="com\.baobaotao\.anno.*Dao"/>
<context:include-filter type="regex" expression="com\.baobaotao\.anno.*Service"/>
<context:exclude-filter type="aspectj" expression="com.baobaotao..*Controller+"/>
</context:component-scan>
<context:component-scan base-package="com.baobaotao" resource-pattern="anno/*.class"/>
지원 하 는 필터 종류:
Filter Type
Example Expression
Description
annotation
org.example.SomeAnnotation
An annotation to be present at the type level in target components.
assignable
org.example.SomeClass
A class (or interface) that the target components are assignable to (extend/implement).
aspectj
org.example..*Service+
An AspectJ type expression to be matched by the target components.
regex
org\.example\.Default.*
A regex expression to be matched by the target components class names.
custom
org.example.MyTypeFilter
A custom implementation of the
org.springframework.core.type .TypeFilter
interface. 연습 할 때 ApplicationContext 를 사용 하 십시오. 일부 BeanFacory 는 자동 의존 주입 을 지원 하지 않 기 때 문 입 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.