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. php删除文件夹下面所有文件包括(删除文件夹)不删除文件夹

    function deldir($dir) { //先删除目录下的文件: $dh=opendir($dir); while ($file=readdir($dh)) { if($file!=" ...

  2. hdu1695

    题解: 莫比乌斯反演 设f[i]=Σgcd(i,j)%z==0 则f[i]=Σgcd(i,j)==zd 成莫比乌斯反演关系 代码: #include<cstdio> #include< ...

  3. delphi 演示数据路径

    链接里默认的--------------------------- Error --------------------------- I/O error for file "C:\Prog ...

  4. sqlalchemy tree 树形分类 无限极分类的管理。预排序树,左右值树。sqlalchemy-mptt

    简介: 无限极分类是一种比较常见的数据格式,生成组织结构,生成商品分类信息,权限管理当中的细节权限设置,都离不开无限极分类的管理. 常见的有链表式,即有一个Pid指向上级的ID,以此来设置结构.写的时 ...

  5. DevExpress WinForms v18.2新版亮点(二)

    行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress WinForms v1 ...

  6. Java Web相关概念调查

  7. codeforces1003D(贪心)

    D. Coins and Queries time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  8. Day4作业及默写

    1,写代码,有如下列表,按照要求实现每一个功能 li = ["alex", "WuSir", "ritian", "barry&q ...

  9. scrapy中自动补全url

    url = "https:" + url 或者url = response.urljoin(url) #这里代表的是自动补全url

  10. Python 类变量

    class Person: country = "大清" def __init__(self, name, gender): self.name = name self.gende ...