Implementation of IOC in Spring

Source: Internet
Author: User
I understand the idea and advantages of the IOC model, and then learn how to implement it. In this article, let's take a closer look at its implementation in Spring.

In Spring, IOC runs through its entire framework, but as martinflower says: "saying that these lightweight containers are special because they use inversion of control is like saying my car is special because it has wheels", which is already called an essential part of framework design. In terms of implementation, Spring adopts a configuration file to implement dependency Injection, and supports Type2 IOC (Setter Injection) and Type3 IOC (Constructor Injection ).

The Core of IOC implementation in Spring is its Core Bean Factory, which assembles components in the framework with a certain degree of coupling, it also provides a Service-oriented Programming model (SOP: Service-Orient Programming) for its applications, such as AOP in Spring and persistence (Hibernate and ibatics.

First, let's look at org. springframework. beans. factory. Bean

Factory interface, which is a very simple interface. The getBean method is the most important method. Spring usually uses xml for populate Bean, so XMLFactoryBean is commonly used.

Use a simple example to check its usage. First, write down two Bean classes:

ExampleBean class:


     
      public class ExampleBean {       private String psnName=null;       private RefBean refbean=null;       private String addinfo=null;       public String getAddinfo() {              return getRefbean().getAddress()+getRefbean().getZipcode();       }       public String getPsnName() {              return psnName;       }       public void setPsnName(String psnName) {              this.psnName = psnName;       }       public void setRefbean(RefBean refbean) {              this.refbean = refbean;       }      public RefBean getRefbean() {              return refbean;       }       public void setAddinfo(String addinfo) {              this.addinfo = addinfo;       }}
     

RefBean class:


     
      public class RefBean {       public String getAddress() {              return address;       }       public void setAddress(String address) {              this.address = address;       }       public String getZipcode() {              return zipcode;       }       public void setZipcode(String zipcode) {              this.zipcode = zipcode;       }       private String zipcode=null;       private String address=null;}
     

Its xml configuration file Bean. xml


     
      <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd"><beans>  <bean id="exampleBean" class="test.ExampleBean">    <property name="psnName"><value>xkf</value></property>    <property name="refbean">       <ref bean="refBean"/>    </property>  </bean>  <bean id="refBean" class="test.RefBean">  <property name="address"><value>BeiJing</value></property>  <property name="zipcode"><value>100085</value></property>  </bean></beans>
     

Then you can write a test class to test, of course, the need for Spring Spring-core.jar and commons-logging.jar, of course, in elipse can be easily achieved by installing the spring-ide plug-in.


     
      public class Test {       public static void main(String[] args){              try{              Resource input = new ClassPathResource("test/Bean.xml");              System.out.println("resource is:"+input);              BeanFactory factory = new XmlBeanFactory(input);              ExampleBean eb =              (ExampleBean)factory.getBean("exampleBean");              System.out.println(eb.getPsnName());              System.out.println(eb.getAddinfo());       }       catch(Exception e){              e.printStackTrace();       }}
     

In this way, through BeanFactory's getBean method and xml configuration file, the ExampleBean can be directly instantiated in the test class, eliminating the coupling between the application (Test) and the Service (ExampleBean, IOC or Dependency Injection is implemented ). (T111)

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.