Java教程

Java练习(七):Spring应用之DI (简单的StudentDI工程)

本文主要是介绍Java练习(七):Spring应用之DI (简单的StudentDI工程),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

目录

1. 背景概念

1.1 DI定义

1.2 常见注入方式的xml配置

2. 简单的StudentDI工程,展示了常见的注入方式的使用(配置)

2.1 maven系统的配置文件:pom.xml

2.2 bean的配置文件: bean_di.xml

2.3 类代码:StudentDI.java , Address.java (该类被StudentDI引用)

2.4 测试类

补充:Eclipse中,一个快速生成getter和setter方法的小技巧


1. 背景概念

1.1 DI定义

(1) IoC的一个重点是:在系统运行中,动态的向某个对象提供它所需要的其他对象。这一点是通过DI来实现的。

(2)DI, 依赖注入:用一个单独的对象(装配器)来装配对象之间的依赖关系。DI,即组件之间的依赖关系由容器在运行期间决定,即由容器动态的将某个依赖关系注入到组件中。

(3)举例:

对象A要操作数据库,以前我们要在A中自己编写代码来获得一个Connection对象,有了Spring后我们就只需要告诉Spring, A中需要一个Connection. 至于怎么构造这个connection?何时构造?A不需要知道。这样就完成了对各个对象之间关系的控制。

(4)DI如何实现?通过反射。它允许程序在运行的时候动态的生成对象,执行对象的方法,改变对象的属性,Spring就是通过反射来实现注入的。

1.2 常见注入方式的xml配置

bean注入

数组注入

List注入

Map注入

set注入

null注入

Properties注入

2. 简单的StudentDI工程,展示了常见的注入方式的使用(配置)

2.1 maven系统的配置文件:pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.my.test</groupId>
  <artifactId>SpringDIDemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
   <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.3</version>
        </dependency>        
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>

    </dependencies>
  
</project>

2.2 bean的配置文件: bean_di.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"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">


    <context:annotation-config />
    
    <bean id="addr" class="com.my.test.Address">
        <property name="address" value="上海" />
    </bean>

    <bean id="student1" class="com.my.test.StudentDI">
        <property name="name" value="Sheryl" />
        <property name="address" ref="addr" />
        <property name="books">
            <array>
                <value>数学</value>
                <value>语文</value>
                <value>英语</value>
            </array>
        </property>
        <property name="hobbys">
            <list>
                <value>听歌</value>
                <value>看电影</value>
                <value>逛街</value>
            </list>
        </property>
        <property name="card">
            <map>
                <entry key="招行" value="123456789" />
                <entry key="工行" value="987654321" />
            </map>
        </property>

        <property name="games">
            <set>
                <value>CS</value>
                <value>斗地主</value>
                <value>高尔夫</value>
            </set>
        </property>
        <property name="wife">
            <null />
        </property>
        <property name="info">
            <props>
                <prop key="学号">12345678</prop>
                <prop key="性别">女</prop>
                <prop key="姓名">Sheryl</prop>
            </props>
        </property>

    </bean>
</beans>
 

2.3 类代码:StudentDI.java , Address.java (该类被StudentDI引用)

(1)StudentDI.java

package com.my.test;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import com.my.test.Address;

public class StudentDI {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;

    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    public String[] getBooks() {
        return books;
    }
    public void setBooks(String[] books) {
        this.books = books;
    }
    public List<String> getHobbys() {
        return hobbys;
    }
    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;
    }
    public Map<String, String> getCard() {
        return card;
    }
    public void setCard(Map<String, String> card) {
        this.card = card;
    }
    public Set<String> getGames() {
        return games;
    }
    public void setGames(Set<String> games) {
        this.games = games;
    }
    public String getWife() {
        return wife;
    }
    public void setWife(String wife) {
        this.wife = wife;
    }
    public Properties getInfo() {
        return info;
    }
    public void setInfo(Properties info) {
        this.info = info;
    }
    
    public void showStudentInfo(){
        System.out.println("name="+ name+ ",address="+ address.getAddress()+ ",books=");
        for (String book:books){
            System.out.print("<<"+book+">>\t");
        }
        System.out.println("\nhobbys:"+hobbys);
        System.out.println("card:"+card);
        System.out.println("games:"+games);
        System.out.println("wife:"+wife);
        System.out.println("info:"+info);
    }   

}
 

(2)Address.java

package com.my.test;

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }

}

2.4 测试类

package com.my.test.demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.my.test.StudentDI;

public class StudentDITest {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean_di.xml");
        StudentDI studentDI = (StudentDI) context.getBean("student1");

        studentDI.showStudentInfo();
    }

}
 

输出结果:

name=Sheryl,address=上海,books=
<<数学>>    <<语文>>    <<英语>>    
hobbys:[听歌, 看电影, 逛街]
card:{招行=123456789, 工行=987654321}
games:[CS, 斗地主, 高尔夫]
wife:null
info:{学号=12345678, 性别=女, 姓名=Sheryl}

补充:Eclipse中,一个快速生成getter和setter方法的小技巧

 

 

这篇关于Java练习(七):Spring应用之DI (简单的StudentDI工程)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!