一、通過Bean類型從IOC容器中獲取Bean實例
<code>
<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>
<property>
<property>
<property>
/<bean>
<bean>
<property>
/<bean>
/<beans>/<code>
<code>package com.bigdata.test;
import com.bigdata.spring.Person;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PersonTest {
/**
* 實驗2:根據Bean的類型從IOC容器中獲取Bean的實例
* 如果ioc容器中這個類型的bean有多個,查找就會報錯。
*/
@Test
public void testGetPersonBean() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc.xml");
Person person = ioc.getBean(Person.class);
System.out.println(person);
Person person02 = ioc.getBean("person02", Person.class);
System.out.println(person02);
}
}
/<code>
二、通過構造器為Bean的屬性賦值
1、在Person類中創建有參和無參構造器
<code>package com.bigdata.spring;
public class Person {
private String lastName;
private int age;
private String email;
public Person() {
super();
System.out.println("無參構造器被調用了");
}
public Person(String lastName, int age, String email) {
super();
this.lastName = lastName;
this.age = age;
this.email = email;
System.out.println("#########有參構造器被調用了###########");
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\\'' +
", age=" + age +
", email='" + email + '\\'' +
'}';
}
}
/<code>
2、在ioc.xml文件中配置有參注入方法
<code>
<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>
<property>
<property>
<property>
/<bean>
<bean>
<property>
/<bean>
<bean>
<constructor-arg>
<constructor-arg>
<constructor-arg>
/<bean>
/<beans>/<code>
3、測試參數構造器給Bean屬性賦值
<code> @Test
public void testGetPersonBean() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc.xml");
// Person person = ioc.getBean(Person.class);
// System.out.println(person);
Person person02 = ioc.getBean("person02", Person.class);
System.out.println(person02);
Person person03 = ioc.getBean("person03", Person.class);
System.out.println(person03);
}/<code>
三、通過有參構造器給Bean屬性賦值index type
1、在ioc.xml添加配置信息
<code>
<bean>
<constructor-arg>
<constructor-arg>
<constructor-arg>
/<bean>/<code>
2、測試有參構造器給Bean屬性賦值--index type
<code> @Test
public void testGetPersonBean() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc.xml");
// Person person = ioc.getBean(Person.class);
// System.out.println(person);
// Person person02 = ioc.getBean("person02", Person.class);
// System.out.println(person02);
//
// Person person03 = ioc.getBean("person03", Person.class);
// System.out.println(person03);
Person person04 = ioc.getBean("person04", Person.class);
System.out.println(person04);
}/<code>
四、通過P命名空間為Bean賦值
1、在ioc.xml文件中添加p標籤
<code>
<beans> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<bean>
/<bean>
/<beans>/<code>
2、在PersonTest.java中添加測試方法
<code> @Test
public void testGetPersonBean() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc.xml");
// Person person = ioc.getBean(Person.class);
// System.out.println(person);
// Person person02 = ioc.getBean("person02", Person.class);
// System.out.println(person02);
//
// Person person03 = ioc.getBean("person03", Person.class);
// System.out.println(person03);
// Person person04 = ioc.getBean("person04", Person.class);
// System.out.println(person04);
Person person05 = ioc.getBean("person05", Person.class);
System.out.println(person05);
}/<code>
閱讀更多 大數據漫路求索 的文章