spring學習筆記之---IOC和DI

IOC和DI

(一)IOC

(1) 概念java

IOC (Inverse of Control) 反轉控制,就是將本來在程序中手動建立對象的控制權,交給spring框架管理。簡單的說,就是建立對象控制權被反轉到了spring框架spring

(2)實例app

1.UserService.java框架

package service;

public interface UserService {
    public void getAa();
}

 

2.UserServiceImpl.javaide

package service;



public class UserServiceImpl implements UserService{

    @Override

    public void getAa() {

        System.out.println("aaaaaaaaaaa");

    }

}

 

3.applicationContext.xmlthis

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

   <!--UserService將建立權交給spring-->

<bean id="userService" class="service.UserServiceImpl"></bean>

</beans>

 

4.UserTest.javaspa

package Test;



import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import service.UserService;

import service.UserServiceImpl;



public class UserTest {

    @Test

    public void one(){

        //建立spring工廠

        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");

        //經過工廠得到類

        UserService userService=(UserService)applicationContext.getBean("userService");

        //經過類調用方法

        userService.getAa();

    }
}

 

 

(二)DI

(1)概念code

DI(Dependency Injection)依賴注入,就是在spring建立這個對象的過程當中,將這個對象所依賴的屬性注入進去xml

(2)實例對象

1.UserService.java

package service;

public interface UserService {

    public void getAa();

}

 

2.UserServiceImpl.java

package service;



public class UserServiceImpl implements UserService{

    //添加屬性

    private String name;



    public String getName() {

        return name;

    }



    public void setName(String name) {

        this.name = name;

    }



    @Override

    public void getAa() {

        System.out.println("aaaaaaaaaaa"+name);

    }

}

 

3.applicationContext.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">

<!--UserService將建立權交給spring-->

<bean id="userService" class="service.UserServiceImpl">

    <!--設置屬性-->

    <property name="name" value="張三"/>

</bean>

</beans>

 

4.UserTest.java

package Test;



import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import service.UserService;

import service.UserServiceImpl;



public class UserTest {

    @Test

    public void one(){

        //建立spring工廠

        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");

        //經過工廠得到類

        UserService userService=(UserService)applicationContext.getBean("userService");

        //經過類調用方法

        userService.getAa();

    }



}
相關文章
相關標籤/搜索