spring ioc---依赖注入
spring ioc:控制反转,或叫依赖注入
简单的bean装配:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import test.HelloWorld;
public class Test {
public static void main(String[] args) {
//获取被代理对象的路径
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
//获取被代理对象实例
HelloWorld helloWorld=(HelloWorld)ac.getBean("helloWorld");
//执行被代理对象的方法
helloWorld.say();
}
}
public class HelloWorld {
public void say(){
System.out.println("Spring4大爷你好!");
}
}
beans.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="helloWorld" class="test.HelloWorld"></bean>
</beans>
依赖注入:
1,属性注入;
2,构造函数注入;(通过类型;通过索引;联合使用)
3,工厂方法注入;(非静态工厂,静态工厂)
4,泛型依赖注入;(Spring4 整合Hibernate4 的时候顺带讲)
package entity;
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 + "]";
}
}
package factory;
import entity.People;
public class PeopleFactory {
public People createPeople(){
People p=new People();
p.setId(5);
p.setName("小七");
p.setAge(77);
return p;
}
}
package factory;
import entity.People;
public class PeopleFactory2 {
public static People createPeople(){
People p=new People();
p.setId(8);
p.setName("小八");
p.setAge(88);
return p;
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import 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);
}
}
<?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="entity.People"></bean>
<!-- 属性注入 -->
<bean id="people2" class="entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
</bean>
<!-- 有参构造方法注入,通过类型 -->
<bean id="people3" class="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="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="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 -->
<bean id="peopleFactory" class="factory.PeopleFactory"></bean>
<!-- 指定某个工厂的某个非静态方法,创建对象 ->
<bean id="people7" factory-bean="peopleFactory" factory-method="createPeople"></bean>
<!-- 静态的工厂不需要创建工厂bean -->
<bean id="people8" class="factory.PeopleFactory2" factory-method="createPeople"></bean>
</beans>