Spring Framework Learning (iii) development of spring annotations

Source: Internet
Author: User

spring-Annotation Development

1. Adding this code to the Applicationcontext.xml allows the IOC container to load a class that scans for 4 types of annotations:

base-package="com.bjsxt.tmall" ></context:component-scan>
<?xml version= "1.0" encoding= "UTF-8"?><Beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context.xsd "><!--when the IOC container is loaded, the classes that have the following 4 types of annotations are automatically scanned in the Tmall packet and in the sub-package: @Respository-corresponding DAO @Service-corresponding service @Controller-corresponding action @Component-the other situation-<context:component-scan base- Package= "Com.bjsxt.tmall" ></context:component-scan></beans>

2. Now to develop the code for each layer:

(1) First of all, Action:action relies on service, then the problem comes. How can we label this class as action? How can I get the declared service class to get objects dynamically?

@Controller: Be able to label this class as the control layer. when the IOC container is created, the IOC scans the class labeled by this annotation and creates the class.

@Controller                    //Spring Comments on the action layer 

@Resource (IOC injection): The labeled attribute will find the class from the IOC container based on the property name (the name of the class name in the lower case), use reflection to new out the object and inject it into this property

     @Resource    //IOC injects      null;   
@SuppressWarnings ("All")    // Ignore warning @Controller                    //Spring Comment on action layer @ Scope (value= "prototype")   // Callout The class is a spring default singleton for multiple cases, whereas action is a thread that requests an object.  Publicclass  cashieraction {    //action dependent service    @Resource    // IOC injection    Private null;  

(2) Service and action are the same

@Service    //Spring Comments on the Service layer 
1 @Service    //Spring Comment 2publicclass for Service layer Cashierservice {3     ///SERVOCE relies on calling DAO layer 4     @Resource    //IOC container injection 5     Private null;

(3) The DAO layer accesses the database layer, has no dependent layer but relies on the entity class

@Resource: Comments on the DAO layer.

@Resource: IOC Injection

    But what is injected here is not the class in MVC, it is an entity class, the entity class is not configured with annotations, but is configured with Applicationcontext.xml! The configuration file configuration is also used to inject annotations.

1 @Repository    //Spring notes to the DAO layer 2publicclass  Cashierdao {3    @Resource4     privatenull;

Applicationcontext.xml Configuring entity classes:

1  class= "Com.wzh.common.Dbinfo" >2         <property name= "Driverclassname" value= " Com.mysql.jdbc.Driver "></property>3         <property name=" url "value=" 123 "></property >4         <property name= "username" value= "root" ></property>5         <property Name= "password" value= "123" ></property>6     </bean>

As you can see here, annotations and configuration files are not conflicting and can be used together.

Annotation development is much simpler than configuration file development, but there are some things that can be lost. What it loses is that annotations are developed in the Java class and cannot be changed directly to the configuration file as a configuration file to dynamically modify Java code, but although it is not as high as the configuration file, the modification of the Java code only modifies the variable name.

Spring Framework Learning (iii) development of spring annotations

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.