spring利用set操作对引用类型和集合类型进行注入值
来源:互联网 发布:linux怎么删除目录 编辑:程序博客网 时间:2024/03/29 09:46
下面说说利用set对对象进行注入值。下面举个例子:
1.对引用对对象如何注入值呢?
首先写一个person类作为引用类型
Person.Java
packagecom.fish;
public class Person {
Stringname ;
Stringid;
publicString getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
publicString getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
然后写一个对person的引用类
packagecom.fish;
public class Student {
Personperson;
int age;
publicPerson getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
那么在spring.XML
方法一: <beanid="fish"class="com.fish.Person">
<propertyname="id"value="11"></property>//首先对person对象注入值,id,name是基本对象。Name写的是person里面的属性,value是你想注入的值
<propertyname="name"value="yaku"></property>//同理
</bean>
<beanid="bulefish"class="com.fish.Student">//然后对student类的实例化
<propertyname="age"value="13"></property>
<propertyname="person"ref="fish"></property>//如果是引用对象就ref属性,来获取值ref里面写的是你想引用的对象的名字。和上面的fish是对应的。
</bean>
方法二:在bean的内部注入:
<beanid="redfish"class="com.fish.Student">
<propertyname="person">//对人这个属性赋值
<beanclass="com.fish.Person">//首先关联这个类
<propertyname="id"value="16"></property>//然后按正常赋值就行
<propertyname="name"value="yayc"></property>
</bean>
</property>
<propertyname="age"value="13"></property>
</bean>
这里内部注入就有点想内部匿名类一样,其他bean访问不到他。
那么我们写一个测试类:Test.JAVA
package com.fish;
importorg.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContextcontext=new ClassPathXmlApplicationContext("spring.xml");
Studentstudent=(Student)context.getBean("redfish");
System.out.println(student.getAge()+""+student.getPerson().getId()+""+student.getPerson().getName());
}
}
我们就可以得到我们注入的值:结果是13 16 yaku
第二:对集合对象的注入,list,map和属性对象的注入。
我们就在原有的基础上拓展吧。
首先在student类里面加上这些属性
List<String>list;
Propertiesproperties;
Map<String,String> map;
publicProperties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
publicMap<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
publicList<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
然后在spring.xml写上
<beanid="redfish"class="com.fish.Student">
<propertyname="person">
<beanclass="com.fish.Person">
<propertyname="id"value="16"></property>
<propertyname="name"value="yayc"></property>
</bean>
</property>
<propertyname="age"value="12"></property>
<propertyname="list"> //这是对list的注入首先name里面的值依旧是填上我们类中对应的属性,下面也一样
<list> //spring有标签对lst集合进行赋值的。
<value>a</value>
<value>b</value>
<value>c</value>
<value>d</value>
</list>//这段话就是讲abcd放入 list集合中
</property>
<propertyname="map"> //同理map也是有有标签的
<map>
<entrykey="q"value="zzzz"></entry> //存放按键值对形式。
<entrykey="w"value="xxxx"></entry>
<entrykey="e"value="cccc"></entry>
<entrykey="r"value="vvvv"></entry>
</map>
</property>
<propertyname="properties"> //properties也是一样的。
<props>
<propkey="b">aaa</prop>
<propkey="n">bbb</prop>
<propkey="m">ccc</prop>
</props>
</property>
</bean>
下面对这些注入的值进行读取吧:
我们在test.Java在写上
for(String i:student.getList()){
System.out.println(i);
}
for(String i:student.getMap().keySet()){
System.out.println(i+"="+student.getMap().get(i));
}
for(Object i:student.getProperties().keySet()){
System.out.println(i+"="+student.getProperties().getProperty((String) i));
}
这样我们就可以获取我们需要的值了。利用增强for循环对值的遍历。
- spring利用set操作对引用类型和集合类型进行注入值
- Spring集合类型值注入
- Spring对集合类型的属性注入
- Spring、基本类型属性和集合类型属性的注入
- Java中引用类型vs值类型&对引用类型所做的GC操作简介
- spring 注入枚举类型和日期类型
- 把Set单值类型的集合扩张成Map对值类型的集合
- Spring @Autowired 注入集合类型的问题
- Spring自动类型转换/集合属性注入
- Spring集合类型数据的注入
- Spring-注入参数详解-[集合类型属性]
- Spring @Autowired 注入集合类型的问题
- 对值类型和引用类型的全方位认识
- 对值类型和引用类型的理解
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- Android4
- 初识字节对齐
- STM32 外部中断 易出错总结
- PostgreSQL学习手册(系统视图)
- nutch java.io.UTFDataFormatException: Invalid byte 1 of 1-byte UTF-8 sequence
- spring利用set操作对引用类型和集合类型进行注入值
- StringUtils详细介绍
- android:屏幕自适应
- auto,register,static分析
- 10进制转换16进制
- asp.net开发常用技巧收集
- CI9.3-旋转数组查找给定值
- extjs动态树的生成。
- 易语言POST教程-CSDN上传头像解析