[Spring Frame] Spring Development Example: xml+ annotations.

Source: Internet
Author: User

Preface: This article for their own study of spring Records used, the article contains the summary of spring has been simple development, mainly related to the IOC knowledge, I hope to be able to new spring students to help, but also hope that we discuss the relevant knowledge.

I. Spring overview


1.1, What is spring:
Spring is an open source framework, and spring is a lightweight Java development framework that emerged in 2003 by Rod Johnson in his book expert one-on-one development and Some of the concepts and prototypes elaborated in design are derived. it is created to address the complexities of enterprise application development. one of the main advantages of the framework is its layered architecture, which allows the user to choose which component to use, while providing an integrated framework for Java EE application development. Spring uses basic JavaBean to accomplish things that were previously only possible by EJBS. However, the use of spring is not limited to server-side development. From the standpoint of simplicity, testability, and loose coupling, any Java application can benefit from spring. The core of spring is control inversion (IoC) and facet-oriented (AOP). In short,spring is a layered javase/ee full-stack (one-stop) Lightweight open-source framework.

1.2, why do I need to learn spring

Easy decoupling for simplified development

Spring is a large factory that can maintain all object creation and dependency relationships and give spring management

Support for AOP programming

Spring provides aspect-oriented programming, which can easily implement the functions of permission interception, operation monitoring, etc.

Support for declarative Transactions

The management of transactions can be done only through configuration, without the need for manual programming

Easy testing of programs

Spring support for JUNIT4, which can be easily tested with annotations for spring programs

Easy integration of a variety of excellent frameworks

Spring does not exclude a variety of excellent open source frameworks that provide direct support for a variety of excellent frameworks (e.g. Struts, Hibernate, MyBatis, quartz, etc.)

Reduce the difficulty of using Java EE APIs

Spring provides encapsulation for some of the most difficult APIs (JDBC, JavaMail, remote calls, and so on) in Java EE Development, making these API applications much less difficult to apply

Two, quick start of Spring IOC
Previous article: Http://www.cnblogs.com/wang-meng/p/5597490.html has an in-depth analysis of the IOC concept, which is not mentioned here, directly speaking of the rapid development of the IOC.

Getting Started with 2,1 rapid development
Step one: Download the Spring development package :
In order to facilitate the development of people, I have spring-framework-3.2.4.release-dist.zip and Spring-framework-3.0.2.release-dependencies.zip upload to My network disk. The address is as follows:
Link: http://pan.baidu.com/s/1slqvOzb Password: ikgn

Step two: Understand the directory structure of spring :

Docs        : Spring's development document Libs        : Spring's development package. Schema      : Constrain the document.


Step three: Create a project to introduce the JAR package:



Step four: Introduce the spring configuration file :

1 Create a applicationcontext.xml under SRC2 introduce constraints:3 <Beansxmlns= "Http://www.springframework.org/schema/beans"4 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"5 xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">6 7 </Beans>8 9 Write Tags:Ten <BeanID= "CustomerService"class= "Cn.itcast.spring.demo2.CustomerServiceImpl"></Bean>


Step Five: Write the test class:

1 @Test2      Public voiddemo1 () {3         //to create a spring factory class:4ApplicationContext ApplicationContext =NewClasspathxmlapplicationcontext ("Applicationcontext.xml");5CustomerService CustomerService =(customerservice) ApplicationContext6. Getbean ("CustomerService");7 Customerservice.sayhello ();8}

2.2 IOC and DI
Ioc:inverse of controller inversion is reversed. The bean creation is forwarded to the spring container.
Di:dependency injection Dependency Injection. The dependency properties of this class are injected into the process of creating this class in spring.

2.3 Spring's factory class

ApplicationContext

|----Classpathxmlapplicationcontext: Parse the XML under the classpath.

|----Filesystemxmlapplicationcontext: Parse the XML on the local disk.

Beanfactory and ApplicationContext are the factories in spring:

Beanfactory is the factory class for the old spring version:

* Instantiate the class the first time the Getbean method is called.

ApplicationContext is the new spring version of the factory class:

* When the core configuration file is loaded, all classes are instantiated.

[Spring Frame] Spring Development Example: xml+ 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.