Simplified ssh construction and simplified ssh Construction

Source: Internet
Author: User

Simplified ssh construction and simplified ssh Construction

Simplified ssh Construction

Simply put, the bean in the configuration file applicationcontext. xml is replaced with the corresponding annotations, greatly reducing the amount of code and improving the readability of the Code.

Example source code: http://pan.baidu.com/s/1o7R5S8q

For example:

We usually need to inject values to the attributes of the objects in the configuration file applicationcontext. xml.

Do you think it is very troublesome to write this?

  

Now let's start to teach you how to use annotations and remove all the annoying code.

Before using annotations, you must first write a sentence in applicationcontext. xml.

<Context: component-scan base-package = "news"/>
Some information about the label below:
<! -- <Context: component-scan>: There is a use-default-filters attribute, which defaults to true. This means that all annotation-marked classes under the specified package will be scanned, and register it as bean. we can find that the scanning granularity is too large. What if you only want to scan the Controller under the specified package? In this case, the sub-tag <context: CE-filter> becomes a brave place. <Context: component-scan base-package = "news" use-default-filters = "false"> <context: exclude-filter type = "annotation" expression = "org. springframework. stereotype. controller "/> </context: component-scan> If use-dafault-filters is not specified, the default value is true, which means that you add <context: exclude-filter/> is the same as that you didn't add. If you want to use the sub-tag of <context: component-scan>, you must change the use-dafault-filters value to false. Of course, the other one is the opposite. Here, I will not explain it again. The above explanation is changed to one sentence: use-dafault-filters = "false": <context: exclude-filter> the specified scan does not exist. <context: include-filter> the specified scan <context: the base-package attribute of component-scan> is used to set the package to be scanned --> (this case is not used, but a full scan is used, <context: component-scan base-package = "news"/>

Now, let's start to analyze the functions of the annotations one by one:

First, let's talk about the class annotation.

Four annotations can be used to instantiate a class object.

@ Service

@ Repository

@ Controller

@ Entity

For the sake of standardization, we usually write like this (of course you don't have any influence on this score)

@ Service instantiate the business logic class (that is, our ServiceImpl), @ Repository instantiate the data layer class,

@ Controller instantiate Action class, @ Entity instantiate object class

By the way, don't forget that the annotation of the class also has a very important @ Scope (value = "prototype ")

This annotation means: Non-singleton. That is to say, different users use different objects. It is very useful. If this sentence is not added, everyone accesses the same object, it's easy to blow up.

Class annotation here, next let's talk about the annotation of global variables!

Annotation provided by jdk:

@ Autowired

@ Qualifier (name = "beanId ")

If you do not write @ Qualifier (name = "beanId"), the corresponding bean is matched by type by default (it is recommended that you do not write, which will make the code highly readable)

Annotations provided by spring

@ Resource (name = "beanId ")

If you do not write (name = "beanId"), the corresponding bean is matched by the variable name by default (it is recommended that you do not write it so that the code reading performance is high)

For example

  

 

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.