"Java framework projects from the beginning to the loading force" section I-the frenzied commentary of the spring framework IOC

Source: Internet
Author: User

Hello, long time no see, today we will study the IOC technology about spring framework.

Control reversal--spring facilitates loose coupling through a technique called inversion of Control (IoC). When an IOC is applied, other objects that an object relies on are passed in passively, rather than the object itself creating or locating dependent objects. You can think of the IOC as opposed to Jndi--not the object looking for dependencies from the container, but the container actively passing the dependency to it when the object is initialized without waiting for the object to be requested.

Well, let's start with a simple word. A straightforward concept is a bit abstract, so let's analyze it from a practical scenario. Mao wanted to find a girlfriend, his friend Er Mao helped introduce a girl, called Cui Hua. Then the Da Mao and Cui Flower met. Spent a period of time, because Cui flower to big Mao with him to play King Glory, but big Mao only will fight landlord, so broke up, for this big Mao sad for a long time. Later, Mao went to find ER Mao to help, hoping to find a girlfriend again. We look at this process, in fact, very troublesome, if the way to find a girlfriend through acquaintances, it is bound to be a contact, such as the first time he and Cui flower Blind Date, the second time with sweet-scented osmanthus, the third time with the West Orchid Blind Date, each time consumes a lot of energy.

Blind date to spend money to eat, watching movies to spend it, in case we can't talk, break up also trouble. This is the strong dependence of Mao and his every girlfriend.

Now, please make an imagination, imagine ourselves to be the gods, with our own will to give the DA Mao to arrange a girlfriend. Since we are gods, we can write a script of life for Da Mao.

Before Mao was introduced through ER Mao, only to know the Cui flower, osmanthus, and broccoli. Now, let's reproduce this process in the form of code.

Script:

Da Mao

package com.spring.bean;public class DaMao { private GirlFriend girlFriend; public GirlFriend getGirlFriend() { return girlFriend; } public void setGirlFriend(GirlFriend girlFriend) { this.girlFriend = girlFriend; } }

Girlfriend as a super class:

package com.spring.bean;/** * 女朋友 * @author Administrator */public class GirlFriend { private String hobby; public String getHobby() { return hobby; } public void setHobby(String hobby) { this.hobby = hobby; } }

Cui Hua inherits from girlfriend:

package com.spring.bean;public class CuiHua extends GirlFriend{ }

Test class:

package com.spring.test; import Com.spring.bean.CuiHua; import Com.spring.bean.DaMao; import com.spring.bean.GirlFriend; public class testdaomao {public static void main ( String[] args) {Damao Damao = new Damao (); Girlfriend Cuihua = new Cuihua (); Cuihua.sethobby ( "King Glory"); Damao.setgirlfriend (Cuihua); //see what big-haired girlfriend likes to do? System.out.println (Damao.getgirlfriend (). Gethobby ()); }} 

After reading this example, we found that although we are the gods, but the arrangement of such a script is going to continue to new girlfriend, this is not very troublesome?

The actual function of our God is to take charge of this big Mao's fate, at the right time, let the big Mao to talk about a love can, as to let who act as a big-haired girlfriend, as the gods, we do not care, right?

Or, anyway, it's our decision, it doesn't matter who it is.

So, we just need to write the script of the long-haired love, we just need to know that Mao has a girlfriend is OK, who, in the future. Because we arranged the script for him before the Da Mao was born. Is that why we never assign a girlfriend to Da Mao? Of course not, we tenjin generally like to put things that might change in the "sacred Grass paper", this manuscript is an XML file.

Next, let's introduce the spring's integrated environment to simulate this thing!

First, introduce the jar packages required by spring.

The second step is to write the configuration file.

<?xml version= "1.0" encoding= "UTF-8"?><Beansxmlns="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.xsd" ><BeanId="Damao"class="Com.spring.bean.DaMao" ><PropertyName= "girlfriend" ref= "CuiHua" ></property> </ bean> <bean id=  "Cuihua" class= " Com.spring.bean.CuiHua "> <property  Name= "hobby" value= "King Glory" ></property> </ bean> </BEANS>       

Then the script becomes this:

Package com.spring.test;Import Org.springframework.context.ApplicationContext;import Org.springframework.context.support.ClassPathXmlApplicationContext; import Com.spring.bean.DaMao; public class TestDaoMao2 { public static void Main (string[] args) { ApplicationContext Contex t = new Classpathxmlapplicationcontext ("Girlfriends.xml"); Damao Damao = (Damao) Context.getbean ("Damao"); //See what big-haired girlfriend likes to do? System.out. println (Damao.getgirlfriend (). Gethobby ());}}            

Operating effect:

The benefits are obvious, and we don't need to specify who the girlfriend is in the script (Java code), which we all write on "Sacred Grass manuscript". That's my brother. The IOC principle is that in the case of not changing the script, it is up to you to designate who will act as the "God", which is embodied in an XML file. And these configurations can be changed. The script is Java code, can not change without change.

Homework:
Create a new Java project SpringTest01, creating the package Com.spring.bean.
Java classes that need to be created:
Hero. java
Property:
Private String Heroname;
Priate String type;
Private String description;
Generate the corresponding get, set method, and then rewrite the ToString method to your liking.

Create a new source folder etc, specifically for storing configuration files. Write the XML file--heropool.xml of the hero pool.
In the configuration to add a hero's specific information: Luban seventh, Archer, hehe, successfully detected the opposite IQ, it seems I can not play all the strength.

Create Package Com.spring.test, new test class Testhero, read Heropool.xml, and print out the specific information of Luban seventh.

"Java framework projects from the beginning to the loading force" section I-the frenzied commentary of the spring framework IOC

Related Article

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.