Spring学习次日:IOC,DI

IOC:inverse of control 控制反转 编程

DI:Dependency inject 依赖注入app

解释:高层模块(控制层)不该该依赖你低层模块,二者都应该依赖其抽象。server不须要知道具体的实现,只须要知道接口(面向接口编程)。能够经过配置文件,知道具体的实现。spa

示例代码:code

接口代码:server

 

package test04.dao;

public interface IUserDao {
    public void sayHello();
}

 

dao代码:xml

 

package test04.dao;


public class UserDao implements IUserDao {

    public void sayHello() {
        System.out.println("hello---------------");
    }
}

 

xml代码:blog

 

<bean id="userdao" class="test04.dao.UserDao"></bean>
<bean id="service" class="test04.service.UserService">
     <property name="iUserDao" ref="userdao"></property>
</bean>

 

Main代码:接口

 

public class Main {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classBeanMVC.xml");
        UserService userService = (UserService) applicationContext.getBean("service");
        userService.test();
    }
}
相关文章
相关标签/搜索