Spring4 第三讲

public class People {

	private int id;
	private String name;
	private int age;
	
	
	
	public People() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	
	
	
	public People(int id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}




	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}


	@Override
	public String toString() {
		return "People [id=" + id + ", name=" + name + ", age=" + age + "]";
	}

	
	
}

public class PeopleFactory {

	public People createPeople(){
		People p=new People();
		p.setId(5);
		p.setName("小七");
		p.setAge(77);
		return p;
	}
}

public class PeopleFactory2 {

	public static People createPeople(){
		People p=new People();
		p.setId(8);
		p.setName("小八");
		p.setAge(88);
		return p;
	}
}
public class Test2 {

	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
		People people=(People)ac.getBean("people");
		System.out.println(people);
		
		// 属性注入
		People people2=(People)ac.getBean("people2");
		System.out.println(people2);
		
		// 构造方法注入
		People people3=(People)ac.getBean("people3");
		System.out.println(people3);
		
		People people4=(People)ac.getBean("people4");
		System.out.println(people4);
		
		People people5=(People)ac.getBean("people5");
		System.out.println(people5);
		
		// 工厂方法注入
		People people7=(People)ac.getBean("people7");
		System.out.println(people7);
		
		People people8=(People)ac.getBean("people8");
		System.out.println(people8);
	}
}

<?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="people" class="com.java1234.entity.People"></bean>
	
	<bean id="people2" class="com.java1234.entity.People">
		<property name="id" value="1"></property>
		<property name="name" value="张三"></property>
		<property name="age" value="11"></property>
	</bean>
	
	<bean id="people3" class="com.java1234.entity.People">
		<constructor-arg type="int" value="2"></constructor-arg>
		<constructor-arg type="String" value="李四"></constructor-arg>
		<constructor-arg type="int" value="22"></constructor-arg>
	</bean>
	
	<bean id="people4" class="com.java1234.entity.People">
		<constructor-arg index="0" value="3"></constructor-arg>
		<constructor-arg index="1" value="王五"></constructor-arg>
		<constructor-arg index="2" value="55"></constructor-arg>
	</bean>
	
	<bean id="people5" class="com.java1234.entity.People">
		<constructor-arg index="0" type="int" value="4"></constructor-arg>
		<constructor-arg index="1" type="String" value="招六"></constructor-arg>
		<constructor-arg index="2" type="int" value="66"></constructor-arg>
	</bean>
	
	<bean id="peopleFactory" class="com.java1234.factory.PeopleFactory"></bean>
	
	<bean id="people7" factory-bean="peopleFactory" factory-method="createPeople"></bean>
  
  	<bean id="people8" class="com.java1234.factory.PeopleFactory2" factory-method="createPeople"></bean>
</beans>

阅读更多

没有更多推荐了,返回首页