一、装配一个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 + "]";
} }

spring管理bean的xml: 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="com.cy.test.HelloWorld"></bean> <!-- 装配一个bean -->
<bean id="people" class="com.cy.entity.People"></bean> <!-- 依赖注入 -->
<!-- 属性注入 -->
<bean id="people2" class="com.cy.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.cy.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.cy.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="com.cy.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 id="peopleFactory" class="com.cy.factory.PeopleFactory"></bean>
<bean id="people6" factory-bean="peopleFactory" factory-method="createPeople"></bean> <!-- 工厂方法注入 静态工厂 -->
<bean id="people7" class="com.cy.factory.PeopleFactory2" factory-method="createPeople"></bean>
</beans>

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

非静态工厂: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;
}
}

测试代码:

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

console打印:

峰Spring4学习(2)依赖注入的几种方式的更多相关文章

  1. ASP.NET MVC中使用Unity进行依赖注入的三种方式

    在ASP.NET MVC中使用Unity进行依赖注入的三种方式 2013-12-15 21:07 by 小白哥哥, 146 阅读, 0 评论, 收藏, 编辑 在ASP.NET MVC4中,为了在解开C ...

  2. spring4之依赖注入的三种方式

    1.Setter注入 <bean id="helloWorld" class="com.jdw.spring.beans.HelloWorld"> ...

  3. Spring中依赖注入的四种方式

    在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入  这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式). 使用属性的sett ...

  4. Spring IOC 依赖注入的两种方式XML和注解

    依赖注入的原理 依赖注入的方式---XML配置 依赖注入的方式---注解的方式 Spring 它的核心就是IOC和AOP.而IOC中实现Bean注入的实现方式之一就是DI(依赖注入). 一 DI的原理 ...

  5. 在ASP.NET MVC中使用Unity进行依赖注入的三种方式

    在ASP.NET MVC4中,为了在解开Controller和Model的耦合,我们通常需要在Controller激活系统中引入IoC,用于处理用户请求的 Controller,让Controller ...

  6. Spring依赖注入的三种方式

    看过几篇关于Spring依赖注入的文章,自己简单总结了一下,大概有三种方式: 1.自动装配 通过配置applicationContext.xml中的标签的default-autowire属性,或者标签 ...

  7. SSH深度历险记(八) 剖析SSH核心原则+Spring依赖注入的三种方式

           于java发育.一类程序猿必须依靠类的其他方法,它是通常new依赖类的方法,然后调用类的实例,这样的发展问题new良好的班统一管理的例子.spring提出了依赖注入的思想,即依赖类不由程 ...

  8. Spring注解依赖注入的三种方式的优缺点以及优先选择

    当我们在使用依赖注入的时候,通常有三种方式: 1.通过构造器来注入: 2.通过setter方法来注入: 3.通过filed变量来注入: 那么他们有什么区别吗?应该选择哪种方式更好? 三种方式的区别小结 ...

  9. SSH深度历险(八) 剖析SSH核心原理+Spring依赖注入的三种方式

           在java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依 ...

随机推荐

  1. bzoj1081

    题解: 先暴力找规律 然后就一加一减的枚举 代码: #include<bits/stdc++.h> using namespace std; ],p[]; int main() { sca ...

  2. String比较相等的问题探索

    String比较相等的问题探索 工作上,有个同事犯了个低级错误,把字符串的计较用了==.由于代码已经交付客户,上了生产环境,给公司带了了损失.于是看了他的代码,自己根据以前学的知识,写了几个小demo ...

  3. Python Django 之 Views HttpRequest HttpReponse

    一.Python Django 之 Views 数据交互 http请求中产生两个人核心对象: http请求:HttpRequest对象 http响应:HttpReponse对象 所在位置django. ...

  4. maven install deploy

    1.安装到本地仓库 install jar to local fs mvn .jar -DgroupId=com.bonc -DartifactId=licenseVerify-.jar -Dvers ...

  5. ansible with_subelements

    with_subelements 循环列表中的子元素 (意想不到的地方会用到) --- - hosts: web tasks: - authorized_key: "user={{ item ...

  6. JQuery, Silverlight 公用WCF

    WCF web.config配置: <?xml version="1.0"?> <configuration> <system.web> < ...

  7. ubuntu16 intellij idea install lombok plugin

    项目中用到lombok,idea会出现类似编译报错的红色,但并不影响运行.所以为了没有类似警告,就在idea上安装lombok插件.file-settings 安装完成之后,按照提示重启idea,问题 ...

  8. CTF-练习平台-Misc之 Linux基础1

    十四.Linux基础1 下载打开文件,解压后发下是一个没有后缀名的文件,添加后缀名为txt,搜索关键词“KEY”,发现flag Linux???不存在的!

  9. ppt正文排版

  10. Android USB gadget框架学习笔记

    一 Gadget框架结构 kernel/drivers/usb/gadget,这个目录是android下usbgadget的主要目录. Gadget功能组织单元:主要文件android.c,usb g ...