Spring Notes 2-annotations, spring2-Annotations

Source: Internet
Author: User

Spring Notes 2-annotations, spring2-Annotations

I. Differences between attributes and member variables:
Attribute: exposed. getxxx/setxxx is called an attribute;
Member variable: private String name is a member variable or field.

2. Writing applicationContext. xml
<! -- Constraint -->
<Beans xmlns: context = "http://www.springframework.org/schema/context"
Xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-4.2.xsd
Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-4.2.xsd>
</Beans>

<! -- Enable spring support for annotation ioc in the spring configuration file, and specify the package to be scanned during spring initialization->
<Context: component-scan base-package = "com. itheima"> </context: component-scan>

<! -- Read the database configuration file->
<Context: property-placeholder location = "classpath: db. properties"/>
<Bean name = "dataSource" class = "com. mchange. v2.c3p0. ComboPooledDataSource">
<Property name = "driverClass" value = "$ {jdbc. driverClass}"> </property>
<Property name = "jdbcUrl" value = "$ {jdbc. url}"> </property>
<Property name = "user" value = "$ {jdbc. user}"> </property>
<Property name = "password" value = "$ {jdbc. password}"> </property>
</Bean>


Iii. annotation Interpretation
@ Component: Let spring manage resources. It is equivalent to configuring a bean in xml. If the value attribute is not specified, the default bean id is the class name of the current class. Lowercase letters.
@ Controller: Annotations used for presentation layer.
@ Service: Annotations used for the Business Layer
@ Repository: annotation used for persistence layer
Details: if there is only one attribute in the annotation that needs to be assigned a value, and the name is value, the value can be left empty.

@ Autowired:
Purpose: automatically inject data by type.
The set method can be omitted when annotation injection is used. It can only inject other bean types. When there are multiple types of matching, use the name of the object variable to be injected as the bean id. You can also find it in the spring container and inject it successfully. If it cannot be found, an error is returned.

@ Qualifier: based on automatic injection by type, then injection by Bean id. It cannot be used independently when injecting fields. It must be used together with @ Autowire. However, it can be used independently when injecting method parameters.

@ Resource: inject data directly according to the Bean id. It can only inject other bean types.

@ Value: inject data of the basic data type and String type

@ Scope: Specify the bean Scope. Value: the value of the specified range. Value: singleton prototype request session globalsession

@ PostConstruct: Specifies the initialization method.

@ PreDestroy: used to specify the destruction method

@ Configuration: specifies that the current class is a Configuration class. Annotations are loaded from this class. Read the @ ComponentScan Annotation on the class to initialize the spring container.

@ ComponentScan: Specifies the package to be scanned by spring when initializing the container. (The basePackages attribute is required in xml to specify the package to be scanned ). This is the same as the value attribute in this annotation.

@ PropertySource: used to load the configuration in the. properties file. value []: used to specify the location of the properties file. If it is in the class path, you need to write classpath:

@ Import: used to Import other configuration classes. value []: used to specify the bytecode of other configuration classes.

@ Bean: This annotation can only be written on the method, indicating that an object is created using this method and handed over to spring for management. Name: specify a name (Bean id) for the object created by @ bean annotation method ).

@ RunWith annotation replaces the original Runner; @ RunWith (SpringJUnit4ClassRunner. class)

@ ContextConfiguration specifies the location of the spring configuration file; @ ContextConfiguration (locations = {"classpath: bean. xml "})

Related Article

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.