SSM-Spring-04:Spring的DI的构造注入,P命名注入,和集合注入

简介:   ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------       DI和IOC相比,DI更偏向于实现   DI的set方式注入在前面入门案例里有写,所以此处不多啰嗦,直接开搞,先说构造注入和P命名注入     构造方式,理所当然要有带参构...

 

 

------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

 

 

 

DI和IOC相比,DI更偏向于实现

 

DI的set方式注入在前面入门案例里有写,所以此处不多啰嗦,直接开搞,先说构造注入和P命名注入

    构造方式,理所当然要有带参构造,这儿值得注意的是,你最好再补全一个无参构造,因为你写了带参构造,系统就不再会为你默认补全一个无参构造了,当你在不经意或者不知情的情况下被调用了,就会报错

    P命名则有注意的是那个头文件 xmlns  xsi需要你去配置一道,我下面有,你直接copy就可以

 

    在实体类中(有俩个实体类,我做了关联关系)

 

package cn.dawn.day05diup;

/**
 * Created by Dawn on 2018/3/5.
 */
public class Car {
    private String color;
    private String type;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}







package cn.dawn.day05diup;

/**
 * Created by Dawn on 2018/3/5.
 */
//student类
public class Student {
    private String name;
    private Integer age;
    private Car car;

    //带参构造
    public Student(String name, Integer age, Car car) {
        this.name = name;
        this.age = age;
        this.car = car;
    }

    //无参构造
    public Student() {
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }
}

 

    在大配置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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">

    <bean id="car" class="cn.dawn.day05diup.Car">
        <property name="color" value="黑色"></property>
        <property name="type" value="奥迪"></property>
    </bean>
    <!--di构造注入-->
    <!--<bean id="student" class="cn.dawn.day05diup.Student">
        <constructor-arg index="0" value="孟六"></constructor-arg>
        <constructor-arg index="1" value="20"></constructor-arg>
        <constructor-arg index="2" ref="car"></constructor-arg>
    </bean>-->

    <!--p命名注入-->
    <bean id="student" class="cn.dawn.day05diup.Student" p:name="孟小六" p:age="8" p:car-ref="car"></bean>

</beans>

 

    没有什么好讲的,带参构造的注入方法,index索引从0开始,对应的是那个带参构造的index

    单测方法:

 

    @Test
    /*diP命名注入*/
    public void t02(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day05diup.xml");
        Student student = (Student) context.getBean("student");
        System.out.println("学生"+student.getName()+"开着"+student.getCar().getColor()+""+student.getCar().getType());
    }



    @Test
    /*di构造注入*/
    public void t01(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day05diup.xml");
        Student student = (Student) context.getBean("student");
        System.out.println("学生"+student.getName()+"开着"+student.getCar().getColor()+""+student.getCar().getType());
    }

 

 

集合注入:

    数组,List,Set,Map,Properties

  实体类

 

package cn.dawn.day05diup;

import java.util.*;

/**
 * Created by Dawn on 2018/3/5.
 */
public class MyCollection {
    private String[] array;
    private List<String> list;
    private Set<String> set;
    private Map<String,String> map;
    private Properties properties;

    @Override
    public String toString() {
        return "MyCollection{" +
                "array=" + Arrays.toString(array) +
                ", list=" + list +
                ", set=" + set +
                ", map=" + map +
                ", properties=" + properties +
                '}';
    }

    public String[] getArray() {
        return array;
    }

    public void setArray(String[] array) {
        this.array = array;
    }

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public Set<String> getSet() {
        return set;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}

 

  大配置中的bean节点

 

<!--di的集合注入-->
    <bean id="mycollection" class="cn.dawn.day05diup.MyCollection">
        <!--数组注入-->
        <property name="array">
            <array>
                <value>孟六</value>
                <value>孟六十六</value>
                <value>孟六百六十六</value>
            </array>
        </property>
        <!--list集合注入-->
        <property name="list">
            <list>
                <value>奥迪</value>
                <value>奥小迪</value>
                <value>奥迪迪</value>
            </list>
        </property>
        <!--set集合注入-->
        <property name="set">
            <set>
                <value>set1</value>
                <value>set2</value>
                <value>set3</value>
            </set>
        </property>
        <!--map集合注入-->
        <property name="map">
            <map>
                <entry key="姓名">
                    <value>孟五</value>
                </entry>
                <entry key="年龄">
                    <value>555</value>
                </entry>
            </map>
        </property>
        <!--properties-->
        <property name="properties">
            <props>
                <prop key="key1">v1</prop>
                <prop key="key2">v2</prop>
                <prop key="key3">v3</prop>
            </props>
        </property>
    </bean>

 

    单测

 

    @Test
    /*di集合注入*/
    public void t03(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day05diup.xml");
        MyCollection mycollection = (MyCollection) context.getBean("mycollection");
        System.out.println(mycollection);
    }

 

    由于重写了toString,可以直接一览无余

 

 

 

 

 

 

 

目录
相关文章
|
Java Spring
在使用Spring的`@Value`注解注入属性值时,有一些特殊字符需要注意
【10月更文挑战第9天】在使用Spring的`@Value`注解注入属性值时,需注意一些特殊字符的正确处理方法,包括空格、引号、反斜杠、新行、制表符、逗号、大括号、$、百分号及其他特殊字符。通过适当包裹或转义,确保这些字符能被正确解析和注入。
732 3
|
Java 测试技术 程序员
为什么Spring不推荐@Autowired用于字段注入?
作为Java程序员,Spring框架在日常开发中使用频繁,其依赖注入机制带来了极大的便利。然而,尽管@Autowired注解简化了依赖注入,Spring官方却不推荐在字段上使用它。本文将探讨字段注入的现状及其存在的问题,如难以进行单元测试、违反单一职责原则及易引发NPE等,并介绍为何Spring推荐构造器注入,包括增强代码可读性和维护性、方便单元测试以及避免NPE等问题。通过示例代码展示如何将字段注入重构为构造器注入,提高代码质量。
380 1
|
8月前
|
Java 容器 Spring
什么是Spring IOC 和DI ?
IOC : 控制翻转 , 它把传统上由程序代码直接操控的对象的调用权交给容 器,通过容器来实现对象组件的装配和管理。所谓的“控制反转”概念就是对组件对象控制权的转 移,从程序代码本身转移到了外部容器。 DI : 依赖注入,在我们创建对象的过程中,把对象依赖的属性注入到我们的类中。
|
8月前
|
前端开发 Java 数据库连接
Spring MVC 扩展和SSM框架整合
通过以上步骤,我们可以将Spring MVC扩展并整合到SSM框架中。这个过程包括配置Spring MVC和Spring的核心配置文件,创建控制器、服务层和MyBatis的Mapper接口及映射文件。在实际开发中,可以根据具体业务需求进行进一步的扩展和优化,以构建更加灵活和高效的企业级应用程序。
197 5
|
11月前
|
Java Spring
一键注入 Spring 成员变量,顺序编程
介绍了一款针对Spring框架开发的插件,旨在解决开发中频繁滚动查找成员变量注入位置的问题。通过一键操作(如Ctrl+1),该插件可自动在类顶部添加`@Autowired`注解及其成员变量声明,同时保持光标位置不变,有效提升开发效率和代码编写流畅度。适用于IntelliJ IDEA 2023及以上版本。
155 2
一键注入 Spring 成员变量,顺序编程
|
11月前
|
存储 Java 应用服务中间件
【Spring】IoC和DI,控制反转,Bean对象的获取方式
IoC,DI,控制反转容器,Bean的基本常识,类注解@Controller,获取Bean对象的常用三种方式
353 12
|
存储 开发框架 Java
什么是Spring?什么是IOC?什么是DI?IOC和DI的关系? —— 零基础可无压力学习,带源码
文章详细介绍了Spring、IOC、DI的概念和关系,解释了控制反转(IOC)和依赖注入(DI)的原理,并提供了IOC的代码示例,阐述了Spring框架作为IOC容器的应用。
830 1
什么是Spring?什么是IOC?什么是DI?IOC和DI的关系? —— 零基础可无压力学习,带源码
|
前端开发 Java Spring
【Spring】“请求“ 之后端传参重命名,传递数组、集合,@PathVariable,@RequestPart
【Spring】“请求“ 之后端传参重命名,传递数组、集合,@PathVariable,@RequestPart
297 2
|
缓存 Java Spring
源码解读:Spring如何解决构造器注入的循环依赖?
本文详细探讨了Spring框架中的循环依赖问题,包括构造器注入和字段注入两种情况,并重点分析了构造器注入循环依赖的解决方案。文章通过具体示例展示了循环依赖的错误信息及常见场景,提出了三种解决方法:重构代码、使用字段依赖注入以及使用`@Lazy`注解。其中,`@Lazy`注解通过延迟初始化和动态代理机制有效解决了循环依赖问题。作者建议优先使用`@Lazy`注解,并提供了详细的源码解析和调试截图,帮助读者深入理解其实现机制。
747 1
|
存储 Java 程序员
SpringIOC和DI的代码实现,Spring如何存取对象?@Controller、@Service、@Repository、@Component、@Configuration、@Bean DI详解
本文详细讲解了Spring框架中IOC容器如何存储和取出Bean对象,包括五大类注解(@Controller、@Service、@Repository、@Component、@Configuration)和方法注解@Bean的用法,以及DI(依赖注入)的三种注入方式:属性注入、构造方法注入和Setter注入,并分析了它们的优缺点。
204 0
SpringIOC和DI的代码实现,Spring如何存取对象?@Controller、@Service、@Repository、@Component、@Configuration、@Bean DI详解

热门文章

最新文章