Spring3 annotation Zero Configuration

Source: Internet
Author: User

When we used to learn spring, all the configuration information was written in applicationcontext. xml. The general example is as follows:

Java code:
View copies to clipboard and print
<Beans>
<Bean name = "ds" class = "org. Apache. commons. DBCP. basicdatasource">
<Property name = "driverclassname" value = "oracle. JDBC. Driver. oracledriver"/>
<Property name = "url" value = "JDBC: oracle: thin: @ localhost: 1521: wangbin"/>
<Property name = "username" value = "tech37"/>
<Property name = "password" value = "tech37"/>
</Bean>

<Bean name = "txmanager" class = "org. springframework. JDBC. datasource. cetcetransactionmanager">
<Property name = "datasource" ref = "ds"/>
</Bean>
<TX: Advice id = "txadvice" transaction-Manager = "txmanager">
<TX: Attributes>
<TX: method name = "get *" Read-Only = "true"/>
<TX: method name = "*"/>
</TX: Attributes>
</TX: Advice>

<AOP: config>
<AOP: Advisor advice-ref = "txadvice"
Pointcut = "execution (* CN. javass .. business. EBO. * EBO. * (..)"/>
</AOP: config>
</Beans>

In the above example, we can see three typical spring functions:
1. IOC container, such:
<Bean…>
<Property... />
</Bean>
2. AOP
<AOP: config/>
3. Transactions
<TX: advice/>
First, we will learn how to use annotations to construct an IOC container.
Use annotations to register beans with spring containers. You must register <context: component-scan base-package = "cn. S"/> In applicationcontext. xml. Indicates CN. in the ipvs package and its sub-packages, if a class has a specific Annotation on its head [@ component/@ repository/@ service/@ controller ], the object is registered as a bean in the spring container.
The above four annotations have exactly the same usage, but only have semantic differences.
@ Component is a common form of all spring management components. Spring also provides more refined annotation forms: @ repository, @ service, and @ controller, which correspond to data layer beans respectively, service layer bean and performance layer bean.
@ Component is not recommended.
These four annotations can be placed on the class head. If the value [@ Service] is not specified, the bean name is the first letter of the simple Class Name of the class in lowercase by default; if value [@ Service ("Dao")] is specified, value is used as the ban name.
Note: What will happen if both CN. S. sampledao and CN. javass1.sampledao only use the @ service annotation without specifying the value?
In addition to registering beans, you can set attributes on <bean> to control other attributes of beans, such:
<Bean name = "" class = ""
Lazy-init = "true" // whether Initialization is delayed
Scope = "prototype" // bean Lifecycle
Depends-on = "other beans" // dependent on other beans
/>
There are also corresponding annotations in spring to correspond
@ Lazy
@ Scope
@ Dependson
They are all placed on the head of the class.
In addition to registering beans, you can set attributes on <bean> to control bean initialization methods and destructor, for example:
<Bean name = "" class = ""
Init-method = "init" // Initialization Method
Destroy-method = "close" // destructor
/>
In spring, there are also corresponding beans. Of course, these two annotations are built in JDK.
@ Postconstruct
@ Predestroy
These two annotations are placed on the method head.
@ Autowired search from spring context based on bean type. We need to learn this annotation from the following aspects:
1. What can it put on?
It can be placed on attributes, methods, and constructor.
2. Based on what injection?
2.1 If the implementation class of an interface is unique in the spring container, you can use @ autowired to inject the interface correctly, for example:
@ Autowired
Private sampledao;
2.2 If the implementation class of an interface is not unique in the spring container
2.2.1 use @ qualifier to specify the name of the injected bean, as shown in figure
@ Autowired
@ Qualifier ("sampledaoimpl ")
Private sampledao;
2.2.2 use @ primary to specify the top priority among multiple beans. Note that it is not used with @ autowired but with @ service, as shown in figure
@ Service @ primary sampledaoimpl
2.2.3 use attribute name, method parameter name, and constructor parameter name to set which bean to be injected, as shown in
Public class sampledaoimpl implements sampledao
Public class sampledaoimpl1 implements sampledao
Corresponding
@ Autowired
Private sampledao sampledaoimpl;
Note: The property name must exist after compilation. However, you must specify the corresponding compilation options for the method parameter name and constructor parameter name to retain the property name.
3. @ autowired has only one option, Boolean required () default true; required or not, and the default value is true. Therefore, if you only use @ autowired, you must be able to inject it. Otherwise, an error is reported.
 
@ Resource has a similar role as @ autowired.
Spring also supports the use of @ configuration to use a class as an IOC container. If @ bean is registered in a method header, it will be used as a bean in the spring container.

Java code:
View copies to clipboard and print
@ Configuration ("CTX ")
Public class javaapplicationcontext {
@ Bean
Public String Hello (){
Return "hello ";
}
@ Bean
Public int max (){
Return 9;
}
}
Use annotationconfigapplicationcontext to obtain the spring container
Applicationcontext context = new annotationconfigapplicationcontext (javaapplicationcontext. Class );
Use @ importresource ("classpath: applicationcontext. xml") to import other containers to this container.
The AOP annotations used by spring are divided into three layers:
1. @ aspect puts the Class header and regards this class as a plane, but this class must be explicitly registered in the spring container.
2. Place @ pointcut on the method head and define a pointcut expression that can be referenced by other methods.
3. 5 types of notifications. Www.2cto.com
3.1, @ before, pre-notification, put on method head.
3.2, @ After, post the [finally] notification and put it on the method head.
3.3 @ afterreturning, post [try] notification, put it on the method head, and use returning to reference the return value of the method.
3.4 @ afterthrowing, post the [catch] notification, put it on the method head, and use throwing to reference the thrown exception.
3.5, @ around, surround notification, placed on the method head, this method must determine whether the actual method is executed, and the return value must exist.

Video supporting PPT, video address [Spring annotation Zero Configuration video tutorial]

 

From: http://www.2cto.com/kf/201207/140774.html

 

 

Here is also good: http://www.douban.com/note/71602488/

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.