spring- zero configuration support

1, the search bean class, using annotations marked spring bean.

     @Component: labeling an ordinary spring bean class

     @Controller: a controller component class label (Java EE components)

     @Service: a business logic component class label (Java EE components)

     @Repository: DAO marked a component class (Java EE components)

     After using common bean @Component notes, also you need to configure the search path of the bean in the configuration file.

     The introduction of the namespace: xmlns: context = "http://www.springframework.org/schema/context"

     The introduction of elements:

            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd

      Bean annotation to specify a search path:

<context:component-scan 
        base-package="com.lfy.bean"/>

       for example:

<? Xml Version = "1.0" encoding = "GBK" ?> 
< Beans xmlns = "http://www.springframework.org/schema/beans" 
    xmlns: xsi = "http://www.w3.org/2001 / XMLSchema-instance " 
    xmlns: context =" http://www.springframework.org/schema/context " 
    xsi: schemaLocation =" http://www.springframework.org/schema/beans 
    HTTP: //www.springframework. ORG / Schema / Beans / Spring-Beans-4.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.0.xsd " > 
    
    < ! - All Bean class specified automatically scan the package and its sub-packets -> 
    < context:component-scan base-package="com.lfy.bean"/>
</beans>

       Annotation-based mode, the default name of the bean instance is the first letter lowercase bean class, others are unchanged. Of course, spring is also supported in the specified bean instance when the name of the add annotations, such as @Component ( "chinese").

       Besides, may also be added by <component-scan ... /> element <include-filter ... /> or <exclude-filter ... /> subelement spring bean specified class, as long as the specified path java class meets the specified rule, regardless of whether they use annotations, spring will be the same as the bean class to handle it. Using <include-filter ... />, about two attributes specified in claim <exclude-filter> elements:

           1 "type: Specifies the type of filter. spring built filter type are:

                     annotation: Annotation filter, the filter Annotation specify a name, such as com.lfy.bean.MyAnnotation.

                     assignable: class name filter, the filter directly specify a Java class.

                     regex: regular expression filter, the filter specifies a regular expression that matches the regular expression Java classes will satisfy the rule.

                     aspectj: AspectJ filter.

           2 "expression: specifies the required filter expression.

2, specify the scope of the bean

     The default is a single pass. You can use use @Scope, you can also specify the scope-resolver property in the configuration file, the scope of the custom parser requires custom, custom parsers need to achieve ScopeMetadataResolver interface and provides custom scope resolution strategies.

     for example:

<beans ...>
        <context:component-scan base-package="com.lfy.bean" scope-resolver="="com.lfy.util.MyScopeResolver"/>
    </beans>

3, using the configuration-dependent @Resource

    @Resource has a name attribute that corresponds to the mode of action XML Schema ref attribute tag.

// At this time, the direct use of attribute fields Field Java EE specifications injection 
@Resource (name = "stoneAxe" )
 Private Ax Ax;

    or

// At this time, as a parameter setter incoming 
@Resource (name = "stoneAxe" )
 public  void setAxe (Ax Ax) {
    the this .axe = Ax; 
}

    Or the name attribute is omitted

4, @PostConstruct custom lifecycle behavior and @PreDestroy

     Earlier we used lifecycle are:

          init-method: Specifies the initialization method of the bean container --spring After completion callback will be injected in the process dependency bean.

          destroy-method: method before the specified bean containers will be destroyed --spring callback method in the bean before destruction.

      There are aspects of the corresponding Notes

          @PostConstruct: init-method corresponding to

          @PreDestroy:对应destroy-method

5, @ DependsOn forced initializing other bean

     bean class or method can be modified. Specify a string array as a parameter.

@dependsOn({"steelAxe","abc"})
@Component
public class Chinese{
   ...
}

6, @ Lazy whether to cancel the pre-initialization

     Use this annotation to specify the type of a boolean value property, which decides whether or not pre-initialized bean

@dependsOn({"steelAxe","abc"})
@Lazy(true)
@Component
public class Chinese{
   ...
}

7, @ Autowired automatic assembly, @ Qualifier precise assembly

Guess you like

Origin www.cnblogs.com/ZeroMZ/p/11333210.html