一、配置文件
1.application.xml
<?xml version="1.0" encoding="utf-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p">
<!--通过构造方法注入:带参数。p命名空间-->
<bean name="user" class="test.spring.Domain.UserBean">
<constructor-arg name="id" value="0"></constructor-arg>
<constructor-arg name="age" value="2"></constructor-arg>
<constructor-arg name="name" value="张三"></constructor-arg>
</bean>
<bean name="userC" class="test.spring.Domain.UserBean" c:id="1" c:age="2" c:name="李四"></bean>
<!--通过setter方法注入,p命名空间-->
<bean name="dog" class="test.spring.Domain.AnimalBean">
<property name="id" value="0"></property>
<property name="age" value="2"></property>
<property name="name" value="dog"></property>
</bean>
<bean name="cat" class="test.spring.Domain.AnimalBean" p:id="1" p:age="3" p:name="cat"></bean>
<!--StudentBean里注入AnimalBean: -ref 就是引用的意思-->
<bean name="duck" class="test.spring.Domain.AnimalBean" p:id="1" p:age="3" p:name="duck"></bean>
<bean name="student" class="test.spring.Domain.StudentBean" p:animal-ref="duck" p:name="小明"></bean>
<!--SpEL注入-->
<!--<bean id="sum" value="#{}"/>-->
<!--集合类型,数组,set等类型属性-->
<!--<bean id="listBean" class="com."> -->
<!--<property name="list"> -->
<!--<list>-->
<!--<value>hello</value> -->
<!--<value>how</value> -->
<!--<value>you</value> -->
<!--</list> -->
<!--</property> -->
<!--<property name="ids"> -->
<!--<array> -->
<!--<value>1</value> -->
<!--<value>2</value>-->
<!--</array> -->
<!--</property> -->
<!--</bean>-->
</beans>
二、测试
1.userBean
package test.spring.Domain;
public class UserBean {
private int id;
private int age;
private String name;
public UserBean(int id, int age, String name) {
this.id = id;
this.age = age;
this.name = name;
} @Override
public String toString() {
return "UserBean{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}';
}
}
2.AnimalBean
package test.spring.Domain;
public class AnimalBean {
private int id;
private int age;
private String name;
public int getId() {return id;}
public void setId(int id) { this.id = id;}
public int getAge() { return age; }
public void setAge(int age) {this.age = age; } public String getName() { return name;}
public void setName(String name) { this.name = name; }
}
3.TestController
public class TestController {
public static void main(String[] args){
/* 第一步:初始化spring容器*/
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
/* 第二步:从spring容器获取Bean*/
//构造器方法
UserBean userBean = (UserBean) applicationContext.getBean("user");
System.out.println( userBean.toString());
//setter方法
AnimalBean animalBean = (AnimalBean)applicationContext.getBean("dog");
System.out.println(animalBean.getName());
//setter方法 往StudentBean 里注入AnimalBean
StudentBean stu = (StudentBean)applicationContext.getBean("student");
stu.say();
}
}