Spring-01-helloworld

Source: Internet
Author: User

The traditional HelloWorld
    • Writing Java Classes
    package com.weixuan.spring;    publicclass HelloWorld {        publicvoidhello() {            System.out.println("Hello World .");                }        publicstaticvoidmain(String[] args) {        }    }
    • compiling into a. class file
    • Using ClassLoader to load class into the JVM
    • New one instance
    • All processes are fully involved
Using Spring's HelloWorld
    • Write a Java class
    • Write the configuration file, put the class into the spring container
    • Start the spring container (look for the configuration file under the Classpath to instantiate the container)
    • Remove the Java class from the container
    • Object. Method
      Control Flip Concept: the creation of objects, initialization, destruction and other actions to the spring container to do, by the spring container to control the object's life cycle. Note: The destruction is in case of scope being a singleton
HelloWorld instances

HelloWorld class

package com.weixuan.spring;publicclass HelloWorld {    /**     * 属性是共享的,可能会引发线程安全问题     */    publicvoidhello() {        System.out.println("Hello World .");            }}

Configuration file

<?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/sch Ema/beans/spring-beans-2.5.xsd ">            <!--put a class in the spring container, the class becomes the first letter of the bean ID, lowercase ---    <bean id= "helloWorld" class=" Com.weixuan.spring.HelloWorld ">        <!--collaborators and configuration for this bean go    </Bean>    <!--more beans definitions Go</Beans>

Client test File
-Launch IOC container
-Remove class HelloWorld
-Use Object (object. method)

 PackageCom.weixuan.test;ImportOrg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.weixuan.spring.HelloWorld; Public  class testofhelloworld extends HelloWorld {    //Traditional notation    @Test     Public void Test() {HelloWorld HelloWorld =NewHelloWorld ();    Helloworld.hello (); }//using the IOC container notation    @Test     Public void TESTIOC() {/** * 1, launch IOC container 2, remove HelloWorld 3, object. Method */        //1. Launch IOC containerApplicationContext ApplicationContext =NewClasspathxmlapplicationcontext ("Com/weixuan/spring/applicationcontext.xml");//2, remove HelloWorldHelloWorld HelloWorld = (HelloWorld) applicationcontext. Getbean ("HelloWorld");//3, object. MethodHelloworld.hello (); }}
Scope property
@Test    publicvoidtestSpring() {        new ClassPathXmlApplicationContext(                "com/weixuan/spring/applicationContext.xml");        HelloWorld helloWorld1 = (HelloWorld) applicationContext                .getBean("helloWorld");        HelloWorld helloWorld2 = (HelloWorld) applicationContext                .getBean("helloWorld");        System.out.println(helloWorld1);        System.out.println(helloWorld2);    }

You can see that the output is the same, indicating that the default is a singleton mode

com.weixuan.spring.HelloWorld@42com.weixuan.spring.HelloWorld@42e366c1

The singleton mode takes into account thread safety issues, and you can set the scope property to be a prototype mode

<bean id="helloWorld" class="com.weixuan.spring2.HelloWorld" scope="prototype"></bean>
Additional Property configuration
    • Init-method: After the constructor, there is a spring container that executes immediately, and the purpose of this method is to do some preparatory work before invoking the method after the constructor.
    • Destroy-method: When the scope is set to Singleton, destroy this method when the spring container is closed or destroyed. Otherwise, the shutdown resource must be manually closed by the developer.
    • Test file
 PackageCom.weixuan.initdestory; Public  class HelloWorld {     Public HelloWorld() {System.out.println ("Default constructor ..."); } Public void Init() {System.out.println ("Init ..."); } Public void Destory() {System.out.println ("Destory ..."); } Public void Hello() {System.out.println ("Hello world."); }}
<?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/sch Ema/beans/spring-beans-2.5.xsd ">            <!--put a class in the spring container, the class becomes the first letter of the bean ID, lowercase ---    <!--Init-method: After the constructor, there is a spring container that executes this method immediately after the constructor, before invoking the method, to do some preparatory work Destroy-method: When the scope is set When the spring container is closed or destroyed, the resource must be closed manually by the developer.    <bean id="HelloWorld" class="Com.weixuan.initdestory.HelloWorld"  Init-method="init" destroy-method="Destory" >                <!--collaborators and configuration for this bean go    </Bean>    <!--more beans definitions Go</Beans>

Test files and Results

 PackageCom.weixuan.test;ImportOrg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.weixuan.initdestory.HelloWorld; Public  class testofinitdestory extends HelloWorld {    PrivateApplicationContext ApplicationContext =NewClasspathxmlapplicationcontext ("Com/weixuan/initdestory/applicationcontext.xml");@Test     Public void Test() {HelloWorld HelloWorld = (HelloWorld) applicationcontext. Getbean ("HelloWorld"); Helloworld.hello ();/** * output, and no Destory method * Default constructor ... * Init ... * Hello world. */}@Test     Public void test2() {Classpathxmlapplicationcontext Classpathxmlapplicationcontext = (classpathxmlapplicationcontext) applicationCon        Text HelloWorld HelloWorld = (HelloWorld) classpathxmlapplicationcontext. Getbean ("HelloWorld");        Helloworld.hello (); Classpathxmlapplicationcontext.close ();/** * Scope defaults to singleton mode * This output: * default constructor ... * Init ... * Hello world. * .... * Destory ... * *        /** * Scope for prototype mode * output * Default constructor ... * Init ... * Hello world         . */}}

This is the simplest HelloWorld.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Spring-01-helloworld

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.