Spring學習(一)idea中創建第一個Spring項目


1、前言

Spring框架是一個開放源代碼的J2EE應用程序框架,由Rod Johnson發起,是針對bean的生命周期進行管理的輕量級容器(lightweight container)。 Spring解決了開發者在J2EE開發中遇到的許多常見的問題,提供了功能強大IOC、AOP及Web MVC等功能。Spring可以單獨應用於構築應用程序,也可以和Struts、Webwork、Tapestry等眾多Web框架組合使用,並且可以與 Swing等桌面應用程序AP組合。因此, Spring不僅僅能應用於JEE應用程序之中,也可以應用於桌面應用程序以及小應用程序之中。Spring框架主要由七部分組成,分別是 Spring Core、 Spring AOP、 Spring ORM、 Spring DAO、Spring Context、 Spring Web和 Spring Web MVC。

2、創建Spring項目之前的准備

1、Maven

通過Maven項目管理工具、我們能在idea中更快的創建一個Spring項目、相關的jar包只需在pom.xml中添加依賴即可全部導入。

2、idea2019.6商業版

相關下載不過多贅述。

3、新建一個簡單的Spring項目

pom.xml添加依賴
默認是沒有spring相關依賴的、我們需要修改pom.xml、讓idea 導入相關的依賴。

      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.9.RELEASE</version>
      </dependency>

在pom.xml中加入這么一段話、所有spring需要的依賴即導入進來了、可以在下面看到。注意idea提示的時候點擊import changes

4、進行IOC測試

IOC是什么呢?控制反轉(Inversion of Control,縮寫為IoC),是面向對象編程中的一種設計原則,可以用來減低計算機代碼之間的耦合度。其中最常見的方式叫做依賴注入(Dependency Injection,簡稱DI),還有一種方式叫“依賴查找”(Dependency Lookup)。通過控制反轉,對象在被創建的時候,由一個調控系統內所有對象的外界實體將其所依賴的對象的引用傳遞給它。也可以說,依賴被注入到對象中。Spring框架的核心就是IOC、通過IOC原則、代碼之間的耦合性大大地降低、許多繁雜的對象創建被省去、效率大大提高。

1、新建一個實體類Student

package com.feng.entity;

import com.feng.factory.CourseFactory;
import com.feng.newinstance.HtmlCourse;
import com.feng.newinstance.ICourse;
import com.feng.newinstance.JavaCourse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Student {
    private int stuNo;
    private String stuName;
    private int stuAge;

    public int getStuNo() {
        return stuNo;
    }

    public void setStuNo(int stuNo) {
        this.stuNo = stuNo;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public int getStuAge() {
        return stuAge;
    }

    public void setStuAge(int stuAge) {
        this.stuAge = stuAge;
    }

    @Override
    public String toString() {
        return "Student{" +
                "stuNo=" + stuNo +
                ", stuName='" + stuName + '\'' +
                ", stuAge=" + stuAge +
                '}';
    }
}

2、在applicationContext.xml中配置bean

<?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">
    <!--  該spring中產生的所有對象、被spring放入spring IOC容器中  -->
    <!-- id:唯一標識符、class:標識符的類型 -->
    <bean id="student" class="com.feng.entity.Student">
        <!-- property: 該class所代表的屬性
             name:  屬性名
             value: 屬性值
        -->
        <property name="stuNo" value="2"></property>
        <property name="stuName" value="ls"></property>
        <property name="stuAge" value="20"></property>
    </bean>
</beans>

3、在測試類中進行測試

package com.feng.test;
import com.feng.entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    public static void main(String[] args) {
        //Spring 上下文對象:context
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 執行從springIOC容器中獲取一個id為student的對象
        Student student =(Student) context.getBean("student");
        System.out.println(student);
    }
}

輸出如下:

5、總結

可以發現、SpringIOC容器幫助我們更加輕松的獲取對象的屬性和值、在這之前、我們只需要在application.xml中配置即可、從原來的new對象、變成現在從IOC容器中直接去取對象、這就是IOC的本質、也有一種說法叫做DI原則即依賴注入(Dependency Injection,簡稱DI)。即將對象注入到bean中、在從bean中去取就可以了。如果項目很大、這種方式免去了對象多次的賦值、減少代碼耦合性、大大地提高了開發效率。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM