Spring v3.0.2 Learning Note 6 - Inject Components
2010-10-25 11:09
381 查看
注入依赖对象
基本类型对象注入:
<bean id="myId" class="xxx">
<constructor-arg index="0" type="java.lang.String" value="aaa"/> //构造器注入
<porperty name="name" value="test"/> // 属性setter方式注入
</bean>
Ref
<bean id="personDao" class="com.spring.test.dao.PersonDao"/>
<bean id="personManager" class="com.spring.test.manager.PersonManager">
<property name="personDao" ref="personDao"/>
</bean>
使用内部bean
,但该bean不能被其他bean使用
<bean id="personManager" class="com.spring.test.manager.PersonManager">
<property name="personDao">
<bean class="com.spring.test.dao.PersonDao"/>
</property>
</bean>
spring还提供了其他注入方式,比如基于接口的注入等,详见参考手册。
集合类型的注入
public class PersonManager {
Set<String> sets = new HashSet<String>();
List<String> lists = new ArrayList<String>();
Properties properties = new Properties();
Map<String,String> maps = new HashMap<String,String>();
// 省略setter方法,对于spring的组件注入,对应的setter方法是必须的
!
}
注入Set
<bean id="personService" class="com.spring.test.manager.impl.PersonManager">
<property name="sets"> // name是类中的属性名
<set>
<value>AAA</value>
<value>BBB</value>
<value>CCC</value>
</set>
</property>
</bean>
测试类:
package com.spring.test.junit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.test.manager.IPersonService;
public class PersonServiceTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Test
public void test() {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(
"spring.xml");
IPersonService personService = (IPersonService) ctx
.getBean("personService");
for (String value : personService.getSets()) {
System.out.println(value);
}
}
}
运行该测试类,可在控制台看到Set的内容。
注入List
<property name="lists"> // name是类中的属性名
<list>
<value>AAA</value>
<value>BBB</value>
<value>CCC</value>
</list>
</property>
注入Properties
<bean id="personService" class="com.spring.test.manager.impl.PersonManager">
<property name="properties">
<props>
<prop key="key1">value1</prop>
<prop key="key2">value2</prop>
<prop key="key3">value3</prop>
</props>
</property>
</bean>
测试类:
package com.spring.test.junit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.test.manager.IPersonService;
public class PersonServiceTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Test
public void test() {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(
"spring.xml");
IPersonService personService = (IPersonService) ctx
.getBean("personService");
for (Object key : personService.getProperties().keySet()) {
System.out.println(personService.getProperties().getProperty(
(String) key));
}
}
}
运行该测试类,可在控制台看到注入的内容。
注入Map
<property name="maps">
<map>
<entry key="key1" value="value1"/>
<entry key="key2" value="value2"/>
<entry key="key3" value="value3"/>
</map>
</property>
基本类型对象注入:
<bean id="myId" class="xxx">
<constructor-arg index="0" type="java.lang.String" value="aaa"/> //构造器注入
<porperty name="name" value="test"/> // 属性setter方式注入
</bean>
Ref
<bean id="personDao" class="com.spring.test.dao.PersonDao"/>
<bean id="personManager" class="com.spring.test.manager.PersonManager">
<property name="personDao" ref="personDao"/>
</bean>
使用内部bean
,但该bean不能被其他bean使用
<bean id="personManager" class="com.spring.test.manager.PersonManager">
<property name="personDao">
<bean class="com.spring.test.dao.PersonDao"/>
</property>
</bean>
spring还提供了其他注入方式,比如基于接口的注入等,详见参考手册。
集合类型的注入
public class PersonManager {
Set<String> sets = new HashSet<String>();
List<String> lists = new ArrayList<String>();
Properties properties = new Properties();
Map<String,String> maps = new HashMap<String,String>();
// 省略setter方法,对于spring的组件注入,对应的setter方法是必须的
!
}
注入Set
<bean id="personService" class="com.spring.test.manager.impl.PersonManager">
<property name="sets"> // name是类中的属性名
<set>
<value>AAA</value>
<value>BBB</value>
<value>CCC</value>
</set>
</property>
</bean>
测试类:
package com.spring.test.junit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.test.manager.IPersonService;
public class PersonServiceTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Test
public void test() {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(
"spring.xml");
IPersonService personService = (IPersonService) ctx
.getBean("personService");
for (String value : personService.getSets()) {
System.out.println(value);
}
}
}
运行该测试类,可在控制台看到Set的内容。
注入List
<property name="lists"> // name是类中的属性名
<list>
<value>AAA</value>
<value>BBB</value>
<value>CCC</value>
</list>
</property>
注入Properties
<bean id="personService" class="com.spring.test.manager.impl.PersonManager">
<property name="properties">
<props>
<prop key="key1">value1</prop>
<prop key="key2">value2</prop>
<prop key="key3">value3</prop>
</props>
</property>
</bean>
测试类:
package com.spring.test.junit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.test.manager.IPersonService;
public class PersonServiceTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Test
public void test() {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(
"spring.xml");
IPersonService personService = (IPersonService) ctx
.getBean("personService");
for (Object key : personService.getProperties().keySet()) {
System.out.println(personService.getProperties().getProperty(
(String) key));
}
}
}
运行该测试类,可在控制台看到注入的内容。
注入Map
<property name="maps">
<map>
<entry key="key1" value="value1"/>
<entry key="key2" value="value2"/>
<entry key="key3" value="value3"/>
</map>
</property>
相关文章推荐
- Spring v3.0.2 Learning Note 2 - A Sample Example
- Spring v3.0.2 Learning Note 3 - Initialisation of Beans
- Spring v3.0.2 Learning Note 7 - Constructor-based dependency injection
- Spring v3.0.2 Learning Note 8 - Annotation-based dependency injection
- Spring v3.0.2 Learning Note 10 - Annotation-based Transaction Management
- Spring v3.0.2 Learning Note 12 - Integrate with Hiberate
- Spring v3.0.2 Learning Note 13 - AOP Example
- Spring v3.0.2 Learning Note 15 - Use Velocity Email Template with Spring
- The NOTE of learning ASP.NET [14] 使用.NET Framework中System.Xml命名空间下的类读写XML文件
- Spring v3.0.2 Learning Note 1 - Introduction to Spring Framework
- Python Learning Note
- Note of deeplearning.ai
- READING NOTE: Learning Deconvolution Network for Semantic Segmentation
- Paper Note - Learning to Hash with Binary Deep Neural Network
- CalTech machiine learning, video 10 note(Neural Network)
- sailsjs learning note
- English Learning Daily Note of Fourth
- Spring v3.0.2 Learning Note 16 (Cont.)- Resolve NullPointerException on weblogic v10.3
- c-learning-note(2017-9-20)
- Windows Azure Learning Note (5)