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. git开发过程的配置和使用

    git开发过程的使用 1.创建仓库 2.新建项目,填写项目名称等信息 3.初始化仓库,创建git仓库 git init 4.配置个人信息(配置过可忽略) git config --global use ...

  2. shell 基本概述

    SHELL的概念 SHELL是一个命令行解释器,它为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序, 用户可以用shell来启动,挂起,停止甚至是编写一些程序. ​ Shell还是 ...

  3. linux command useradd

    Linux command useradd [Purpose]        Learning linux command useradd to create a new user or update ...

  4. node编译C++,比如安装node-gyp失败的问题

    遇到的这个问题是很多需要编译才能运行的node模块共有的问题. npm i -g windows-build-tools 首先以管理员身份打开命令行,然后在命令行下执行这一行命令. 然后重新运行你刚才 ...

  5. memset详解 设置无穷大INF

    memest原型 (please type "man memset" in your shell) void *memset(void *s, int c, size_t n); ...

  6. Centos7搭建软路由

    Xenserver环境: 一:环境准备 内网:192.168.2.100 外网:x.x.x.x 1.1:登陆XenCenter 1.2:进入Xenserver中的Networking选项 1.3:点选 ...

  7. c++的读入txt文件(转)

    因为学姐的项目需要,要用到excel的读入读出,百度过后发现txt的读入读出比较简单,于是,我采用了先把excel转成txt,然后再读入. 方法是csdn上的天使的原地址:   https://blo ...

  8. r-mq实现顺序消费,不重复消费

    根据订单号,同一订单号的消息,会被发送到同一个topic下的同一个queue,发送端的有序,会导致topic中消息的有序,而consumer和queue是一对多?的关系.可以保证topic中的有顺序的 ...

  9. iframe 常见问题 解析

    1. jquery在iframe子页面获取父页面元素代码如下: $("#objid",parent.document) 2. jquery在父页面获取iframe子页面的元素代码如 ...

  10. Android:E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7f9d1b41c0

    这个问题是在测试leakCanaryTestDemo时发现的,期初看到有点蒙,这个demo中只使用了一个button和一个textView控件进行测试,按理说是不应该出现这种问题,在 网上查找这个问题 ...