Java依赖注入方式
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依赖注入方式的更多相关文章
- 一步一步深入spring(3)--spring的依赖注入方式
对于spring配置一个bean时,如果需要给该bean提供一些初始化参数,则需要通过依赖注入方式,所谓的依赖注入就是通过spring将bean所需要的一些参数传递到bean实例对象的过程,sprin ...
- java依赖注入(injection)
和SpringSource分别通过其开源项目Guice及Spring Framework提供了依赖注入的功能.然而直到现在开发者也没有一种标准的.独立于供应商的方式从而无需修改其源文件就能在这些框架之 ...
- 深入浅出spring IOC中三种依赖注入方式
深入浅出spring IOC中三种依赖注入方式 spring的核心思想是IOC和AOP,IOC-控制反转,是一个重要的面向对象编程的法则来消减计算机程序的耦合问题,控制反转一般分为两种类型,依赖注入和 ...
- Java 依赖注入标准(JSR-330)简介
作者:88250 ,Vanessa 时间:2009 年 11 月 19 日 Java 依赖注入标准(JSR-330,Dependency Injection for Java)1.0 规范已 ...
- spring四种依赖注入方式(转)
spring四种依赖注入方式!! 平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提 ...
- Spring_002 依赖注入方式实现
继续写我们的第一个Spring程序,这次我们使用依赖注入的方式实现程序 第一步,建立我们的Spring_002程序,并在程序中添加BookDao.java.BookDaoImpl.java.BookS ...
- 给力啊!这篇Spring Bean的依赖注入方式笔记总结真的到位,没见过写的这么细的
1. Bean的依赖注入概念 依赖注入(Dependency Injection):它是 Spring 框架核心 IOC 的具体实现.在编写程序时,通过控制反转,把对象的创建交给了 Spring,但是 ...
- 控制反转IOC的依赖注入方式
引言: 项目中遇到关于IOC的一些内容,因为和正常的逻辑代码比较起来,IOC有点反常.因此本文记录IOC的一些基础知识,并附有相应的简单实例,而在实际项目中再复杂的应用也只是在基本应用的基础上扩展而来 ...
- 转:深入浅出spring IOC中四种依赖注入方式
转:https://blog.csdn.net/u010800201/article/details/72674420 深入浅出spring IOC中四种依赖注入方式 PS:前三种是我转载的,第四种是 ...
随机推荐
- 【Java算法】输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数
import java.util.Scanner; public class CountZimuShuzi { public static void main(String[] args) { Sys ...
- 【Oracle安装卸载】oracle卸载
Oracle卸载比较麻烦,不能简单卸载就完成了,有时没有卸载完整,下次安装不能很好的安装: 当然Oracle卸载也没有那么难,只是步骤比较多.Oracle10g还是Oracle11g卸载步骤都是一样的 ...
- Unity中UGUI之Canvas属性解读版本二
Canvas Render Modes(渲染模式) 1.在screen空间中渲染2.在world空间中渲染 Screen Space-Overlay 在这个渲染模式中,UI元素将在场景的上面.如果场景 ...
- 对象关系_many2many
jdbc: package demo.test.many2many; import java.util.HashSet; import java.util.Set; public class Stud ...
- nginx:负载均衡(三)分流策略
[1]轮询策略.轮询策略是最简单的策略,无脑配置,不考虑服务器的访问的能力.每个请求按照时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除 upstream www.daysn. ...
- (C/C++学习笔记) 二. 数据类型
二. 数据类型 ● 数据类型和sizeof关键字(也是一个操作符) ※ 在现代半导体存储器中, 例如在随机存取存储器或闪存中, 位(bit)的两个值可以由存储电容器的两个层级的电荷表示(In mode ...
- error: illegal character '\ufeff' 的解决方案
今天使用scalac 命令编译scala文件的时候,出错了,出现如下错误提示:
- FindResource () RT_HTML 为什么总是出错呢 ?
#include <windows.h> #include <commdlg.h> #include <ole2.h> BOOL GetHtmlResource(L ...
- netty源码理解(二) serverstrap.bind()
eventloop是一个线程,里面有一个executor封装了一个线程工厂,在启动的时候启动一个线程,传入的实现了runnable的内部类,里面调用了eventloop的run方法.
- day 67 django 之ORM 基础安装
一 ORM的基础部分 1 ORM的概念 对象关系映射(Object Relational Mapping(映射),简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 2 ...