Create a spring HelloWorld program

Source: Internet
Author: User

Spring IOC

IOC refers to the inversion of control, the creation of objects, initialization, destruction and other work to the spring container. The spring container controls the life cycle of the object. To illustrate the way we traditionally create classes and the differences after using spring:

To create a Java class:

Package COM.YIHAI.SPRINGIOC;//IOC refers to control inversion. The creation, initialization, and destruction of objects are/are given to the spring container. The spring container controls the life cycle of the object. public class HelloWorld {public void Hello () {System.out.println ("Hello World");}}

Place the class in the Spring container:

<?xml version= "1.0" encoding= "UTF-8"?

><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance " xsi:schemalocation=" Http://www.springframework.org/schema/beans/ http Www.springframework.org/schema/beans/spring-beans-2.5.xsd "> <!--indicates that a class ID is a unique identifier for a class, and class represents the full name of the classes, that is, the package name + class name -- <bean id= "HelloWorld" class= "Com.yihai.springioc.HelloWorld" > </bean></beans>


Remove objects from the spring container for classes

Package Com.yihai.springioc;import Org.springframework.context.applicationcontext;import ORG.SPRINGFRAMEWORK.CONTEXT.SUPPORT.CLASSPATHXMLAPPLICATIONCONTEXT;//IOC refers to control inversion. The creation, initialization, and destruction of objects are/are given to the spring container.

The spring container controls the life cycle of the object. public class HelloWorld {public void Hello () {System.out.println ("Hello World");} public static void Main (string[] args) {/** * 1, starting spring container * 2, extracting the object from the Spring container * 3, Object invocation method */applicationcontext context = new Classpathxmlapplicationcontext ("Beanioc.xml"); HelloWorld HelloWorld = (HelloWorld) context.getbean ("HelloWorld"); Helloworld.hello ();}}


This type of create Bean object is the default constructor for the spring call class to create the object. The other way to create an object in 2 is to use the static factory method to create the bean and use the instance factory method to create the bean.

Create a spring HelloWorld program

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.