Spring第三讲

package com.java1234.entity;

public class People {
	
	private int id;
	private String name;
	private int 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;
	}
	
	
}

<?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>
	
</beans>

注入属性

<?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>
	
</beans>
		//属性注入
		People people2=(People)ac.getBean("people2");
		System.out.println(people2);
	}

构造函数

<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>
//构造方法注入
		People people3=(People)ac.getBean("people3");
		System.out.println(people3);
	}

索引

	<bean id="people4" class="com.java1234.entity.People">
		<constructor-arg index="0" value="2"></constructor-arg>
		<constructor-arg index="1" value="李四"></constructor-arg>
		<constructor-arg index="2" value="22"></constructor-arg>
	</bean>

联合使用

	<bean id="people5" class="com.java1234.entity.People">
		<constructor-arg index="0" type="int" value="2"></constructor-arg>
		<constructor-arg index="1" type="String" value="李四"></constructor-arg>
		<constructor-arg index="2" type="int" value="22"></constructor-arg>
	</bean>
	
People people5=(People)ac.getBean("people5");
		System.out.println(people5);

工厂注入

package com.java1234.factory;

import com.java1234.entity.People;

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

<bean id="peopleFactory" class="com.java1234.factory.PeopleFactory"></bean>
	
<bean id="people7" factory-bean="peopleFactory" factory-method="createPeople"></bean>

//工厂方法注入
		People people7=(People)ac.getBean("people7");
		System.out.println(people7);
		

静态

package com.java1234.factory;

import com.java1234.entity.People;

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

		
		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="2"></constructor-arg>
		<constructor-arg index="1" value="李四"></constructor-arg>
		<constructor-arg index="2" value="22"></constructor-arg>
	</bean>
	
	
	<bean id="people5" class="com.java1234.entity.People">
		<constructor-arg index="0" type="int" value="2"></constructor-arg>
		<constructor-arg index="1" type="String" value="李四"></constructor-arg>
		<constructor-arg index="2" type="int" value="22"></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>
package com.java1234.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.java1234.entity.People;

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);
	}
}

你可能感兴趣的:(Spring,spring,chrome,java)