<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!--set 注入-->
<!--<bean id="car" class="cn.hello.spring02_Zhujie_Zhujie.Car1">
<property name="color" value="红色"></property>
<property name="brand" value="兰博基尼"></property>
</bean>
<bean id="stu" class="cn.hello.spring02_Zhujie_Zhujie.Student">
<property name="age" value="19"></property>
<property name="name" value="哈哈"></property>
<property name="car" ref="car"></property>
</bean>-->
<!--P 注入-->
<bean id="car1" class="cn.hello.spring02_Zhujie.Car">
<property name="color" value="红色"></property>
<property name="brand" value="劳斯莱斯"></property>
</bean>
<bean id="stu1" class="cn.hello.spring02_Zhujie.Student" p:age="12" p:name="22" p:car-ref="car1">
</bean>
<!--List 属性注入-->
<!-- <bean id="Collection" class="cn.hello.spring02_Zhujie_Zhujie.MyCollection">
<property name="list">
<list>
<value>你好</value>
<value>呵呵</value>
</list>
</property>
</bean>-->
<!-- Set 属性注入-->
<!--<bean id="Collection2" class="cn.hello.spring02_Zhujie_Zhujie.MyCollection">
<property name="set">
<set>
<value>你好22</value>
<value>呵呵33</value>
</set>
</property>
</bean>-->
<!-- Map 属性注入-->
<!--<bean id="Collection3" class="cn.hello.spring02_Zhujie_Zhujie.MyCollection">
<property name="map">
<map>
<entry key="1">
<value>你说</value>
</entry>
<entry key="2">
<value>你唱</value>
</entry>
</map>
</property>
</bean>-->
<!--properties -->
<bean id="properties" class="cn.hello.spring02_Zhujie.MyCollection">
<property name="properties">
<props>
<prop key="001">hah</prop>
<prop key="003">xxx</prop>
<prop key="002">111</prop>
</props>
</property>
</bean>
</beans>
public class MyCollection {
private List<String> list;
private Set<String> set;
private Map<String,String> map;
private Properties properties;
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Set<String> getSet() {
return set;
}
public void setSet(Set<String> set) {
this.set = set;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
测试类:
@Test
/*测试 集合 注入 */
public void aVoid2(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext02.xml");
MyCollection bean = ctx.getBean(MyCollection.class);
System.out.println(bean.getProperties());
}
注解:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!--注解-->
<context:component-scan base-package="cn.hello.spring02_Zhujie">
</context:component-scan>
</beans>
@Component("student")
public class Student {
@Value("牛")
private String name;
@Value("18")
private Integer age;
@Resource()
private Car car2;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", car=" + car2 +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Car getCar2() {
return car2;
}
public void setCar2(Car car2) {
this.car2 = car2;
}
}
@Component()
public class Car {
private String brand;
@Value("彩色")
private String color;
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", color='" + color + '\'' +
'}';
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
@Test
/*测试 注解 */
public void aVoid3(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext03.xml");
/*Student bean = ctx.getBean(Student.class);
System.out.println(bean.toString());*/
Student student =(Student) ctx.getBean("student");
System.out.println(student);
}