spring学习42-属性注入中注入引用对象

简介: spring学习42-属性注入中注入引用对象

image.png

pom.xml
    <?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.geyao</groupId>
         <artifactId>spring01geyao</artifactId>
         <version>1.0-SNAPSHOT</version>
         <dependencies>
             <dependency>
                 <groupId>org.springframework</groupId>
                 <artifactId>spring-context</artifactId>
                 <version>4.3.13.RELEASE</version>
             </dependency>
             <dependency>
                 <groupId>org.springframework</groupId>
                 <artifactId>spring-test</artifactId>
                 <version>3.2.0.RELEASE</version>
             </dependency>
             <dependency>
                 <groupId>log4j</groupId>
                 <artifactId>log4j</artifactId>
                 <version>1.2.17</version>
             </dependency>
             <dependency>
                 <groupId>junit</groupId>
                 <artifactId>junit</artifactId>
                 <version>4.12</version>
             </dependency>
             <dependency>
                 <groupId>org.springframework</groupId>
                 <artifactId>spring-test</artifactId>
                 <version>4.3.4.RELEASE</version>
                 <scope>test</scope>
             </dependency>
             <dependency>
                 <groupId>org.springframework</groupId>
                 <artifactId>spring-test</artifactId>
                 <version>4.3.4.RELEASE</version>
                 <scope>test</scope>
             </dependency>
         </dependencies>
     </project>applictioncontext.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元素:描述当前的对象需要由spring容器管理
         id属性:标识对象 未来在应用程序中可以根据id获取对象
         class对象:被管理的对象的全名
        -->
         <bean id="music1" class="com.geyao.demo.soundSystem.Music">
             <property name="title" value="告白气球"/>
             <property name="duration" value="215"/>
         </bean>
         <bean id="music2" class="com.geyao.demo.soundSystem.Music">
             <property name="title" value="爱情废材"/>
             <property name="duration" value="215"/>
         </bean>
         <bean id="compactDisc2" class="com.geyao.demo.soundSystem.CompactDisc">
             <property name="title" value="爱情废材"/>
             <property name="artist" value="周杰伦"/>
             <property name="tracks" >
                 <array>
                     <!--<value>i do 1</value>
                     <value>i do 2</value>
                     <value>i do 3</value>
                     <ref bean="music1"/>
                     <ref bean="music2"/>
                     <ref bean="music3"/>
                     <ref bean="music3"/>
                     <entry key="m1" value-ref="music1"/>
                     <entry key="m2" value-ref="music2"/>
                     <entry key="m3" value-ref="music3"/> -->
                     <ref bean="music1"/>
                     <ref bean="music2"/>
                 </array>
             </property>
         </bean>
         <bean id="CDplayer1" class="com.geyao.demo.soundSystem.CDPlayer">
             <property name="cd" ref="compactDisc2"/>
         </bean>
     </beans>log4j.properties
    ### 设置###
     log4j.rootLogger = ERROR,stdout
     ### 输出信息到控制抬 ###
     log4j.appender.stdout = org.apache.log4j.ConsoleAppender
     log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
     log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
     log4j.category.org.springframework.beans.factory=ERRORCompactDisc类
    package com.geyao.demo.soundSystem;
     import java.util.List;
     import java.util.Map;
     import java.util.Set;
     public class CompactDisc {
         private String title;
         private String artist;
         private Music[] tracks;
         public CompactDisc() {
             super();
             System.out.println("compactdisc构造函数");
         }
         public CompactDisc(String title,String artist){
             this.title =title;
             this.artist=artist;
             System.out.println("compactDisc的有残构造函数"+this.toString());
         }
         public CompactDisc(String title, String artist, Music[] tracks) {
             this.title = title;
             this.artist = artist;
             this.tracks = tracks;
             System.out.println("s三个方法的构造函数");
         }
         public String getTitle() {
             return title;
         }
         public void setTitle(String title) {
             this.title = title;
             System.out.println("一再"+this.toString()+"中注入title");
         }
         public String getArtist() {
             return artist;
         }
         public void setArtist(String artist) {
             this.artist = artist;
             System.out.println("一再"+this.toString()+"中注入artist");
         }
         public Music[] getTracks() {
             return tracks;
         }
         public void setTracks(Music[] tracks) {
             this.tracks = tracks;
             System.out.println("一再"+this.toString()+"中注入tracks");
         }
         public void play(){
             System.out.println("播放cd音乐"+this.toString()+""+this.title+""+this.artist);
             for(Music track:this.tracks){
                System.out.println("音乐"+track.getTitle()+"时长"+track.getDuration());
             }
             //for(String key:this.tracks.keySet()){
              //   System.out.println("key:"+key);
               //  Music music=this.tracks.get(key);
              //   System.out.println("音乐"+music.getTitle()+"时长"+music.getDuration());
            // }
         }
     }Music类
    package com.geyao.demo.soundSystem;
     public class Music {
         private String title;
         private Integer duration;
         public String getTitle() {
             return title;
         }
         public void setTitle(String title) {
             this.title = title;
             System.out.println("一再"+this.toString()+"中注入title");
         }
         public Integer getDuration() {
             return duration;
         }
         public void setDuration(Integer duration) {
             this.duration = duration;
             System.out.println("一再"+this.toString()+"中注入duration");
         }
         public Music() {
             super();
             System.out.println("Music的狗只函数。。。"+this.toString());
         }
         public Music(String title, Integer duration) {
             this.title = title;
             this.duration = duration;
         }
     }cdplayer类
    package com.geyao.demo.soundSystem;
     public class CDPlayer {
         private CompactDisc cd;
         public CDPlayer() {
             super();
             System.out.println("cdplayer的构造方法"+this.toString());
         }
         public CDPlayer(CompactDisc cd){
             this.cd=cd;
             System.out.println("cdplayer的有残构造方法");
         }
         public CompactDisc getCd() {
             return cd;
         }
         public void setCd(CompactDisc cd) {
             this.cd = cd;
             System.out.println("一再"+this.toString()+"中注入cd");
         }
         public void play(){
             System.out.println("CDplayer:"+this.toString());
             cd.play();
         }
     }ApplicationSpring类
    import com.geyao.demo.soundSystem.CompactDisc;
     import org.springframework.context.support.ClassPathXmlApplicationContext;
     public class ApplicationSpring {
         public static void main(String[] args){
             System.out.println("Application ");
             ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
             CompactDisc cd1=(CompactDisc) context.getBean("compactDisc1");
             CompactDisc cd2=(CompactDisc) context.getBean("compactDisc2");
             cd1.play();
             cd2.play();
         }
     }appTest类
    package com.geyao.demo.souundSystem;
     import com.geyao.demo.soundSystem.CompactDisc;
     import org.junit.Test;
     import org.junit.runner.RunWith;
     import org.springframework.beans.factory.annotation.Autowired;
     import org.springframework.beans.factory.annotation.Qualifier;
     import org.springframework.test.context.ContextConfiguration;
     import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     @RunWith(SpringJUnit4ClassRunner.class)
     @ContextConfiguration("classpath:applicationContext.xml")
     public class AppTest {
         @Autowired
         private CompactDisc compactDisc1;
         //@Autowired
         //private CompactDisc compactDisc12;
         //@Autowired
         //@Qualifier("compacDisc2")
        // private CompactDisc compactDisc3;
         @Test
         public void testApp(){
             compactDisc1.play();
            // compactDisc12.play();
            // compactDisc3.play();
         }
     }
      CDplaytest类
    package com.geyao.demo.souundSystem;
     import com.geyao.demo.soundSystem.CDPlayer;
     import org.junit.Test;
     import org.junit.runner.RunWith;
     import org.springframework.beans.factory.annotation.Autowired;
     import org.springframework.test.context.ContextConfiguration;
     import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     @RunWith(SpringJUnit4ClassRunner.class)
     @ContextConfiguration("classpath:applicationContext.xml")
     public class CDplayerTest {
         @Autowired
         private CDPlayer cdPlayer1;
         @Autowired
         private CDPlayer cdPlayer2;
         @Test
         public void test01(){
             cdPlayer1.play();
         }
         @Test
         public void test02(){
             cdPlayer1.play();
         }
     }ApptestSX类
    package com.geyao.demo.souundSystem;
     import com.geyao.demo.soundSystem.CDPlayer;
     import org.junit.Test;
     import org.junit.runner.RunWith;
     import org.springframework.beans.factory.annotation.Autowired;
     import org.springframework.test.context.ContextConfiguration;
     import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     @RunWith(SpringJUnit4ClassRunner.class)
     @ContextConfiguration("classpath:applicationContext.xml")
     public class AppTestSX {
         @Autowired
         private CDPlayer cdPlayer;
         @Test
         public void test(){
             cdPlayer.play();
         }
     }运行结果
    Music的狗只函数。。。com.geyao.demo.soundSystem.Music@66d3eec0
     一再com.geyao.demo.soundSystem.Music@66d3eec0中注入title
     一再com.geyao.demo.soundSystem.Music@66d3eec0中注入duration
     Music的狗只函数。。。com.geyao.demo.soundSystem.Music@428640fa
     一再com.geyao.demo.soundSystem.Music@428640fa中注入title
     一再com.geyao.demo.soundSystem.Music@428640fa中注入duration
     compactdisc构造函数
     一再com.geyao.demo.soundSystem.CompactDisc@506ae4d4中注入title
     一再com.geyao.demo.soundSystem.CompactDisc@506ae4d4中注入artist
     一再com.geyao.demo.soundSystem.CompactDisc@506ae4d4中注入tracks
     cdplayer的构造方法com.geyao.demo.soundSystem.CDPlayer@7d4f9aae
     一再com.geyao.demo.soundSystem.CDPlayer@7d4f9aae中注入cd
     CDplayer:com.geyao.demo.soundSystem.CDPlayer@7d4f9aae
     播放cd音乐com.geyao.demo.soundSystem.CompactDisc@506ae4d4爱情废材周杰伦
     音乐告白气球时长215
     音乐爱情废材时长215
相关文章
|
28天前
|
Java Spring
在使用Spring的`@Value`注解注入属性值时,有一些特殊字符需要注意
【10月更文挑战第9天】在使用Spring的`@Value`注解注入属性值时,需注意一些特殊字符的正确处理方法,包括空格、引号、反斜杠、新行、制表符、逗号、大括号、$、百分号及其他特殊字符。通过适当包裹或转义,确保这些字符能被正确解析和注入。
|
12天前
|
前端开发 Java 开发者
Spring生态学习路径与源码深度探讨
【11月更文挑战第13天】Spring框架作为Java企业级开发中的核心框架,其丰富的生态系统和强大的功能吸引了无数开发者的关注。学习Spring生态不仅仅是掌握Spring Framework本身,更需要深入理解其周边组件和工具,以及源码的底层实现逻辑。本文将从Spring生态的学习路径入手,详细探讨如何系统地学习Spring,并深入解析各个重点的底层实现逻辑。
38 9
|
1月前
|
前端开发 Java 数据库
SpringBoot学习
【10月更文挑战第7天】Spring学习
35 9
|
1月前
|
XML Java 数据格式
Spring学习
【10月更文挑战第6天】Spring学习
20 1
|
1月前
|
Java 测试技术 开发者
springboot学习四:Spring Boot profile多环境配置、devtools热部署
这篇文章主要介绍了如何在Spring Boot中进行多环境配置以及如何整合DevTools实现热部署,以提高开发效率。
61 2
|
1月前
|
前端开发 Java 程序员
springboot 学习十五:Spring Boot 优雅的集成Swagger2、Knife4j
这篇文章是关于如何在Spring Boot项目中集成Swagger2和Knife4j来生成和美化API接口文档的详细教程。
92 1
|
6月前
|
存储 Java 数据库
Spring的使用-Bean对象的储存和获取/Bea对象的作用域与生命周期
Spring的使用-Bean对象的储存和获取/Bea对象的作用域与生命周期
|
设计模式 前端开发 Java
Spring Bean对象生命周期
Spring Bean对象生命周期
412 0
|
存储 Java Spring
【Spring原理探索】深入认识对象生命周期之BeanPostProcessor
【Spring原理探索】深入认识对象生命周期之BeanPostProcessor
137 0
【Spring原理探索】深入认识对象生命周期之BeanPostProcessor
|
Java Maven 开发者
Spring 对象生命周期示例|学习笔记
快速学习Spring 对象生命周期示例
126 0
Spring 对象生命周期示例|学习笔记