Sping4之依赖注入

Spring的依赖注入可以是我们不需要去管理bean,网上看到一个回答很精辟:

现在你需要买一把锤子:

1、自己做一把,解释成java就是,调用者创建被调用着,也就是自己去创造一个造锤子的方法,然后自己调用;

2、自己找到生产锤子的工厂,然后问工厂买。就是工厂模式;

3、可以打电话给商店,让商店给自己送一把锤子过来,这就是sping的依赖注入;

第一种方法调用者创建被调用者,两者之前无法实现松耦合;

第二种方法调用者无须关心被调用者具体实现过程,只需要找到符合某种标准(接口)的实例,即可使用;

第三种调用者无须自己定位工厂,程序运行到需要被调用者时,系统自动提供被调用者实例。调用者和被调用者通过spring管理。他们的关系由spring维护

model

package com.hongcong.model;

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 + "]";
} }

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="people1" class="com.hongcong.model.People">
<property name="id" value="1"/>
<property name="name" value="小一"/>
<property name="age" value="11"/>
</bean> <!-- 构造方法类型注入 -->
<bean id="people2" class="com.hongcong.model.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="people3" class="com.hongcong.model.People">
<constructor-arg index="0" value="3"></constructor-arg>
<constructor-arg index="1" value="小三"></constructor-arg>
<constructor-arg index="2" value="33"></constructor-arg>
</bean>
<!-- 工厂注入 -->
<bean id="peopleFactory" class="com.hongcong.factory.PeopleFactory"></bean>
<bean id="people4" factory-bean="peopleFactory" factory-method="CreatePeople"></bean>
<bean id="people5" class="com.hongcong.factory.PeopleFactory" factory-method="CreatePeopleByStatic"></bean>
</beans>

执行类

package com.hongcong.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hongcong.model.People;

public class test {
public static void main(String[] args) {
ClassPathXmlApplicationContext ca = new ClassPathXmlApplicationContext("beans.xml");
//属性注入
People people1 = (People)ca.getBean("people1");
System.out.println(people1); //构造方法type注入
People people2 = (People)ca.getBean("people2");
System.out.println(people2); //构造方法顺序注入
People people3 = (People)ca.getBean("people3");
System.out.println(people3); //非静态工厂
People people4 = (People)ca.getBean("people4");
System.out.println(people4); //静态工厂注入
People people5 = (People)ca.getBean("people5");
System.out.println(people5);
}
}

工厂

package com.hongcong.factory;

import com.hongcong.model.People;

public class PeopleFactory {
public People CreatePeople(){
People people = new People();
people.setId(4);
people.setName("小四");
people.setAge(44);
return people;
} public static People CreatePeopleByStatic(){
People people = new People();
people.setId(5);
people.setName("小五");
people.setAge(55);
return people; }
}
上一篇:使用VS2019 + .net 6创建 webapi 项目


下一篇:windows中Redis简单使用