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:前三种是我转载的,第四种是 ...
随机推荐
- memory prefix inter,intra,intro,iso out 5
1● inter 在~之间:相互 2● intra 3● iso 等 同 4● intro 向内,在内,内部
- Linux安装Nginx报错: ./configure: error: C compiler cc is not found
CentOS 7.3 下 安装 Nginx 执行配置命令 ./configure 时提示以下错误: checking for OS + Linux 2.6.32-431.el6.x86_64 x86_ ...
- Java Web(十) 分页功能
分页 分页的使用非常普遍,现在一步步的把分页功能实现出来,先看看已经写好的效果: 该页面的所有数据都存放在一个javaBean对象(PageBean)里,每次访问该页面时,Serlvet就会把page ...
- Win10系列:UWP界面布局基础2
属性设置 在面向对象程序开发中,所提及的属性通常指的是对象的属性.在XAML代码中,定义元素时也可以为其设置属性,例如对于一个TextBox元素,有背景属性.宽度属性和高度属性等.为了满足实际应用的需 ...
- July_One_Week—linked list
#include <stdio.h> #include <stdlib.h> typedef struct linklist { unsigned int count; str ...
- 8188EU 在AM335X MC183上以AP+STA工作
[目的] 8188EU 在AM335X MC183上以AP+STA工作. [环境] 1. Ubuntu 16.04发行版 2. linux-3.2.0-psp04.06.00.11 3. MC1 ...
- python之路-网络基础
1.什么是网络: 通过网络设备将各个设备连接在一起,使用协议让设备之间可以通信,共享资源,这些组成了一个网络. 2.osi七层模式: 国际标准化组织(ISO)创建OSI(开放系统互联)参考模型,希望不 ...
- bzoj1717
题解: 二分答案 然后hash 代码: #include<bits/stdc++.h> using namespace std; ,P2=,P=; int a1[P],num[P],a2[ ...
- Oracle数据库备份策略:全备与增量备份
一.RMAN全备份 在数据量比较小.或者数据库服务器性能很强大的情况下,可以每天进行一次全备份. 全被策略如下 1.crontab定时任务,避开业务繁忙时段 ##################### ...
- Top k问题的讨论(三种方法的java实现及适用范围)
在很多的笔试和面试中,喜欢考察Top K.下面从自身的经验给出三种实现方式及实用范围. 合并法 这种方法适用于几个数组有序的情况,来求Top k.时间复杂度为O(k*m).(m:为数组的个数).具体实 ...