Implementation of IOC in Spring

xiaoxiao2021-03-06  75

Learn about the ideology of IOC mode and its advantages, then learn its implementation. The PicoContainer and Spring respectively described the implementation of IOC in the upper article. This article takes a detailed look at its implementation

Spring in the IOC through its entire frame, 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", IOC will have termed framework Design An indispensable part. In terms of implementation, Spring takes the form of the configuration file to implement the injective injection, and supports Type2 IOC (SETTER INJECTION) and Type3 IOC (Constructor INJECTION).

The core of the implementation of IOC in Spring is its Core Bean Factory, which assembles components within the frame in a certain coupling, and provides a service-oriented programming mode for using it (SOP: Service-Orient Programming), For example, AOP in Spring, as well as the implementation of Hibernate, Ibatics. First, start from the most basic Factory Bean, first look at Org.SpringFramework.beans.Factory.Bean

Factory interface, it is a very simple interface, the GetBean method is the most important way, Spring is usually used to use XML to populate beans, so it is often used for XMLFactoryBean.

Take a look at your usage with a simple example. First write two Bean classes:

EXAMPLEBEAN Class:

Public class examplebean {

PRIVATE STRING PSNNAME = NULL;

PRIVATE REFBEAN REFBEAN = NULL;

PRIVATE STRINGDINFO = 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;

}

XML configuration file bean.xml

"http://www.springframework.org/dtd/spring-beans.dtd">

xkf

beijing

100085

You can then write a test class to test, of course, Spring-Core.jar in Spring, and Commons-Logging.jar, of course, in Elipse, you can easily implement the Spring-IDE plugin.

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 =

"" "" "" "" "" "" "" "" "" "" "" "" ""

System.out.println (eb.getpsnname ());

System.out.println (Eb.GetadDinfo ());

}

Catch (Exception E) {

E.PrintStackTrace ();

}

}

In this way, pass

Beanfactory

of

GetBean

Method, and

xml

Profile, avoid it

Test

Direct instantiation in the class

Examplebean

Eliminate the application

(Test)

Service

(ExampleBean)

The coupling between, it is realized

IOC

(Control reversal) or realize dependence (

Dependency Injection

).

转载请注明原文地址:https://www.9cbs.com/read-95234.html

New Post(0)