Spring Example: an example of IOC dependency injection

Source: Internet
Author: User
Tags chop

I. Using MYECLIPSE to introduce spring features:

After creating a new Web project, right-click the project name, select MyEclipse, select Add spring capabilities, and select the desired libraries.

Two. Spring-inspired interface-oriented programming:

Interface Bag:

Contains two interfaces

1  Package Sshinterface; 2 3  Public Interface Person {4      Public void useaxe (); 5 }

1  Package Sshinterface; 2 3  Public Interface Axe {4      Public String Chop (); 5 }

  

Implement Bag:

Chinese implements the person, Steelaxe, and stoneaxe to implement the axe

 Packageimplement;ImportSshinterface.axe;ImportSshinterface.person; Public classChinsesImplementsperson{PrivateAxe Axe;  Publicchinses () {} Public voidsetaxe (Axe Axe) { This. Axe =Axe; }     Public voidUseaxe () {//TODO auto-generated Method StubSystem.err.println (Axe.chop ()); }    }

 package   implement;  import   Sshinterface.axe;  public  class  Steelaxe implements   axe{ public   Steelaxe () {}  public  
   
     String Chop () { 
    // 
     TODO auto-generated    Method stub  
    return  "Iron Axe relatively fast" 
    
   
 Package implement; Import Sshinterface.axe;  Public class Implements axe{        public  stoneaxe () {}        public  String Chop () {         // TODO auto-generated Method stub        return "Cut the tree with an axe";}    }

Spring provides applicationcontext.xml to organize the various beans:

1<?xml version= "1.0" encoding= "UTF-8"?>2<Beans3Xmlns= "Http://www.springframework.org/schema/beans"4Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"5xmlns:p= "http://www.springframework.org/schema/p"6Xsi:schemalocation= "Http://www.springframework.org/schema/beans7http//www.springframework.org/schema/beans/spring-beans-3.0.xsd ">8<!--Spring calls the parameterless constructor for an instance, invokes the setter injection property value, configures the Chinses instance, and implements the implement. Chinses-->9<bean id= "Chinses"class= "Implement." Chinses ">Ten<!--property Tag: If it is an object with ref, if it is an attribute, use value-- One<!--here, Steelaxe is injected to axe properties-- A<property name= "Axe" ref= "Steelaxe" ></property> -</bean> -<bean id= "Stoneaxe"class= "Implement." Stoneaxe "></bean> the<bean id= "Steelaxe"class= "Implement." Steelaxe "></bean> -  -</beans>

Test class:

1  Packagesshtest;2 3 ImportOrg.springframework.context.ApplicationContext;4 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;5 6 ImportSshinterface.person;7 8 ImportExample.examplebean;9 Ten  Public classSshtest { One      Public Static voidMain (string[] args) { AApplicationContext app =NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); -Person China = (person) app.getbean ("chinses");//according to the name of the XML to get the bean, relative to the new chinses, here does not need to choose in Java code What kind of axe, because in the XML selection, - china.useaxe (); the     } -}

Console results:

In XML, Steelaxe was chosen to inject in axe.

Spring Example: an example of IOC dependency injection

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.