pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.wzh</groupId>
<artifactId>Inject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>Inject</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.5.RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.5.RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.5.RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.5.RELEASE</version>
</dependency> </dependencies>
</project>

构造方法注入

application.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 = "Orange" class="com.wzh.fruit.impl.Orange"></bean>
<bean id = "Apple" class="com.wzh.fruit.impl.Apple"></bean> <bean id = "personApple" class="com.wzh.person.Person">
<constructor-arg ref="Apple"></constructor-arg>
</bean> <bean id = "personOrange" class="com.wzh.person.Person">
<constructor-arg ref="Orange"></constructor-arg>
</bean> </beans>

Fruit.java

package com.wzh.fruit;

public interface Fruit {
public String getFruit();
}

Apple.java

package com.wzh.fruit.impl;

import com.wzh.fruit.Fruit;

public class Apple implements Fruit{

    public Apple() {

    }

    public String getFruit() {
String apple = "apple";
return apple;
} }

Orange.java

package com.wzh.fruit.impl;

import com.wzh.fruit.Fruit;

public class Orange implements Fruit{

    public Orange() {

    }

    public String getFruit() {
String orange = "orange";
return orange;
} }

Person.java

package com.wzh.person;

import java.lang.reflect.Constructor;

import com.wzh.fruit.Fruit;

public class Person {

    private Fruit fruit;

    public Person(Fruit _fruit) {
fruit = _fruit;
} public void eat() {
System.out.println("I want eat "+fruit.getFruit());
} }

Run.java

package com.wzh.run;

import com.wzh.person.Person;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Run { public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("application.xml");
Person p =(Person)ac.getBean("personOrange");
p.eat();
} }

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

setter注入

application.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 = "Orange" class="com.wzh.fruit.impl.Orange"></bean>
<bean id = "Apple" class="com.wzh.fruit.impl.Apple"></bean>
<bean id = "Watermelon" class="com.wzh.fruit.impl.Watermelon"></bean> <bean id = "personApple" class="com.wzh.person.Person">
<property name="Fruit" ref="Apple"></property>
</bean> <bean id = "personOrange" class="com.wzh.person.Person">
<property name="Fruit" ref="Orange"></property>
</bean> <bean id = "personWatermelon" class="com.wzh.person.Person">
<property name="Fruit" ref="Watermelon"></property>
</bean> </beans>

Person.java

package com.wzh.person;

import java.lang.reflect.Constructor;

import com.wzh.fruit.Fruit;

public class Person {

    private Fruit fruit;

    public void setFruit(Fruit _fruit) {
fruit = _fruit;
} public void eat() {
System.out.println("I want eat "+fruit.getFruit());
} }

Run.java

package com.wzh.run;

import com.wzh.person.Person;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Run { public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("application.xml");
Person p =(Person)ac.getBean("personWatermelon");
p.eat();
} }

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

注解

application.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config />
<context:component-scan base-package="com.wzh.*">
</context:component-scan> </beans>

Apple.java

package com.wzh.fruit.impl;

import org.springframework.stereotype.Component;

import com.wzh.fruit.Fruit;

@Component("Apple")
public class Apple implements Fruit{ public String getFruit() {
String apple = "apple";
return apple;
} }

Orange.java

package com.wzh.fruit.impl;

import org.springframework.stereotype.Component;

import com.wzh.fruit.Fruit;

@Component("Orange")
public class Orange implements Fruit{ public String getFruit() {
String orange = "orange";
return orange;
} }

Person.java

package com.wzh.person;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component; import com.wzh.fruit.Fruit; @Component("Person")
public class Person { @Autowired
@Qualifier("Apple")
private Fruit fruit; public void eat() {
System.out.println("I want eat "+fruit.getFruit());
} }

Run.java

package com.wzh.run;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wzh.person.Person; public class Run { public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("application.xml");
Person p =(Person)ac.getBean("Person");
p.eat();
} }

Java依赖注入方式的更多相关文章

  1. 一步一步深入spring(3)--spring的依赖注入方式

    对于spring配置一个bean时,如果需要给该bean提供一些初始化参数,则需要通过依赖注入方式,所谓的依赖注入就是通过spring将bean所需要的一些参数传递到bean实例对象的过程,sprin ...

  2. java依赖注入(injection)

    和SpringSource分别通过其开源项目Guice及Spring Framework提供了依赖注入的功能.然而直到现在开发者也没有一种标准的.独立于供应商的方式从而无需修改其源文件就能在这些框架之 ...

  3. 深入浅出spring IOC中三种依赖注入方式

    深入浅出spring IOC中三种依赖注入方式 spring的核心思想是IOC和AOP,IOC-控制反转,是一个重要的面向对象编程的法则来消减计算机程序的耦合问题,控制反转一般分为两种类型,依赖注入和 ...

  4. Java 依赖注入标准(JSR-330)简介

    作者:88250 ,Vanessa 时间:2009 年 11 月 19 日      Java 依赖注入标准(JSR-330,Dependency Injection for Java)1.0 规范已 ...

  5. spring四种依赖注入方式(转)

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

  6. Spring_002 依赖注入方式实现

    继续写我们的第一个Spring程序,这次我们使用依赖注入的方式实现程序 第一步,建立我们的Spring_002程序,并在程序中添加BookDao.java.BookDaoImpl.java.BookS ...

  7. 给力啊!这篇Spring Bean的依赖注入方式笔记总结真的到位,没见过写的这么细的

    1. Bean的依赖注入概念 依赖注入(Dependency Injection):它是 Spring 框架核心 IOC 的具体实现.在编写程序时,通过控制反转,把对象的创建交给了 Spring,但是 ...

  8. 控制反转IOC的依赖注入方式

    引言: 项目中遇到关于IOC的一些内容,因为和正常的逻辑代码比较起来,IOC有点反常.因此本文记录IOC的一些基础知识,并附有相应的简单实例,而在实际项目中再复杂的应用也只是在基本应用的基础上扩展而来 ...

  9. 转:深入浅出spring IOC中四种依赖注入方式

    转:https://blog.csdn.net/u010800201/article/details/72674420 深入浅出spring IOC中四种依赖注入方式 PS:前三种是我转载的,第四种是 ...

随机推荐

  1. Android--Android Studio 打开ADM报错

    Android studio无法打开类似与eclipse的DDMS, 在android studio里点击android device monitor(点击菜单栏里的Tools->Android ...

  2. python 自然语言处理(六)____N-gram标注

    1.一元标注器(Unigram Tagging) 一元标注器利用一种简单的统计算法,对每个标注符分配最有可能的标记.例如:它将分配标记JJ给词frequent,因为frequent用作形容词更常见.一 ...

  3. day04流程控制之while循环

    流程控制之while循环 1.什么是while循环 循环指的是一个重复做某件事的过程 2.为何有循环 为了让计算机能像人一样重复 做某件事 3.如何用循环 ''' # while循环的语法:while ...

  4. Values & Reference:值和引用

    var a = 2; var b = a; //b 是 a 的值的一个副本 b++; a; b; var c = [1, 2, 3]; var d = c; // d 是 值[1, 2, 3]的一个引 ...

  5. unity中制作模拟第一人称视角下的指南针

    private int zRotation; public GameObject obj; public void Update() { //obj = GameObject.Find("C ...

  6. nginx;keepalived配置出现主主的解决方法(脑裂问题)

    1.查看日志 tail -f /var/log/messages 发现master和backup机都是mastaer模式启动的 通过查看别人的经历,发现VRRP基于报文实现的.master设置一定时间 ...

  7. vector容器的注意事项

    1.容器是指对象的集合,每一个元素都是一个对象,并且对象的类型相同.可以使用索引去访问容器中的对象. 2.由于容器中存放的是对象,所以引用无法成为vector的成员. 3.容器的初始化,与string ...

  8. Python实战之logging模块使用详解

    用Python写代码的时候,在想看的地方写个print xx 就能在控制台上显示打印信息,这样子就能知道它是什么了,但是当我需要看大量的地方或者在一个文件中查看的时候,这时候print就不大方便了,所 ...

  9. 用FPGA对ASIC进行原型验证的过程(转)

    用FPGA对ASIC进行原型验证的过程   reference:http://xilinx.eetrend.com/d6-xilinx/article/2018-10/13736.html     鉴 ...

  10. leetcode第11题:盛水最多的容器

    给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...