当前位置: 首页 > news >正文

html5网站单页模板职业技能培训

html5网站单页模板,职业技能培训,番禺网站建设培训班,确定建设电子网站的目的目录 前言 回顾 注入集合 List与set集合 Map集合 前言 前面学习依赖注入时注入的都是对象,这里记录注入的值为集合的情况 回顾 在注入的时候,如果要注入的属性的值为字符串或基本数据类型,用value即可;如果要注入一个对象的…

目录

前言

回顾

注入集合

List与set集合

Map集合


前言

前面学习依赖注入时注入的都是对象,这里记录注入的值为集合的情况

回顾

    在注入的时候,如果要注入的属性的值为字符串或基本数据类型,用value即可;如果要注入一个对象的引用,则使用ref属性。

用一段代码进行演示: 

准备一个类B作为要注入的类

public class B {public void useB(){System.out.println("B对象成功注入.......");}
}

在类A中写入对象类型的属性B,基本数据类型count,字符串类型str;并且提供对应的set方法

public class A {B b;int count;String str;public void setStr(String str) {this.str = str;}public void setB(B b) {this.b = b;}public void setCount(int count) {this.count = count;}public void text(){System.out.println("即将使用属性B的方法");b.useB();System.out.println("注入的基本数据类型的值为"+count);System.out.println("注入的字符串为"+str);}
}

接着在spring的配置文件中配置bean,并且用对应的属性注入值给str,count,b

<?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 class="com.cc.Test.A" id="a"><property name="b" ref="b"></property><property name="count" value="188"></property><property name="str" value="StringString"></property></bean><bean class="com.cc.Test.B" id="b"></bean>
</beans>

最后编写一个测试类

public class Main {public static void main(String[] args) {ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("test.xml");A bean = (A) app.getBean("a");bean.text();}
}

测试结果:可以看到对应的值已经成功注入!!

 

注入集合

List与set集合

    要注入list集合,只需在spring的配置文件进行简单的修改,使用list标签,标签内使用很多value标签注入值:

<?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 class="com.cc.Test.C" id="c"><property name="list"><list><value>第一个值</value><value>two</value><value>33333</value></list></property></bean>
</beans>
public class C {List<String> list;public void setList(List<String> list) {this.list = list;}public void showList(){System.out.println(list);}
}

 当然,list集合内不止可以存字符串,也能存一个类的引用,此时只需将list标签内的value改为ref即可:

    <bean class="com.cc.Test.B" id="b"></bean><bean class="com.cc.Test.B" id="b1"></bean><bean class="com.cc.Test.B" id="b2"></bean><bean class="com.cc.Test.C" id="c"><property name="list"><list><ref bean="b"></ref><ref bean="b2"></ref><ref bean="b1"></ref></list></property></bean>
public class C {List<B> list;public void setList(List<B> list) {this.list = list;}public void showList(){System.out.println(list);}
}

同理,set集合只需在配置文件中将<list>改为<set>即可,这里就不做演示。

 

Map集合

Map集合与list和set略有不同,因为它的值为key/value键值对。

    <bean class="com.cc.Test.C" id="c"><property name="map"><map><entry key="1" value="one"></entry><entry key="2" value-ref="b1"></entry></map></property></bean>

http://www.mmbaike.com/news/92160.html

相关文章:

  • 外贸人常用网站精准客源引流平台
  • wordpress怎么用啊seo点击排名软件哪家好
  • wordpress文章类插件海外aso优化
  • 用自建网站做外贸上海服务政策调整
  • php商业网站制作google adsense
  • wordpress mibt手机seo百度点击软件
  • 网渠道seo推广软件排名
  • 影视网站视频接口怎么做外包公司
  • 营销型网站建设是什么意思it培训机构哪个好
  • 动态网站开发 文献综述十大搜索引擎网站
  • 做外汇那个网站好网络营销策划论文
  • 如何采集网站文章关键词排名优化怎么做
  • 做视频的素材怎么下载网站电子商务网站建设多少钱
  • 顺德做营销网站公司广告推广怎么做最有效
  • 网站做的关键词被屏蔽推广方式
  • 发送wordpress湖南网站seo公司
  • 青岛企业网站制作公司电脑培训学校课程
  • 自助建站原理淘宝关键词查询
  • 企业网站建设哪家专业疫情最新政策最新消息
  • 初做淘宝客选哪个网站国内seo工具
  • php网站开发图文教程新品推广计划与方案
  • 自己做网站可以用私有云吗网络销售 市场推广
  • 深圳网站seo建设网页设计作品集
  • 有官网建手机网站seo短视频网页入口引流
  • 普洱北京网站建设网站点击快速排名
  • 徐州市网站建设seo云优化软件
  • 专做衬衣的网站店铺运营
  • 做网站用什么开发语言互联网媒体推广
  • 网站备案中页面全自动推广引流软件免费
  • phpweb网站搬家教程油烟机seo关键词