1.通过有参构造方法注入属性
Bean1.java
package zh.spring.entity;
public class Bean1 {
private String name;
public Bean1(){
}
// 通过有参构造方法注入属性,就必须提供有参构造方法
public Bean1(String name){
this.name = name;
}
public void fun() {
System.out.println("Bean1..." + name);
}
}
在src下创建bean1.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.xsd">
<!-- 通过有参构造方法注入属性 -->
<bean id="bean1" class="zh.spring.entity.Bean1"> <!-- 默认scope="singleton" -->
<constructor-arg name="name" value="nameOfBean1"></constructor-arg>
</bean>
</beans>
Bean1Test.java
package zh.spring.entity;
public class Bean1 {
private String name;
private Integer age;
public Bean1(){
}
// 通过set方法注入属性,就必须提供set方法
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void fun() {
System.out.println("Bean1..." + name+","+age);
}
}
2.通过set方法注入属性
Bean1.java
package zh.spring.entity;
public class Bean1 {
private String name;
public Bean1(){
}
// 通过set方法注入属性,就必须提供set方法
public void setName(String name) {
this.name = name;
}
public void fun() {
System.out.println("Bean1..." + name);
}
}
在src下创建bean1.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.xsd">
<!-- 通过set方法注入属性 -->
<bean id="bean1" class="zh.spring.entity.Bean1"> <!-- 默认scope="singleton" -->
<property name="name" value="nameOfBean1"></property>
<property name="age" value="24"></property>
</bean>
</beans>
Bean1Test.java
package zh.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import zh.spring.entity.Bean1;
public class Bean1Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean1.xml");
Bean1 bean1 = (Bean1) applicationContext.getBean("bean1");
bean1.fun();// Bean1...nameOfBean1,24
}
3.通过名称空间注入属性
Bean1.java
package zh.spring.entity;
public class Bean1 {
private String name;
private Integer age;
private char sex;
public Bean1() {
}
// 通过名称空间注入属性,也要提供set方法
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setSex(char sex) {
this.sex = sex;
}
public void fun() {
System.out.println("Bean1..." + name + "," + age + "," + sex);
}
}
在src下创建bean1.xml,并且引入名称空间。
Bean1Test.java
package zh.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import zh.spring.entity.Bean1;
public class Bean1Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean1.xml");
Bean1 bean1 = (Bean1) applicationContext.getBean("bean1");
bean1.fun();// Bean1...nameOfBean1,18,男
}
}
4.注入复杂属性
MyService.java
package zh.spring.entity;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class MyService {
private MyDao myDao;
private Integer[] array;
private List<String> list;
private Map<Integer, String> map;
private Properties properties;
public void setMyDao(MyDao myDao) {
this.myDao = myDao;
}
public void setArray(Integer[] array) {
this.array = array;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMap(Map<Integer, String> map) {
this.map = map;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
// MyService的dao()
public void dao(){
myDao.dao();
}
@Override
public String toString() {
return "MyService [myDao=" + myDao + ", array="
+ Arrays.toString(array) + ", list=" + list + ", map=" + map
+ ", properties=" + properties + "]";
}
}
MyDao.java
package zh.spring.entity;
public class MyDao {
public void dao() {
System.out.println("dao...");
}
}
在src下创建bean.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.xsd">
<bean id="myDao" class="zh.spring.entity.MyDao"></bean>
<bean id="myService" class="zh.spring.entity.MyService" scope="prototype">
<!-- 注入对象MyDao -->
<property name="myDao" ref="myDao"></property>
<!-- 注入数组Integer[] -->
<property name="array">
<array>
<value>11</value>
<value>22</value>
<value>33</value>
</array>
</property>
<!-- 注入List<String> -->
<property name="list">
<list>
<value>element1</value>
<value>element2</value>
<value>element3</value>
</list>
</property>
<!-- 注入Map<Integer,String> -->
<property name="map">
<map>
<entry key="1" value="value1"></entry>
<entry key="2" value="value2"></entry>
<entry key="3" value="value3"></entry>
</map>
</property>
<!-- 注入Properties -->
<property name="properties">
<props>
<prop key="key11">value11</prop>
<prop key="key22">value22</prop>
<prop key="key33">value33</prop>
</props>
</property>
</bean>
</beans>
BeanTest.java
package zh.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import zh.spring.entity.MyService;
public class BeanTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
MyService myService = (MyService) applicationContext.getBean("myService");
System.out.println(myService);
// MyService [myDao=zh.spring.entity.MyDao@6ae1f1f1,
// array=[11, 22, 33],
// list=[element1, element2, element3],
// map={1=value1, 2=value2, 3=value3},
// properties={key11=value11, key22=value22, key33=value33}]
myService.dao();// dao...
}
}