spring 常用的注入方式有哪些?
spring 常用的注入方式有哪些?
1、xml中配置
- bean 的申明、注册
节点注册 bean
节点的 factory-bean 参数指工厂 bean,factory-method 参数指定工厂方法
- bean 的注入
节点使用 set 方式注入
节点使用 构造方法注入
实测代码
maven pom 文件
<dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.2.4.RELEASE</version>
</dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.4.RELEASE</version>
</dependency>
A、 + ,set方法注入
class Bowl
package constxiong.interview.inject;public class Bowl {public void putRice() {System.out.println("盛饭...");}}
class Person
package constxiong.interview.inject;public class Person {private Bowl bowl;public void eat() {bowl.putRice();System.out.println("开始吃饭...");}public void setBowl(Bowl bowl) {this.bowl = bowl;}}
spring 配置文件
<?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.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><bean id="bowl" class="constxiong.interview.inject.Bowl" /><bean id="person" class="constxiong.interview.inject.Person"><property name="bowl" ref="bowl"></property></bean></beans>
测试类
package constxiong.interview.inject;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class InjectTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("spring_inject.xml");Person person = (Person)context.getBean("person");person.eat();}
}
B、修改为 配置文件和class Person, + 节点使用 构造方法注入
class Person
package constxiong.interview.inject;public class Person {private Bowl bowl;public Person(Bowl bowl) {this.bowl = bowl;}public void eat() {bowl.putRice();System.out.println("开始吃饭...");}}
spring 配置文件
<?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.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><bean id="bowl" class="constxiong.interview.inject.Bowl" /><bean id="person" class="constxiong.interview.inject.Person"><constructor-arg name="bowl" ref="bowl"></constructor-arg></bean></beans>
C、 节点 factory-method 参数指定静态工厂方法
工厂类,静态工厂方法
package constxiong.interview.inject;public class BowlFactory {public static final Bowl getBowl() {return new Bowl();}}
spring 配置文件
<?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.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><bean id="bowl" class="constxiong.interview.inject.BowlFactory" factory-method="getBowl"/><bean id="person" class="constxiong.interview.inject.Person"><constructor-arg name="bowl" ref="bowl"></constructor-arg></bean></beans>
D、非静态工厂方法,需要指定工厂 bean 和工厂方法
工厂类,非静态工厂方法
package constxiong.interview.inject;public class BowlFactory {public Bowl getBowl() {return new Bowl();}}
配置文件
<?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.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><bean id="bowlFactory" class="constxiong.interview.inject.BowlFactory"></bean> <bean id="bowl" factory-bean="bowlFactory" factory-method="getBowl"/><bean id="person" class="constxiong.interview.inject.Person"><constructor-arg name="bowl" ref="bowl"></constructor-arg></bean></beans>
2、注解
- bean 的申明、注册
@Component //注册所有bean
@Controller //注册控制层的bean
@Service //注册服务层的bean
@Repository //注册dao层的bean
- bean 的注入
@Autowired 作用于 构造方法、字段、方法,常用于成员变量字段之上。
@Autowired + @Qualifier 注入,指定 bean 的名称
@Resource JDK 自带注解注入,可以指定 bean 的名称和类型等
测试代码
E、spring 配置文件,设置注解扫描目录
<?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.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="constxiong.interview" /></beans>
class Bowl
package constxiong.interview.inject;import org.springframework.stereotype.Component;
//import org.springframework.stereotype.Controller;
//import org.springframework.stereotype.Repository;
//import org.springframework.stereotype.Service;@Component //注册所有bean
//@Controller //注册控制层的bean
//@Service //注册服务层的bean
//@Repository //注册dao层的bean
public class Bowl {public void putRice() {System.out.println("盛饭...");}}
class Person
package constxiong.interview.inject;//import javax.annotation.Resource;
//
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;@Component //注册所有bean
//@Controller //注册控制层的bean
//@Service //注册服务层的bean
//@Repository //注册dao层的bean
public class Person {@Autowired
// @Qualifier("bowl")
// @Resource(name="bowl")private Bowl bowl;public void eat() {bowl.putRice();System.out.println("开始吃饭...");}}
测试类同上
A、B、C、D、E 测试结果都ok
盛饭...
开始吃饭...
【Java面试题与答案】整理推荐
- 基础与语法
- 集合
- 网络编程
- 并发编程
- Web
- 安全
- 设计模式
- 框架
- 算法与数据结构
- 异常
- 文件解析与生成
- Linux
- MySQL
- Oracle
- Redis
- Dubbo
spring 常用的注入方式有哪些?相关推荐
- Spring之DI注入方式
什么是DI? DI(Dependency Injection)依赖注入,指容器复制创建和维护对象之间的依赖关系,而不是通过对象本身复制自己的创建和解决自己的依赖.控制反转是通过依赖注入实现的. 依赖注 ...
- spring(一):spring IoC的注入方式总结
前言:谈谈对springIOc的理解,两张图很好的阐述了springIoc容器的作用. 传统应用程序示意图.jpg IoC容器后程序结构示意图.jpg springIoC容器注入方式有set注入,构造 ...
- 依赖注入的三种方式_一起学Spring之三种注入方式及集合类型注入
本文主要讲解Spring开发中三种不同的注入方式,以及集合数据类型的注入,仅供学习分享使用,如有不足之处,还请指正. 概述 Spring的注入方式一共有三种,如下所示: 通过set属性进行注入,即通过 ...
- spring三种注入方式
设置Spring的作用域 或者使用枚举值设置 单例和多里使用场景 自动注入 @Primary 一个接口有多个实现被spring管理吗,在依赖注入式,spring会不知道注入哪个实现类就会抛出NoUni ...
- 【Spring】- 属性注入方式
2019独角兽企业重金招聘Python工程师标准>>> House类:只有一个People属性,验证引用的ref引用bean的set方法注入方式 package com.zhiwei ...
- spring中依赖注入方式总结
文章来源于今日头条用户:分布式系统架构 一.注解注入 注解注入在Spring中是用的最多的一种方式,就是在java代码中使用注解的方式进行装配,在代码中加入@Resource或者@Autowired. ...
- Spring Bean 的注入方式
DI 注入 一. 传统的注入方式 通过bean标签方式注入 通过包扫描的方式注入 二. 注解方式配置注入 @Configuration 与 @Bean 注解方式注入 @ComponentScan 包扫 ...
- 依赖注入的两种常用的注入方式
在Spring框架中,主要有两种依赖注入方式:基于构造函数.基于setter方法 1.基于构造函数 基于构造函数的DI(依赖注入)是通过调用具有多个参数的的构造函数的额容器来完成的,每个参数表示依赖关 ...
- spring的依赖注入方式
spring的依赖注入两种方式:Setter-based Dependency Injection(set方法去注入) 和 Constructor-based Dependency Injection ...
最新文章
- 用dotTace模仿下老赵的“使用Profiler分析程序性能”
- SQL SERVER2005加密解密数据
- 超图空间数据模型概要1
- 不要随随便便听别人安利买基金。。。
- Matconvnet安装:win7+VS2015(pro)+Matlab 2017a+cuda8.0+cudnn 5.1
- 代码规范:在Keil5中使用代码格式化工具Astyle(插件)
- VTK:可视化之SceneBounds
- tf.acos_带有Python示例的math.acos()方法
- linux proc大量文件夹,简析Linux中 /proc/[pid] 目录的各文件
- java hdu2014青年歌手大奖赛_评委会打分
- Python编程 基础篇(一)
- 3.5.3 连接偏移量管理器
- 战争调度(动态规划、记忆化搜索)
- 树状数组 之 poj 3067
- 商城系统面试题(二)
- 计算机课程教学资源网,课程动态
- JPA 7. Spring 整合 JPA
- 网易2017秋招编程题集合
- java编程培训学习的就业前景好不好
- DE-MerTOX™ 重金属排毒剂-道格拉斯实验室