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

最新文章

  1. SQL Server 监控统计阻塞脚本信息
  2. phpcms v9中调用栏目及调用多个子栏目中的文章列表
  3. PHP5各个版本的新功能和新特性总结(转载 http://www.jb51.net/article/48150.htm)
  4. Project Euler 107:Minimal network 最小网络
  5. oracle11g 远程登录数据库
  6. Java反射 - 2(对象复制,父类域,内省)
  7. 含服务端,客户端,数据库的注册/登录/聊天/在线/离线查看的聊天demo
  8. 如何创建一个标准的Windows服务
  9. 前端接口自动化测试工具-DOClever使用介绍(转载)
  10. sqli-labs:1-4,基于报错的注入
  11. POJ 1149 - PIGS - [最大流构图]
  12. NERDTree基本使用教程
  13. 添加List集合覆盖问题
  14. 关于IScroll使用中的常见问题与解决方案
  15. 爬虫学习之-git拉取远程错误
  16. [51nod1847]奇怪的数学题
  17. OPCDAAuto.dll的C#使用方法浅析
  18. SQL Server 2012自动备份
  19. 认识HTML中文本、图片、链接标签和路径
  20. 找出数字数组中最大的元素(使用Math.max函数)

热门文章

  1. Sqlserver常用基础语句
  2. office 32-bit components 2010 的卸载
  3. MapReduce作业的工作原理
  4. 使用pgrouting进行最短路径搜索
  5. postgresql清理工具
  6. solr中facet及facet.pivot理解(整合两篇文章保留参考)
  7. Postman A请求的返回值作为B请求的入参( 之‘’token‘’ ,用代码设置全局变量)
  8. mybaties插件生成代码
  9. django中ajax的使用以及避开CSRF 验证的方式
  10. Android 开发 实现文本搜索功能