Spring's Simple Demo

Source: Internet
Author: User

----------------------------------------

Develop a simple demo of spring, with the following steps:

1. Construct a MAVEN project

2. Add a spring dependency package to the MAVEN project's Pom.xml file

3. Development of an interface API and two implementation classes Apione,apitwo

4. Add Spring configuration file Applicationcontext.xml

5. Writing the test class client

Here's a description of each step:

Step 1: Construct a MAVEN project Springtest

Step 2: Add the Spring Dependency package to the MAVEN project's Pom.xml file

1<dependencies>2<!--spring-related jar--3<dependency>4<groupId>org.springframework</groupId>5<artifactId>spring-context</artifactId>6<version>4.0.0.RELEASE</version>7</dependency>8 9<dependency>Ten<groupId>org.springframework</groupId> One<artifactId>spring-context-support</artifactId> A<version>4.0.0.RELEASE</version> -</dependency> -  the<dependency> -<groupId>org.springframework</groupId> -<artifactId>spring-jdbc</artifactId> -<version>4.0.0.RELEASE</version> +</dependency> -  +<dependency> A<groupId>org.springframework</groupId> at<artifactId>spring-orm</artifactId> -<version>4.0.0.RELEASE</version> -</dependency> -  -<dependency> -<groupId>org.springframework</groupId> in<artifactId>spring-tx</artifactId> -<version>4.0.0.RELEASE</version> to</dependency> +<dependency> -<groupId>junit</groupId> the<artifactId>junit</artifactId> *<version>3.8.1</version> $<scope>test</scope>Panax Notoginseng</dependency> -</dependencies>

Step 3: Develop an interface API and two implementation classes Apione,apitwo

Interface API:

1  Package com.springtest; 2 3  Public Interface Api {4      Public String Test (int  num); 5 }

Implement class Apione:

1  Packagecom.springtest;2 3  Public classApioneImplementsApi {4 5      PublicString Test (intnum) {6System.out.println ("Apione test method, num =" +num);7         returnnum + "Xixixi";8     }9}

Implement class Apitwo:

1  Packagecom.springtest;2 3  Public classApitwoImplementsApi {4 5      PublicString Test (intnum) {6System.out.println ("Apitwo test method, num =" +num);7         returnnum + "hahaha";8     }9}

Step 4: Add the Spring configuration file Applicationcontext.xml

1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4xmlns:context= "Http://www.springframework.org/schema/context"5xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"6xmlns:tx= "Http://www.springframework.org/schema/tx"7xsi:schemalocation="8http//Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd9http//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdTenhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd Onehttp//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> A          -<bean name= "API"class= "Com.springTest.ApiTwo" ></bean> -      the</beans>

Step 5: Write the test class client

1  Packagecom.springtest;2 3 ImportOrg.springframework.context.ApplicationContext;4 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;5 6  Public classClient {7      Public Static voidMain (string[] args) {8ApplicationContext context =NewClasspathxmlapplicationcontext (Newstring[]{"Applicationcontext.xml"});9API API = (APITWO) Context.getbean ("API"));TenString result = Api.test (123); OneSYSTEM.OUT.PRINTLN ("result =" +result); A     } -}

When <bean name= "API" class= "Com.springTest.ApiTwo" in the Applicationcontext.xml configuration file ></bean> run the test code client, Using the Apitwo implementation class, if you want to replace the implementation class, you only need to replace the configuration bean: <bean name= "API" class= "Com.springTest.ApiOne" ></bean> on it.

As you can see from the entire code above, there is no component of spring, and the customer code is programming only for interfaces, without knowing the exact name of the implementation class. At the same time, you can easily change the configuration file to switch to the specific underlying implementation class.

Beans and Containers

What is a bean in spring

In spring, the entities that make up the application and the objects managed by the spring IOC container are called beans. In a nutshell, beans are the objects that are initialized, assembled, and managed by the spring container, and there is nothing special about beans. The bean definition and the dependencies between the beans are described by configuration metadata.

Spring's IOC container

Org.springframework.beans.factory.BeanFactory is the actual representative of the spring IOC container, the IOC container is responsible for holding the beans and managing the beans.

The Spring IOC container reads the configuration metadata and uses it to instantiate, configure, and assemble individual objects in the application. Typically, we use simple XML as a descriptive format for configuration metadata. In the XML configuration metadata we can define the beans that we want to manage with the spring IOC container.

Beanfactory and ApplicationContext

Ioc/di is one of the core features of spring, and the many features provided by the spring framework are built on the IOC basis, in short, Beanfactory provides a configuration framework and basic functionality, ApplicationContext, however, adds more functionality to the core content of the Enterprise . The ApplicationContext is completely extended by Beanfactory.

Spring's Simple Demo

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.