Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spring ioc理解 #1

Open
zhaozhao15 opened this issue Apr 25, 2019 · 0 comments
Open

spring ioc理解 #1

zhaozhao15 opened this issue Apr 25, 2019 · 0 comments
Labels

Comments

@zhaozhao15
Copy link
Owner

Spring IOC的理解

IOC(控制反转:Inverse Of Control),又叫依赖注入,是OOP中一种解决耦合问题的方法,也是Spring框架的核心。

这是一个 张三 类,一个方法是测试。

public class ZhangSan {
    public void test(){
	    System.out.println("张三-测试程序");
    }
}

public class JavaWork {
    public void doTest(){
	    Zhangsan zhangsan = new Zhangsan();
	    zhangsan.test();
    }
}

public class BossTest {
    public static void main(String[] args) {
	    JavaWork javaWork=new JavaWork();
	    javaWork.doTest();
    }
}

这个时候就是耦合的,因为Javawork类依赖Zhangsan类,Test类依赖JavaWork类,假如我不要张三测试要李四测试,代码的改动太大很麻烦。因此所以我们把控制谁去做测试的权力转交出来给BossTest。
我们去实现一个接口,然后让张三和李四继承这个接口。

public interface Tester {
    public void test();
}

public class ZhangSan implements Tester{
    public void test(){
	    System.out.println("张三-测试程序");
    }
}

public class Lisi implements Tester{
    public void test(){
	    System.out.println("李四-测试程序");
    }
}

我们再给JavaWork定义一个tester属性,决定谁去测试。

public class JavaWork {
    private Tester tester;
    
    public void setTester(Tester tester) {
	    this.tester = tester;
    }
    
    public void doTest(){
	    tester.test();
    }
}

在BossTest中决定这个人是谁。

public class BossTest {
    public static void main(String[] args) {
	    JavaWork javaWork=new JavaWork();
	    javaWork.setTester(new ZhangSan());
	    javaWork.doTest();
    }
}

现在控制权已经转交给了BossTest,控制权已经翻转。我们换成李四也很简单。
这里依然还是需要一个main方法来指定。
我们引入spring之后用spring管理IOC容器。

<?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">

    <bean id="zhangsan" class="com.java1234.service.ZhangSan"></bean>

    <bean id="lisi" class="com.java1234.service.Lisi"></bean>

    <bean id="javaWork" class="com.java1234.service.JavaWork">
	    <property name="tester" ref="lisi"></property>
    </bean>
    
</beans>

bean对应了张三和李四,另一个bean对应JavaWork,它的tester属性我们也可以通过spring设置,具体值我们用ref应用,它会自动调用JavaWork的set方法,把张三的实例对象设置进去。

public class BossTest2 {
    public static void main(String[] args) {
        //获取bean
	    ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
	    //设置bean
	    JavaWork javaWork=(JavaWork)ac.getBean("javaWork");
	    javaWork.doTest();
    }
}

如果这个时候我们再要去换成李四,我们只要改xml配置文件就可以,不需要改任何代码。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant