spring学习之注入对象
2015-10-07 20:34
465 查看
注入依赖对象
注入基本数据类型通过value属性注入
在beans.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-2.0.xsd"> <bean id="PersonService" class="com.gc.service.impl.PersonServiceImpl"> <property name="name" value="nihao"></property> </bean> </beans>
在接口bean对象中
package com.gc.service.impl; import com.gc.service.PersonService; public class PersonServiceImpl implements PersonService { public String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void save() { // TODO Auto-generated method stub System.out.println("name:"+name); System.out.println("save方法"); } }
结果:
ref属性注入bean对象
注入集合类型
1.set集合
<?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-2.0.xsd"> <bean id="PersonService" class="com.gc.service.impl.PersonServiceImpl"> <property name="sets"> <set> <value>first</value> <value>second</value> <value>third</value> </set> </property> </bean> </beans>
2.list集合
<?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-2.0.xsd"> <bean id="PersonService" class="com.gc.service.impl.PersonServiceImpl"> <property name="lists"> <list> <value>first</value> <value>second</value> <value>third</value> </list> </property> </bean> </beans>
3.property
<?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-2.0.xsd"> <bean id="PersonService" class="com.gc.service.impl.PersonServiceImpl"> <property name="propertys"> <props> <prop key="key1">first1</prop> <prop key="key2">first2</prop> <prop key="key3">first3</prop> </props> </property> </bean> </beans>
4.map类型
<?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-2.0.xsd"> <bean id="PersonService" class="com.gc.service.impl.PersonServiceImpl"> <property name="maps"> <map> <entry key="key1" value="value1" /> <entry key="key2" value="value2" /> <entry key="key3" value="value3" /> </map> </property> </bean> </beans>
相关文章推荐
- Maven+struts2+spring4+hibernate4的环境搭建
- java内存布局
- easyui+spring+springmvc+ibatis学习(一)
- 关键字解析
- JAVA自动化测试数据设计
- try catch finally java 异常捕捉
- 学习java自我激励
- java web判断是否登录
- 自己关于java编程的部分知识点总结
- Java基础编程2:1/1+1/3+...+1/99
- 安卓Eclipse工程导到AndroidStudio问题和导出jar包
- [笔记][Java7并发编程实战手册]7. 定制并发类
- 【Java基础】成员变量和局部变量的区别
- Java基本编程1:输出Hello World!
- 关于使用eclipse编写程序
- JAVA
- 从道德经中学习java
- Spring中BeanCreationException异常::0formul unbound in pointcut
- 从道德经中学习java
- Java中的静态分派与动态分派