码迷,mamicode.com
首页 > 编程语言 > 详细

Spring5.X常见的注入方式

时间:2020-07-15 01:22:58      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:pojo   center   tor   rgs   odi   getch   imp   dom   VID   

使用set方法注入

Video.java

package net.cybclass.sp.domain;

public class Video {
    private int id;
    private String title;

    public int getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        System.out.println("Video setTitle被调用");
        this.title = title;
    }
}

applicationContext.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="video" class="net.cybclass.sp.domain.Video">
        <property name="id" value="9"></property>
        <property name="title" value="Spring5.X课程"></property>

    </bean>
</beans>

app.java

package net.cybclass.sp;

import net.cybclass.sp.domain.Video;
import net.cybclass.sp.domain.VideoOrder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class app {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        Video video=(Video) applicationContext.getBean("video");
        System.out.println(video);
    }
}

验证

技术图片

使用带参的构造函数注入

Video.java

package net.cybclass.sp.domain;

public class Video {
    public Video(int id, String title) {
        System.out.println("Video 构造函数被调用");
        this.id = id;
        this.title = title;
    }

    private int id;
    private String title;

    public int getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

applicationContext.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="video" class="net.cybclass.sp.domain.Video">
        <constructor-arg name="id" value="8"></constructor-arg>
        <constructor-arg name="title" value="Spring5.X课程"></constructor-arg>
    </bean>
</beans>

app.java

package net.cybclass.sp;

import net.cybclass.sp.domain.Video;
import net.cybclass.sp.domain.VideoOrder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class app {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        Video video=(Video) applicationContext.getBean("video");
        System.out.println(video);
    }
}

验证

技术图片

POJO类型注入(property没有使用value属性,而是使用了ref属性)

    <bean id="video" class="net.cybclass.sp.domain.Video">
       <!--list 类型注入-->
        <property name="chapterList">
            <list>
                <value>第一章SpringBoot</value>
                <value>第二章Mybatis</value>
                <value>第三章Spring</value>
            </list>
        </property>
        <!--注入map-->
        <property name="videoMap">
            <map>
                <entry key="1" value="SpringCloud课程层"></entry>
                <entry key="2" value="面试宝典"></entry>
                <entry key="3" value="JavaWeb课程"></entry>
            </map>
        </property>
    </bean>
    <bean id="videoOrder" class="net.cybclass.sp.domain.VideoOrder">
        <property name="id" value="8"></property>
        <property name="outTradeNo" value="12312"></property>
        <property name="video" ref="video"></property>
    </bean>

List和Map注入

Video.java

package net.cybclass.sp.domain;

import java.util.List;
import java.util.Map;

public class Video {
    private int id;
    private String title;
    private List<String> chapterList;
    private Map<Integer, String> videoMap;

    public List<String> getChapterList() {
        return chapterList;
    }

    public void setChapterList(List<String> chapterList) {
        this.chapterList = chapterList;
    }

    public Map<Integer, String> getVideoMap() {
        return videoMap;
    }

    public void setVideoMap(Map<Integer, String> videoMap) {
        this.videoMap = videoMap;
    }

    public int getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

applicationContext.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="video" class="net.cybclass.sp.domain.Video">
       <!--list 类型注入-->
        <property name="chapterList">
            <list>
                <value>第一章SpringBoot</value>
                <value>第二章Mybatis</value>
                <value>第三章Spring</value>
            </list>
        </property>
        <!--注入map-->
        <property name="videoMap">
            <map>
                <entry key="1" value="SpringCloud课程层"></entry>
                <entry key="2" value="面试宝典"></entry>
                <entry key="3" value="JavaWeb课程"></entry>
            </map>
        </property>
    </bean>
</beans>

app.java

package net.cybclass.sp;

import net.cybclass.sp.domain.Video;
import net.cybclass.sp.domain.VideoOrder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class app {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        Video video=(Video) applicationContext.getBean("video");
        System.out.println(video.getChapterList());
        System.out.println(video.getVideoMap());
    }
}

验证

技术图片

Spring5.X常见的注入方式

标签:pojo   center   tor   rgs   odi   getch   imp   dom   VID   

原文地址:https://www.cnblogs.com/chenyanbin/p/13303091.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!