• Spring5.X常见的注入方式


    使用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());
        }
    }

    验证

  • 相关阅读:
    C++ vector介绍
    C++string的使用
    关于VS2010error RC2170 : bitmap file res mp1.bmp is not in 3.00 format
    团队项目第一次讨论
    团队项目——铁大树洞个人分析
    第五周学习进度总结
    转发
    android移动端疫情展示
    《构建之法》阅读笔记03
    第四周学习进度总结
  • 原文地址:https://www.cnblogs.com/chenyanbin/p/13303091.html
Copyright © 2020-2023  润新知