Spring Note 3

Source: Internet
Author: User

annotation (note)(visible spring_pref,3.11)

To add annotation, you need to beans.xml the <beans> tag plus attribute xmlns:context= "Http://www.springframework.org/schema/context" and <beans> label <context:annotation-config/> Tag under the jurisdiction. The former is the namespace of the new XML, which indicates that the bean of the XML file has participated in the annotation. The former attribute, preceded by a colon, represents an XML namespace, and a colon indicates the prefix that the label can carry in that XML. No colon is the default namespace, and a colon is the namespace of the label with a prefix.

By setting the editor's Xml-catalog, you can add the first name dictionary that is defined by the former, which makes it easy to complete automatically when editing.

The. xsd file is a metadata file and is a new. dtd file, both of which are XML syntax files, also known as schema. The header defined in the XML file. Schema location uses the domain name as the key for the. xsd file, ensuring that each file's key is unique.

If the source code is not contacted, it can only be injected using XML, and annotation cannot be used.

@Autowired Annotations, used above the setter function (. Java).

There is a annotation case, if there is a bean corresponding to the implementation needs to inject, such as the class setter injected,<bean> under the original need to write <property>, can be by the setter function in. java before adding @ autowired Annotations, the. XML in the <bean> does not write the property can also complete the dynamic assembly.

Autowired is automatically assembled as Bytype by default. If there are multiple bean,bytype of the same class in the Beans.xml, the automatic assembly will be an error, you need to add @Qualifier("value") in front of the setter's parameters, value is Qualifier value, Should be defined in the bean, which can be replaced by the bean ID by default and used to specify which bean to assemble. (not commonly used)

@Required annotations , which are used above the setter function (. java).

The target object of the setter must be injected, or an error will be added.

@Resource annotations, which are used above the setter function (. java), specify the bean to use, default ByName, and Bytype if the name is not found. (Recommended use)

@Resource (name = "U"), which specifies the name of the injected bean.

@Component annotations can be used instead of <bean> tags.

Using @component above a class means that the class becomes a component that can be injected as an element of the spring container. Use need to write <context:component-scan base-package= "value"/> in Beans.xml; Value is the package name (which can be the folder name Com.example of the entire project) for the specific stored bean, and the annotation @component on the class name of the implementation class, indicating that the object produced by the class can be used as a Component (component) Write @resource annotations above the class that uses the component. classes marked as component can be taken directly by the Getbean function.

In practice, the JVM, because of Component-scan, scans the package for classes with @component annotations, generating the object as an alternative component, which is named the standard name (such as Userdaoimpl, the first lowercase of the class name).

You can name yourself by means of @component ("value") or @component (value= "value"), and value is the object (component). specification development requires the designation of Component object names for easy injection use.

The @Resource (name= "value") can be used to specify which object to use as the Resource (Resource), and value is the object name.

@Component, @Repository,@Service,@Controller These four types of annotations are the spring default component annotations. You can set the filter filtering scan type in Component-scan. (Ginseng pref_3.12)

@Scope ("value"), which is used on top of a component annotation, such as @component, to specify the type of the component. The component is a singleton when value is Singleton, and is re-created each time prototype.

@PostConstruct, @PreDestroy, used in the class implementation (. java), above the function. The former specifies that a function runs after the object is constructed, which specifies that a function runs before the object is refactored. That is, Init and destroy in XML.

Generally speaking, there is a source of direct write annotation development efficiency, without the source code can be changed. xml file.

Spring Note 3

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.