spring学习(43):属性注入中注入引用对象

目录结构

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=ERROR

CompactDisc类

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 {@Autowiredprivate CompactDisc compactDisc1;//@Autowired//private CompactDisc compactDisc12;//@Autowired//@Qualifier("compacDisc2")// private CompactDisc compactDisc3;@Testpublic 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 {@Autowiredprivate CDPlayer cdPlayer1;@Autowiredprivate CDPlayer cdPlayer2;@Testpublic void test01(){cdPlayer1.play();}@Testpublic 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 {@Autowiredprivate CDPlayer cdPlayer;@Testpublic 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

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/424394.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

spring学习(44):p名称空间注入

目录结构 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/P…

A*算法

文章出处&#xff1a;极客时间《数据结构和算法之美》-作者&#xff1a;王争。该系列文章是本人的学习笔记。 Dijkstra不能解决的问题 在Dijkstra类似BFS&#xff0c;从起始节点找到距离最短的节点&#xff0c;一层一层向外扩展&#xff0c;直到找到目标节点。 在有些时候这种…

spring学习(45):util名称空间注入

目录结构 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/P…

Jmeter_http request的简单设置和应用

http request 协议、地址、端口号 参数类型 正则搜索 帮助文档 相等的关系 匹配正则的结果&#xff08;jmeter一般都用分组取想用的数据&#xff09; 转载于:https://www.cnblogs.com/rychh/articles/11087537.html

spring学习(46):spring的单例bean

目录结构 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/P…

943. Find the Shortest Superstring

目录题目描述暴力搜索分析暴力搜索优化动态规划参考链接题目描述 输入&#xff1a;字符串数组String[] A 输出&#xff1a;一个字符串result&#xff0c;A中每一个字符串是result的子串&#xff0c;并且reuslt是符合这个条件的最短的字符串。 举例&#xff1a; 输入: [“alex”…

spring学习(47):bean的作用域

目录结构 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/P…

visual studio 2017 显示行号

1.Tools中选择Options...&#xff0c; Options界面中Test Editor -> All Languages&#xff08;当然也可以设置对应的语言&#xff09; -> General&#xff0c; 在Settings中勾选 Line numbers。 2.行号就显示出来了&#xff0c;便于我们去定位代码。 转载于:https://www…

spring学习(48):自动装配中定义的bean的作用域

目录结构 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/P…

212. Word Search II:多个单词查找

写在前面&#xff1a;这两周持续看花花酱整理的题目列表和视频讲解&#xff0c;也得益于自己持续多年刷题&#xff0c;今天刷这道题目的想法是&#xff1a;会trie树居然就能攻克hard题目&#xff01;我也离独立攻破hard题目不远了嘛。前段时间看王争在极客时间的系列课程&#…

spring学习(49):javaconfig里面定义bean的作用域

目录结构 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/P…

spring学习(50):延迟加载

目录结构 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/P…

spring学习(51):对象的初始化和销毁

目录结构 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/P…

282. Expression Add Operators

目录题目理解分析第一步&#xff1a;dfs获得所有表达式第二步&#xff1a;计算结果先计算加减法计算乘法时间复杂度进一步优化题目理解 输入&#xff1a;一个字符串num&#xff0c;一个int target。输入num只包含数字。 规则&#xff1a;可以给num中包含的数字之间任务添加二目…

spring学习(52):工厂方法创建bean对象

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 …

com.alibaba.fastjson.JSONObject cannot be cast to XXX异常解决

项目开发中遇到过几次这个问题&#xff0c;所以记录一下&#xff0c;代码如下&#xff1a; 1 PostMapping("/save-files") 2 public void addFiles(RequestBody List<AddFileRecordRequestDto> fileRecords) 3 { 4 LoginInfo loginInfosession…

spring mvc学习(2):spring jar包下载

jstl的jar包的下载 我们在使用spring框架的时候导入jstl标签库需要使用到jstl的jar包&#xff0c;假如没有加入到eclipse的lib目录下&#xff0c;使用alt /的时候不会有提示&#xff0c;所以我们需要把这个jar包加进来 首先登陆网址&#xff1a;http://tomcat.apache.org/ta…

EF框架学习(5)---EF中的在线和离线场景

EF中的持久性场景 使用EF实现实体持久化&#xff08;保存&#xff09;到数据库有两种情况&#xff1a;在线场景和离线场景。 1.在线场景 在线场景中&#xff0c;context是同一个上下文实例&#xff08;从DbContext派生&#xff09;&#xff0c;检索和保存实体都通过同一个conte…

spring mvc学习(3):建立第一个动态web项目

intellij idea创建第一个动态web项目 我以2018版的intellij idea为例 一.创建JavaWeb 1.第一步 点击左上角的File-->New-->Project 2.第二步 1.找到Java Enterprise之后&#xff0c;在Application Sever中找到自己的Tomcat&#xff0c;同时勾选中Web Application 2.…

Git:(1)简介

Git是一个开源的分布式版本控制系统&#xff0c;分布式相比集中式的最大区别是Git没有“中央服务器”&#xff0c;每位开发者都可以通过克隆&#xff08;git clone&#xff09;远程库&#xff0c;在本地机器上存储一个完整的Git仓库&#xff0c;还可以把代码的修改提交到本地库…