Spring the next day: The concept of Ioc,di, using interface with the DJ to program __ programming

Source: Internet
Author: User
Tags lowercase
summarize the previous lesson Spring is actually a container framework that configures a variety of beans and maintains the relationship between beans and beans, and when a bean is needed, you can get the Bean with Getbean (ID)

Spring Core Reference Package

Spring's core file Applicationcontext.xml, this file can take other names

Configure the Bean, created by ID, and class file attributes

* Use ApplicationContext AC = new
Classpathxmlapplicationcontext ("Applicationcontext.xml"); loading container *

Ioc:inverse of Controll: Control reversal, transfer the right to create objects (beans) and maintenance objects (beans) from the program to the Spring container (applicationcontext.xml)

Di:dependency Injection Dependency Injection: Spring's core technology sping Development Advocates interface programming, with DI interface programming, reduce the coupling between layer and layer case: letter Capitalization conversion Create an interface Changeletter use two classes to implement this interface to configure objects to use in the spring container
Interface Changeletter

Package Com.study.inter Public
Interface changeletter{public
    String Change ();
}

Implement class Upperletter, lowercase to uppercase

Package com.service;

/**
 * Created by Hejian on 16/9/7.
 * * Public
class Upperletter implements changeletter{
    private String str;

    Public String Change () {
        //convert lowercase to uppercase
        str.touppercase ();
        System.out.println (Getstr ());
        return Str.touppercase ();
    }

    Public String Getstr () {return
        str.touppercase ();
    }

    public void Setstr (String str) {
        this.str = str;
    }
}

Implement class Lowerletter, uppercase to lowercase

Package com.service;

/**
 * Created by Hejian on 16/9/7.
 * * Public
class Lowerletter implements Changeletter {
    private String str;

    Public String Change () {
        //uppercase-> lowercase
        System.out.println (str.tolowercase ());
        return Str.tolowercase ();
    }

    public void Setstr (String str) {
        this.str = str;
    }

    Public String Getstr () {return
        str;
    }
}

The XML configuration file is as follows, you can create a beans.xml file below the corresponding package, such as: Com.service Create Beans.xml

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns=
"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 ">
    < !--ID can not be the same, in order to demonstrate the uncoupling, so write consistent, commented out, switch in the open comment off another, so you can pass the bean to select the corresponding logical operation-->
    <bean id = "Changelette" class= " Com.service.LowerLetter ">
        <property name=" str "value=" abced "/>
    </bean>

    <!--< Bean id = "Changelette" class= "Com.service.UpperLetter" >-->
        <!--<property name= "str" value= "ABCD"/ >-->
    <!--</bean>-->
</beans>

To create a test class

Package com.service;

Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Hejian on 16/9/8.
 *
/public class Testspring {public
    static void Main (string[] args) {
        ApplicationContext ApplicationContext = new Classpathxmlapplicationcontext ("Com/service/beans.xml");
        Lowerletter Changeletter = (lowerletter) applicationcontext.getbean ("Changelette");
       Changeletter.change ();

        Using an interface to access the bean, the interface can be turned, where you can decouple the operation and switch Changelette by switching the bean in the Beans.xml file without changing the code in the corresponding class
        Changeletter ChangeLetter1 = (changeletter) applicationcontext.getbean ("Changelette");
        Changeletter1.change ();
    }

Through the above cases, we can understand that Di with interface programming convenience, can reduce the coupling between programs

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.