69期-Java SE-056_Spring-1

56.mdjava

###企業級開發框架

- Spring Framework 是整個 Spring 生態的基礎,各個模塊都是基於 Spring Framework 衍生出來的。
- Spring Boot 是一個快速開發框架,讓開發者能夠快速搭建一套基於 Spring 的應用程序,集成了 Spring 其餘模塊以及第三方模塊,MyBatis、Hibernate 等,只需簡單的配置就能夠使用,開箱即用,默認支持 JSON 格式,實現先後端開發很是方便,Spring Boot + Vue。
- Spring Cloud 是一套整合了分佈式應用經常使用模塊的框架,基於 Spring Boot。



Maven:軟件管理工具

POM:Project Object Model,就是一個 XML 文件,配置 jar 依賴關係。



### Spring 應用程序

一、建立 Maven 工程。

二、pom.xml 中引入 Spring 依賴。

```xml
<dependencies>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.1.7.RELEASE</version>
  </dependency>
</dependencies>
```

Spring 的兩大核心機制:IoC(控制反轉) 和 AOP(面向切面編程)

IoC 對象建立再也不由開發者完成,而是容器自動建立,開發者直接取出來用便可。

三、建立 spring.xml

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

    <bean id="stu" class="com.southwind.entity.Student" >
        <property name="id" value="1"></property>
        <property name="name" value="張三"></property>
        <property name="age" value="22"></property>
    </bean>

</beans>
```

四、加載 IoC 容器,獲取建立好的 bean。

```java
public class IoCTest {
    public static void main(String[] args) {
        //1.加載 IoC 容器,spring.xml
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        Student student = (Student) applicationContext.getBean("stu");
        System.out.println(student);
    }
}
```

特殊字符的處理方式。

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

    <bean id="stu" class="com.southwind.entity.Student" >
        <property name="id" value="1"></property>
<!--        <property name="name" value="&lt;張三&gt;"></property>-->
        <property name="name">
            <value><![CDATA[<張三>]]></value>
        </property>
        <property name="age" value="22"></property>
    </bean>

</beans>
```



使用 IoC 容器建立對象,實體類的注意事項:

- 必須有無參構造函數。
- 成員變量必須有 setter 方法。

IoC 實例化對象的過程,經過反射+XML解析的方式對 spring.xml 進行處理,反射拿到無參構造函數,調用建立對象,同時獲取 setter 方法,調用完成成員變量的賦值。



### 獲取 IoC bean 的方式

- 經過 id 獲取

```java
Student student = (Student) applicationContext.getBean("stu");
```

- 經過運行時類獲取

```java
Student student = applicationContext.getBean(Student.class);
```

經過運行時類獲取 bean 存在一個弊端,當 spring.xml 中配置兩個 Student 的 bean 時會拋出異常。



### 建立 IoC bean 的方式

- 無參構造

```xml
<bean id="stu2" class="com.southwind.entity.Student">
  <property name="id" value="2"></property>
  <property name="name" value="李四"></property>
  <property name="age" value="23"></property>
</bean>
```

- 有參構造

一、在實體類中建立有參構造。

```java
public Student(Integer id, String name, Integer age) {
  this.id = id;
  this.name = name;
  this.age = age;
}
```

二、在 spring.xml 中進行配置。

```xml
<bean id="stu3" class="com.southwind.entity.Student">
  <constructor-arg index="1" value="王五"></constructor-arg>
  <constructor-arg index="0" value="1"></constructor-arg>
  <constructor-arg index="2" value="20"></constructor-arg>
</bean>
```



```xml
<bean id="stu3" class="com.southwind.entity.Student">
  <constructor-arg name="name" value="王五"></constructor-arg>
  <constructor-arg name="id" value="1"></constructor-arg>
  <constructor-arg name="age" value="20"></constructor-arg>
</bean>
```



```xml
<bean id="stu3" class="com.southwind.entity.Student">
  <constructor-arg value="1"></constructor-arg>
  <constructor-arg value="王五"></constructor-arg>
  <constructor-arg value="20"></constructor-arg>
</bean>
```



### 兩個 bean 的級聯

```java
package com.southwind.entity;

public class Student {
    private Integer id;
    private String name;
    private Integer age;
    private Classes classes;

    public Classes getClasses() {
        return classes;
    }

    public void setClasses(Classes classes) {
        this.classes = classes;
    }

    public void setId(Integer id) {
        this.id = id;
    }

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

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", classes=" + classes +
                '}';
    }

    public Student(Integer id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Student() {
    }
}
```



```java
package com.southwind.entity;

public class Classes {
    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Classes{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
```



```xml
<bean id="stu2" class="com.southwind.entity.Student">
  <property name="id" value="2"></property>
  <property name="name" value="李四"></property>
  <property name="age" value="23"></property>
  <property name="classes" ref="classes1"></property>
</bean>

<bean id="classes1" class="com.southwind.entity.Classes">
  <property name="id" value="1"></property>
  <property name="name" value="Java班"></property>
</bean>
```

 

 pom.xmlspring

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.southwind</groupId>
    <artifactId>56</artifactId>
    <version>1.0-SNAPSHOT</version>

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

</project>

 

 

 

 

 

 

 

 Classes.javaapache

package com.southwind.entity;

public class Classes {
    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Classes{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

 

Student.java編程

package com.southwind.entity;

public class Student {
    private Integer id;
    private String name;
    private Integer age;
    private Classes classes;

    public Classes getClasses() {
        return classes;
    }

    public void setClasses(Classes classes) {
        this.classes = classes;
    }

    public void setId(Integer id) {
        this.id = id;
    }

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

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", classes=" + classes +
                '}';
    }

    public Student(Integer id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Student() {
    }
}

 

 IoCTest.java後端

package com.southwind.test;

import com.southwind.entity.Classes;
import com.southwind.entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class IoCTest {
    public static void main(String[] args) {
        //1.加載 IoC 容器,spring.xml
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
//        Student student = (Student) applicationContext.getBean("stu");
//        Student student = applicationContext.getBean(Student.class);
//        System.out.println(student);
        Student student = (Student) applicationContext.getBean("stu2");
        System.out.println(student);
        Classes classes = (Classes) applicationContext.getBean("classes1");
        System.out.println(classes);
    }
}

 

Test.javaapp

package com.southwind.test;

import com.southwind.entity.Student;

public class Test {
    public static void main(String[] args) {
//        Student student = new Student();
//        student.setId(1);
//        student.setName("張三");
//        student.setAge(22);
//        System.out.println(student);
    }
}

 

 spring.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">

<!--    <bean id="stu" class="com.southwind.entity.Student" >-->
<!--        <property name="id" value="1"></property>-->
<!--&lt;!&ndash;        <property name="name" value="&lt;張三&gt;"></property>&ndash;&gt;-->
<!--        <property name="name">-->
<!--            <value><![CDATA[<張三>]]></value>-->
<!--        </property>-->
<!--        <property name="age" value="22"></property>-->
<!--    </bean>-->

    <bean id="stu2" class="com.southwind.entity.Student">
        <property name="id" value="2"></property>
        <property name="name" value="李四"></property>
        <property name="age" value="23"></property>
        <property name="classes" ref="classes1"></property>
    </bean>

<!--    <bean id="stu3" class="com.southwind.entity.Student">-->
<!--        <constructor-arg index="1" value="王五"></constructor-arg>-->
<!--        <constructor-arg index="0" value="1"></constructor-arg>-->
<!--        <constructor-arg index="2" value="20"></constructor-arg>-->
<!--    </bean>-->

    <bean id="classes1" class="com.southwind.entity.Classes">
        <property name="id" value="1"></property>
        <property name="name" value="Java班"></property>
    </bean>

</beans>

相關文章
相關標籤/搜索