Spring IoC Annotation

Source: Internet
Author: User
Tags regex expression
@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @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@Componentpublic @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") annotation indicates that a bean is defined and the ID is userdao. Similar to this annotation, there are: @ repository, @ service, @ controller is used to annotate the DaO layer, the business logic layer and the control layer. They will all be parsed into beans, but the following spring will assign some special functions, therefore, different annotations are written based on the role of the class.

To define the scope of the bean when defining the bean, add another annotation @ scope.

@Target({ElementType.TYPE, ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @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;}

To implement bean assembly, use @ autowired, @ qualifier, @ Resource

@ Autowired is assembled by type by default. If no exception is found, the required attribute is defined as false and no exception will occur.

@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;}

If more than one bean matches in the container, use @ qualifier to specify the bean name and use it with @ autowired.

@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE})@Inherited@Documentedpublic @interface Qualifier {String value() default "";}


@ Resource is similar to @ autowired, but @ resource is under the javax. annotation package.

@Target({TYPE, FIELD, METHOD})@Retention(RUNTIME)public @interface Resource {

@ Resource ("car") to find the bean injection with the ID of car. This annotation requires a bean name attribute. If not specified, by default, the variable name or method name at the annotation is used as the bean name. Similar to @ resource, @ inject is rarely used. @ autowired is recommended.

@ Resource Assembly Sequence
If both name and type are specified, a unique matching bean is found in the spring context for assembly. If no matching bean is found, an exception is thrown.

If the name is specified, the bean with the matching name (ID) is searched in the context for assembly. If no name is found, an exception is thrown.

If type is specified, an exception will be thrown if a unique bean of type matching is found in the context for assembly. If no or multiple beans are found, an exception will be thrown.

If neither name nor type is specified, the Assembly is automatically performed in byname mode (see figure 2). If no matching is found, the matching is performed in userdao mode, automatic Assembly is performed if matching is performed;


After defining these annotations in the class, you must use the base-package attribute of component-scan in the context namespace in the configuration file to specify the scan path, the package and all the classes in the following sub-packages will be scanned.

<context:component-scan base-package="com.baobaotao.anno"/>

In this element, you can configure which classes to scan and which classes to exclude.

<Context: component-scan base-package = "com. auscend"> <! -- Security Configuration. If security is enabled, comment out --> <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"/>

Supported filter types:

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 oforg.springframework.core.type .TypeFilterInterface.

Use applicationcontext during exercise because some beanfacory does not support automatic dependency injection.







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.