一、装配一个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. python 复制/移动文件

    用python实现将某代码文件复制/移动到指定路径下. 场景例如:mv ./xxx/git/project1/test.sh ./xxx/tmp/tmp/1/test.sh (相对路径./xxx/tm ...

  2. MyEclipse WebSphere开发教程:WebSphere 8安装指南(二)

    [周年庆]MyEclipse个人授权 折扣低至冰点!立即开抢>> [MyEclipse最新版下载] IBM为使用WebSphere测试应用程序的开发人员提供了免费的WebSphere Ap ...

  3. JAVA中int转string及String.valueOf()的使用

    日常java开放中,经常会遇到int和String的互转,一般图省事的做法就是: String length = ""+100; length的生成需要使用两个临时字符串" ...

  4. jQuery trigger()以及注意事项

    trigger() 方法触发被选元素的指定事件类型.例如: $('#pcId').trigger("change") ;//触发id为“pcId”的select的 change事件 ...

  5. WCF异常相关

    1.端口没打开 解决办法: services.msc 启动Net.Tcp Port Sharing Service 2.由于访问被拒,服务终结点未能侦听 URI“net.tcp://localhost ...

  6. 【论文解读】行人检测:What Can Help Pedestrian Detection?(CVPR'17)

    前言 本篇文章出自CVPR2017,四名作者为Tsinghua University,Peking University, 外加两名来自Megvii(旷视科技)的大佬. 文章中对能够帮助行人检测的ex ...

  7. CodeForces - 547D: Mike and Fish (转化为欧拉回路)(优化dfs稠密图)(定向问题)

    As everyone knows, bears love fish. But Mike is a strange bear; He hates fish! The even more strange ...

  8. HDU 1233:还是畅通工程(最小生成树)

    还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  9. 使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(三)-- Logger

    本篇是在上一篇的基础上添加日志功能,并记录NLog在Asp.Net Core里的使用方法. 第一部分:默认Logger支持 一.project.json添加日志包引用,并在cmd窗口使用 dotnet ...

  10. 当php版本为5.6时的提示信息解决方法

    ecshop修饰符preg_replace/e不安全的几处改动 Strict standards: Only variables should be passed by reference in D: ...