Annotation (3) -- Spring Annotation Development

Source: Internet
Author: User

The core function of the Spring framework, IoC (Inversion of Control), is to manage objects through Spring containers and map the composite relationships between objects. We usually declare the action, service, dao, and other layers in the xml configuration file, and then inform the framework of the injection method we want, then declare the get and set methods of the combined classes in the class. The Application of annotations in the Spring framework mainly solves such problems. Another core knowledge in the framework, AOP, is a cross-section method programming. In the program, it is generally only possible to set it once, so it is still in the configuration file. For example, I don't need to worry about setting declarative things once, so there is no need to use annotations to simplify it. Because the workload is almost the same. Let's take a look at how to use annotations for development in Spring!

First, add the namespace and constraint files to the configuration file:

 

2. Enable the annotation function of Spring so that the Spring environment can scan at a specified position and combine classes:

 
 

3. Declare the annotation class so that the Spring framework can recognize:

1. Hierarchical annotation of classes. Because our background development is divided into three layers for development, the Spring framework provides three annotation methods for different layers:

Control Layer: @ Controller

Service layer: @ Service

Persistent layer: @ Repository

Similar to the following in our configuration file:

 

By default, the Bean Object id declared using the annotation is named: the first letter of the class name is lower-case, and we do not need to set it. If you want to set it, for example, we specify the implementation class, the name points to the interface and you can directly add the value attribute: @ Service (value = "name"). Of course, value can be omitted.

The Spring framework also provides us with a general annotation instead of the preceding three layered Annotations: @ Component. Of course, we advocate better hierarchical annotation and clear hierarchy.

2. The automatic assembly function implements the combination relationship between objects. Specify the following annotations before the attribute:

@ Autowired: Type-based automatic assembly: byType

@ Resource: automatic assembly is completed by name + type: byName + byType. This method is recommended.

In addition, Spring provides the automatic assembly function of the two annotations. The attribute does not need to be declared set, and the get method can also complete the combination function, which is very convenient.

3. When declaring an action, you need to specify it as multiple instances to solve the thread security problem. In the configuration file, we usually declare the action class as follows:

 

Similar attributes are also provided in Spring annotation development to solve this problem. The front side of the action class is set as follows:

@ Scope ("prototype ")

4. When the Dao layer class is declared using the configuration file, it is usually like this:

  
  
   
 

How does the annotation development method inject sessionFactory? Because the Dao layer inherits the HibernateDaoSupport of the parent class, there is such a method in the parent class:

// Set injection method of sessionFactory in the parent class public final void setSessionFactory (SessionFactory sessionFactory) {if (this. hibernateTemplate = null | sessionFactory! = This. hibernateTemplate. getSessionFactory () {this. hibernateTemplate = createHibernateTemplate (sessionFactory );}}

This is the set injection method of sessionFactory, so if we can rewrite this method, perform @ Resource Annotation on this method to solve our problem, however, we can see that this method is modified with final. Therefore, we usually use the following method to solve the problem:

@ Repository ("orgDao") public class OrgDaoImpl extends HibernateDaoSupport implements OrgDao {@ Resource // you have customized the method, public void setSuperSessionFactory (SessionFactory sessionFactory) {// calls the setSessionFactory method super of the parent class. setSessionFactory (sessionFactory );}}

In this way, the sessionFactory of the Dao layer is injected, and we can write our Dao Layer Code normally. Of course, if we use Ibatis for the Dao layer, we can also use this method to inject the Ibatis core object sqlMapClient.

In summary, it is relatively simple to develop common annotations for Spring framework annotations. At least, compared with the other two frameworks, objects are managed through annotations, and the combination of objects, that is, the IoC function of Spring. The development of annotations can greatly improve our development efficiency. However, it violates the OCP principle to a certain extent, therefore, we strongly recommend that you use annotation development on a premise that our needs are relatively fixed and the changes are small.


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.