IOC & di

Source: Internet
Author: User

What is IOC?

A: IOC (inverse of controll) controls inversion: the so-called control inversion refers to creating an object (bean) and maintaining an object (bean) from the program to the spring container (applicationcontext. XML), and the program itself is not maintained.

What is Di?

A: di (dependency injection) Dependency injection: In fact Di and IOC are the same concept. Spring designers believe that Di represents spring's core technologies more accurately.

 

Get Bean from applicationcontex application context container and get Bean from Bean Factory Container

1. Bean class

package com.llp.ioc;public class Student {String name;public Student(){System.out.println("Student coming");}public String getName() {return name;}public void setName(String name) {this.name = name;}}

2. Configure the Bean class

<? XML version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: context = "http://www.springframework.org/schema/context" xmlns: Tx = "http://www.springframework.org/schema/tx" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id = "student" class = "com. LLP. IOC. student "> <property name =" name "value =" "/> </bean> </beans>

3. Application

Package COM. LLP. IOC; import Org. springframework. beans. factory. beanfactory; import Org. springframework. beans. factory. XML. xmlbeanfactory; import Org. springframework. context. applicationcontext; import Org. springframework. context. support. classpathxmlapplicationcontext; import Org. springframework. core. io. classpathresource; public class app1 {public static void main (string [] ARGs) {// retrieve bean from applicationcontext // applicationcontext AC = new classpathxmlapplicationcontext ("com/LLP/IOC/beans. XML "); // when we instantiate beans. XML, the bean configured in this file is instance (the bean scope is Singleton) // retrieves student from bean // if we use beanfactory to get Bean, when you just instantiate the container, // The bean of the container is not instantiated. Only when you use a bean of getbean, beanfactory factory = new xmlbeanfactory (New classpathresource ("com/LLP/IOC/beans. XML "); factory. getbean ("student ");}}

 

Conclusion:

1. If applicationcontext is used, the configured bean will be instantiated if it is singlton. (The advantage is that it can be loaded in advance, but the disadvantage is that it consumes memory)

2. If it is beanfactory, the configured bean will not be instantiated immediately when you get beanfacotry. It will only be instance when you use it (the advantage is memory saving, but the disadvantage is speed)

3. Regulations: Generally, applicatiocontext should be used to complete (90%)

 

Spring Development Advocates interface programming, and uses di technology to decouple layers.

Now let's try spring's Di with interface programming to complete a case of case-insensitive conversion:

Ideas:

1. Create an interface changeletter

Package com. LLP. Inter; public interface changeletter {// declare a method Public String change ();}

2. Two class implementation Interfaces

Convert uppercase letters to lowercase letters.

package com.llp.inter;public class LowwerLetter implements ChangeLetter {private String str;public String getStr() {return str;}public void setStr(String str) {this.str = str;}@Overridepublic String change() {// TODO Auto-generated method stubreturn str.toLowerCase();}}

Convert lowercase letters to uppercase letters:

Package COM. LLP. inter; public class upperletter implements changeletter {private string STR; Public String getstr () {return STR;} public void setstr (string Str) {This. STR = STR;} Public String change () {// upper-case letters-> return Str. touppercase ();}}

 

3. Configure the object to the spring container.

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><bean id="changeLette" class="com.llp.inter.UpperLetter"><property name="str"><value>abcef</value></property>    </bean><!-- <bean id=""changeLette" class="com.llp.inter.LowwerLetter"><property name="str"><value name="str" value="ABRTY" /></property>    </bean> -->    </beans>

4. Use

Package COM. LLP. inter; import Org. springframework. context. applicationcontext; import Org. springframework. context. support. classpathxmlapplicationcontext; public class app1 {public static void main (string [] ARGs) {applicationcontext AC = new classpathxmlapplicationcontext ("com/LLP/inter/beans. XML "); // get, no interface // upperletter = (upperletter) AC. getbean ("changelette"); // string STR = upperletter. change (); // system. out. println (STR); // use the interface to access beanchangeletter changeletter = (changeletter) AC. getbean ("changelette"); system. out. println (changeletter. change ());}}

 

 

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.