Spring IOC快速入門——(一)

Spring IOC快速入門

Spring IOC底層實現原理

在這裏插入圖片描述

Spring官方下載地址:

https://repo.spring.io/libs-release-local/org/springframework/spring/

目錄結構:

docs:api文檔和開發規範
libs:開發需要的jar包和源碼
schema:開發需要的schema文件
在這裏插入圖片描述

新建項目:

在這裏插入圖片描述

在這裏插入圖片描述項目結構:

沒有resources解決方法:

在這裏插入圖片描述
在這裏插入圖片描述

ok

項目測試:

在這裏插入圖片描述

在pom.xml中導入依賴文件

在這裏插入圖片描述

copy代碼:

 <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>RELEASE</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>

新建Java文件

在這裏插入圖片描述

Java文件變成源代碼文件

右鍵java文件

在這裏插入圖片描述

測試傳統開發方式和Spring反轉控制和依賴注入

在resources下新建xml配置文件

在這裏插入圖片描述

copy代碼:
<?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="com.imooc.ioc.demo1.UserServiceImpl">
                <!--設置屬性-->
                <property name="name" value="食品名稱"/>
                <property name="taste" value="食品口味"/>
                <property name="kind" value="食品種類"/>
        </bean>

</beans>

1.新建包

2.新建接口UserService

public interface UserService {
    public void sayHello();
}

3.新建類UserServiceImpl

public class UserServiceImpl implements UserService {
    private String name;
    private String taste;
    private String kind;

    public  void sayHello(){
        System.out.println("hello spring");
        System.out.println(name+kind+taste);

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTaste() {
        return taste;
    }

    public void setTaste(String taste) {
        this.taste = taste;
    }

    public String getKind() {
        return kind;
    }

    public void setKind(String kind) {
        this.kind = kind;
    }
}

4.新建測試類SpringDemo1

public class SpringDemo1 {
   @Test
    /**
     *傳統方式開發
     */
    public void demo1(){
        //UserService userService = new UserServiceImpl();
       UserServiceImpl userService = new UserServiceImpl();
        //設置屬性
       userService.setName("張三");
       userService.sayHello();
    }
    @Test
    /**
     *Spring方式實現
     */
    public void demo2(){
        //創建Spring的工廠
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //通過工廠獲得類
        UserService userService =(UserService) applicationContext.getBean("userService");

        userService.sayHello();
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章