下面展示spring的注入方式,主要是:

两个接口:

Axe.java:

package com.jim.service;

public interface Axe {

public String chop();

}

接口二:Person.java:

package com.jim.service;

public interface Person {

public void useAxe();

}

接口实现类:

package com.jim.service.impl;

import com.jim.service.Axe;

public class StoneAxe implements Axe {

public String chop() {

return "石头斧头";

}

}

1.设值注入:

在Person实现类Man的实现上:

package com.jim.service.impl;

import com.jim.service.Axe;

import com.jim.service.Person;

public class Man implements Person {

private Axe axe;

public void setAxe(Axe axe){

this.axe = axe;

}

public void useAxe() {

System.out.println(axe.chop());

}

}

2.构造注入:

package com.jim.service.impl;

import com.jim.service.Axe;

import com.jim.service.Person;

public class Man implements Person {

private Axe axe;

public void Man(){}

public Man(Axe axe) {

super();

this.axe = axe;

}

public void useAxe() {

System.out.println(axe.chop());

}

}

同时spring的配置文件bean.xml的区别是:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://www.springframework.org/schema/beans"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!--

设值注入的配置方式

<bean id="man" class="com.jim.service.impl.Man">

<property name="axe" ref="steelAxe"/>

</bean>

-->

<!--构造注入的方式-->

<bean name="man" class="com.jim.service.impl.Man">

<constructor-arg ref="steelAxe"/>

</bean>

<bean id="stoneAxe" class="com.jim.service.impl.StoneAxe" />

<bean id="steelAxe" class="com.jim.service.impl.SteelAxe"/>

</beans>

测试类:

package com.jim.service.test;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jim.service.Person;

public class BeanTest {

@Test

public void testPerson(){

ApplicationContext actx = new ClassPathXmlApplicationContext("bean.xml");

Person person = actx.getBean("man", Person.class);

person.useAxe();

}

}

转载于:https://blog.51cto.com/ywj852752270/1283385

spring的注入方式相关推荐

  1. Spring 依赖注入方式详解

    平常的Java开发中,程序员在某个类中需要依赖其它类的方法. 通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理. Spring提出了依赖注入的思想,即依赖类不由 ...

  2. Spring的注入方式详解

    [html] view plaincopy Spring的注入方式详解 Spring有三个注入方式,type1,type2,type3 type1  接口依赖 type2  setter/getter ...

  3. 最全的 Spring 依赖注入方式,你都会了吗?

    欢迎关注方志朋的博客,回复"666"获面试宝典 前言 Spring 正如其名字,给开发者带来了春天,Spring 是为解决企业级应用开发的复杂性而设计的一款框架,其设计理念就是:简 ...

  4. Spring依赖注入方式

    一.依赖注入(DI)简介     依赖注入背后的基本原理是对象之间的依赖关系,可以通过以下几种方式来实现:构造器的参数.工厂方法的参数,或给由构造函数或者工厂方法创建的对象设置属性.因此,容器的工作就 ...

  5. Spring bean注入方式

    版权声明:本文为博主原创文章,如需转载请标注转载地址. 博客地址:http://www.cnblogs.com/caoyc/p/5619525.html  Spring bean提供了3中注入方式:属 ...

  6. java bean spring_Java+Spring+Bean+注入方式

    1.首先准备共享文件 调用方法Client端Client.java: import org.springframework.context.support.ClassPathXmlApplicatio ...

  7. spring中注入方式

    URL:https://blog.csdn.net/d124939312/article/details/79022984 注入简介 Spring注入可以理解为是对一个对象进行初始化,也就是省去new ...

  8. spring 内部类注入方式

    一个DAO中既要继承HibernateDaoSupport也要继承SqlMapClientDaoSupport,即:hibernate和ibatis同时使用.内部类可以解决多继承的问题,但是如何注入内 ...

  9. Spring IOC 注入方式

    2019独角兽企业重金招聘Python工程师标准>>> 1. setter注入. 2. 构造函数注入. 3.工厂方法注入. 4.自动注入. 转载于:https://my.oschin ...

最新文章

  1. 北理工计算机博士怎么样,北京理工大学在职博士的含金量怎么样
  2. Hadoop1.2.1集群安装三
  3. 笨办法学C 练习28:Makefile 进阶
  4. 设计模式--代理模式--Java实现
  5. python猴子补丁_Python猴子补丁Monkey Patch用法实例解析
  6. Google Hack
  7. VScode修改html代码后,浏览器页面更新不及时
  8. 虾皮面试真题:虾皮后端15连问!
  9. 计算机图形学原理及实践第三版pdf,计算机图形学原理及实践 英文第3版
  10. python怎么解压rar文件_Python解压 rar、zip、tar文件的方法
  11. js中如何判断一个数是不是素数(三种方法)
  12. 实验八:Winwebmail的搭建
  13. datetime用法
  14. 数据工程指南高级技能:处理框架
  15. js过滤出对象中想要的数据
  16. HP Superdome定位网卡位置并更换
  17. Linux启动过程详解
  18. 贝加莱驱动器电源模块维修8B0C0320HW00.002-1
  19. 述职报告PPT怎么做?
  20. 汉威科技气体监测报警系列产品守护油气设施安全

热门文章

  1. sublime text3 sql 格式化
  2. VB取得TextBox、RichTextBox光标所在的行和列(支持汉字)
  3. 从零学ELK系列(一):为什么要跟我学从零学ELK系列
  4. 三行代码实时追踪你的手,只要有浏览器就够了 | Demo·代码
  5. 融完5亿美元创始人出局?腾讯投资的明星无人车公司Zoox地震了
  6. 特斯拉奔驰宝马沃尔沃团灭!安全的半自动驾驶?不存在的
  7. AI发来贺电,您的2333号奶牛已进入恋爱时节 | 野性的呼唤
  8. 特别看好高校团队的联想创投,在中科大拉开高校AI精英挑战赛大幕
  9. SpringBoot基础教程1-1-2 配置文件介绍
  10. 《用户网络行为画像》读书笔记(二)