博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
峰Spring4学习(2)依赖注入的几种方式
阅读量:6471 次
发布时间:2019-06-23

本文共 2967 字,大约阅读时间需要 9 分钟。

一、装配一个bean

二、依赖注入的几种方式

com.cy.entity   People.java:

package com.cy.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 + "]";    }        }
View Code

spring管理bean的xml: beans.xml:

com.cy.factory下的

静态工厂:PeopleFactory2.java:

package com.cy.factory;import com.cy.entity.People;public class PeopleFactory2 {    public static People createPeople(){        People people = new People();        people.setId(6);        people.setName("小八");        people.setAge(88);        return people;    }}
View Code

非静态工厂:PeopleFactory.java:

package com.cy.factory;import com.cy.entity.People;public class PeopleFactory {    public People createPeople(){        People people = new People();        people.setId(5);        people.setName("小七");        people.setAge(77);        return people;    }}
View Code

 

测试代码:

package com.cy.service;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.cy.entity.People;public class Test {    @SuppressWarnings("resource")    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 people6 = (People) ac.getBean("people6");        System.out.println(people6);                //工厂方法注入 静态工厂        People people7 = (People) ac.getBean("people7");        System.out.println(people7);            }}
View Code

console打印:

转载于:https://www.cnblogs.com/tenWood/p/6707005.html

你可能感兴趣的文章
2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem D. Distance 迪杰斯特拉
查看>>
ASP.NET Core 中文文档 第四章 MVC(01)ASP.NET Core MVC 概览
查看>>
PHP函数之日期时间函数date()使用详解
查看>>
easyui中datagrid用法,加载table数据与标题
查看>>
【Consul】 分布式环境中的服务注册和发现利器
查看>>
G2 2.0 更灵活、更强大、更完备的可视化引擎!
查看>>
CSS Pseudo-Element Selectors伪对象选择符
查看>>
路由器wan口ip地址显示0.0.0.0怎么办
查看>>
iOS完美的网络状态判断工具
查看>>
ios-NSString总结
查看>>
python虚拟环境virtualenv的安装与使用
查看>>
【C#/WPF】UI控件的拖拽/拉伸
查看>>
javaScript实现归并排序
查看>>
kickstart文件详解
查看>>
lua——string之string.gsub
查看>>
JS中的跨域问题
查看>>
MySQL 8 新特性之持久化全局变量的修改
查看>>
Docker Tag
查看>>
ZOJ 2459 Pyramids
查看>>
activemq自己配置安装过程
查看>>