Spring note--11. " 0 Configuration "Support

Source: Internet
Author: User

We can also use annotation to replace the XML configuration file. It seems that the annotation is the mainstream Ah!


Search Bean Class

We can use annotations to indicate to the schema what each Java class is. The @Component is labeled as a normal spring bean class. @Controller label a controller component class. @Service label a business logic component class. @Repository labeling a DAO component class

With these annotations, the architecture has been able to know what the Java classes are. Next we will tell the schema where to search for these. By adding the following line to the XML, the schema can automatically scan the Java class under each of the com.cm's sub-packages and handle All the Java classes contained on the spot bean. The Bean's ID is the first letter of the class name of each class, with the other letters reserved. Alternatively, you can specify the bean instance name @component ("Axe").

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

In addition Component-scan can have include-filter and exclude-filter to specify the spring bean type. Type allows us to set the Annotation,assignable,regex and AspectJ four filters. expression specifies the expressions required by the filter.


Specify the scope of the bean

Use @scope ("Prototyep") directly to calibrate the scope of the bean.


Configuring Dependencies using @resource

@Resource (name= "Stoneaxe") to configure the dependency of the current bean, which is equivalent to ref in XML. This annotation is placed before the Setaxe method to indicate that Stoneaxe is a parameter of setaxe.

If you let @resource modify member variables axe directly, then even setaxe methods are not required. When modifying the setaxe, it is sometimes possible to omit the name attribute, and to find out whether the bean is axe directly according to Setaxe. If you have any words, pass in the object. When a member variable is decorated, name defaults to the same name as the instance variable property


Life cycle Behavior

@PostConstruct and @predestroy can modify two methods to specify the bean's life cycle. They are equivalent to the Init-method and Destroy-method two attributes of the bean in XML.


Spring3.0 new Features

@DependsOn can decorate a bean class or method that specifies forcing other beans to be initialized. As follows:

@DependsOn ({"Steelaxe", "abc"}) @Componentpublic class Chinese implements person{}

The above comment will require spring to initialize the two beans of Steelaxe and ABC before initializing Chinese.

@Lazy can be used to specify the pre-initialization behavior of the bean, whether it is to be initialized automatically by the schema. Specified with the true or false parameter.

Spring4.0 new Features

@Autowired annotations are used to specify automatic assembly, which can be decorated with set methods, common methods, instance variables, and constructors. The set is decorated by default with the Bytype automatic assembly, which automatically searches the container for the same type of bean as the set requires and passes in. If more than one is found, an exception occurs. Not found on what is also good, do not error.

When modifying a common method, the schema will see if a matching bean can be found in the container for the method's parameters, and the method will be executed automatically if each parameter is found.

When the instance variable is decorated, an attempt is made to set the bean in the container that matches the instance variable to the value of the instance variable. If it is an array of instance variables, it is long to find all matching beans, passing in the array.

@Qualifier allows precise configuration through the bean's ID. Not used. You can not only modify the instance variable, but also decorate the parameter.

public void Setaxe (@q ("Steelaxe") Axe Axe)

This article from "Fingertip Light Fly" blog, declined reprint!

Spring note--11. " 0 Configuration "Support

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.