The use of the Spring IOC mechanism annotation configuration bean

Source: Internet
Author: User

I. Configuring Beans with Annotations

1.1 Overview

In contrast to XML, configuring beans in an annotated manner is more concise and elegant, and fits well with the concept of MVC component development, and is a common use in development.

1.2 Using annotations to identify components

① Normal components:@Component: identify a component managed by the Spring IOC container

② Persistence layer components:@Respository: identifies a persistence layer component managed by the Spring IOC container

③ Business Logic Layer components:@Service: identifies a business logic layer component managed by the Spring IOC container

④ Presentation Layer Controller components:@Controller: identifies a presentation layer controller component managed by the Spring IOC container

⑤ Component Naming conventions

[1] Default: Use the component's simple class name to get the first letter lowercase string as the bean ID

[2] specifying the bean ID using the Value property of the component annotation

Note: In fact, Spring does not have the ability to identify whether a component is the type it is tagged in, even if the @respository annotation is used on a presentation layer controller component and does not produce any errors.

So @respository, @Service, @Controller These annotations are just for the developer to clarify the role that the current component plays.

1.3 Scanning components

Components are identified by the above annotations and need to be scanned by spring to be able to detect them.

① Specifies the package to be scanned

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

② Detailed description

[1] The base-package property specifies a base class package that needs to be scanned, and the spring container will scan all classes in the base class package and its child packages.

[2] When you need to scan multiple packages can be separated by commas, you can also use a wildcard * to match.

[3] If you want to scan only specific classes, not all classes under the base package, you can use the Resource-pattern property to filter specific classes, for example:

<context:component-scan

base-package="Com.neuedu.component"

resource-pattern="Autowire/*.class"/>

[4] Inclusion and exclusion

<context:include-filter> child nodes represent the target classes to be included

Note: It is often necessary to work with the Use-default-filters property to achieve the effect of "only some components".

That is, by setting the Use-default-filters property to False, the default filter is disabled, and then only the components specified by the rules in Include-filter are scanned.

<context:exclude-filter> child nodes indicate the target class to exclude

Component-scan can have several include-filter and exclude-filter child nodes under a

where Include-filter and Exclude-filter attribute type value and meaning:

Assignable: Match introduction or exclusion based on full class name

Annotation: An entry or exclusion based on an annotation match

Filter an expression

Category

Example

Description

Annotation

Com.neuedu.XxxAnnotation

Filters all classes labeled Xxxannotation. This rule filters based on whether the target component is annotated with a specified type of annotation.

Assignable

Com.neuedu.BaseXxx

Filters all subclasses of the Basexxx class. This rule filters based on whether the target component is a subclass of the specified type.

Aspectj

Com.neuedu.*service+

All class names are subclasses of the class that end with service, or such. This rule is filtered according to the ASPECTJ expression.

Regex

com\.neuedu\.anno\.*

All classes under the Com.neuedu.anno package. This rule is filtered based on the class name that the regular expression matches to.

Custom

Com.neuedu.XxxTypeFilter

Use the Xxxtypefilter class to customize the filtering rules in a coded manner. The class must implement the Org.springframework.core.type.filter.TypeFilter interface

③jar Bag

One must be imported on the basis of the original JAR package combination:Spring-aop-4.0.0.release.jar

1.4 Component Assembly

① demand

Instances of service components are often needed in controller components, and instances of repository components are often used in service components. Spring can help us implement the assembly of attributes by annotating them.

② Realization Basis

When you specify a package to scan, the,<context:component-scan> element automatically registers a bean's post processor: an instance of Autowiredannotationbeanpostprocessor.

The post processor can automatically assemble properties labeled @Autowired, @Resource, or @inject annotations.

③ @Autowired Annotations

[1] According to the type to achieve automatic assembly .

[2] constructors, normal fields (even non-public), all methods with parameters can be applied @autowired annotations

[3] By default, all properties that use @autowired annotations need to be set. When spring cannot find a matching Bean assembly property, an exception is thrown.

[4] If a property is allowed to not be set, you can set the required property of the @autowired annotation to False

[5] By default, when there are multiple type-compatible beans in the IOC container, Spring tries to match whether the Bean's ID value is the same as the variable name, and if the same is the case, assemble it.

If the bean's ID value is not the same, automatic assembly by type will not work. The name of the bean can now be provided in the @qualifier annotation.

Spring even allows @qualifiter annotations to be annotated on the method's formal parameters to specify the name of the injected bean.

[6] @Autowired annotations can also be applied to the properties of an array type, at which time spring will automatically assemble all matching beans.

[7] @Autowired annotations can also be applied on a collection property, when spring reads the collection's type information, and then automatically assembles all the beans that are compatible with it.

[8] @Autowired Note When used on JAVA.UTIL.MAP, if the map's key value is string, then spring will automatically assemble the value type-compatible bean as the value, and the bean's ID value as the key.

④ @Resource

@Resource annotations require a property of the bean name, and if the property is empty, the variable or method name at the label is automatically used as the name of the bean.

⑤ @Inject

@Inject and @autowired annotations also inject matching beans by type, but no reqired properties.

Second, the specific steps:

① Note for declaring beans

@Component declare the current class as a normal component in the IOC container

@Controller declare the current class as a controller component in the IOC container

@Service declare the current class as a business logic layer component in the IOC container

@Repository declare the current class as a persisted layer component in the IOC container

Spring, based on the above annotations, does not tell whether the current class is really a controller or DAO, even if the tagged classes and annotations do not correspond or have syntax errors. But in the actual work, the special annotations must be marked on the corresponding class.

② with the configuration of the annotation-based bean, you need to import an additional jar package: Spring-aop-4.0.0.release.jar

③ need to set up automatically scanned packages

< Context:component-scan base-package = "Com.neuedu.ioc.bean"/>

④ using annotations, the default is to write the value of the ID according to the first letter of the class name, or you can use the Value property in the annotation to specify the Id,value property name or omit the annotation

    Annotation ID Value
@Component
public class Commoncomponent {
}

@Controller (value= "Neuedubookaction")
public class Bookaction {

}

@Service ("Happyservice")
public class Bookservice {

}

⑤ Automatic assembly using annotations: @Autowired annotations [The advantage is: even get, set methods do not have to write! ]

[1] First detects the type of attribute marked with the @autowired annotation

[2] According to the type of assembly

[3] If a bean of the specified type has more than one, the value of the ID is made according to the property name of the property to be assembled, and the bean is found

[4] If the bean is not found based on the ID value, you can use the @qualifier annotation to manually specify the ID of the bean to be assembled.

The use of the Spring IOC mechanism annotation configuration bean

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.