通过Java代码装配bean
向着某一天终于要达到的那个终极目标迈步还不够,还要把每一步骤看成目标,使它作为步骤而起作用。
——歌德
很多场景下我们都可以通过Spring组件扫描和自动装配的方式来装配bean,但是在部分情况下,如使用第三方库中的Java类时,我们没办法将注解添加到其Java类中,Spring也就无法扫描识别装配bean。在这种情况下,我们就必须采用显式配置的方式:使用Java代码或XML配置。
在进行显式配置时,使用Java代码式更好的方案,因为它更强大、类型安全和友好。如下,我们创建一个GameConfig的java配置类,为其添加了@Configuration注解,表明这个类是一个配置类,作用是声明Spring应用上下文如何创建bean细节。
package chapter2.practice2;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;@Configuration public class GamerConfig {@Beanpublic Game lolGames() {return new LeagueOfLegends();} }
在GamerConfig类中我们使用@bean注解声明了一个LeagueOfLegends bean,@bean注解会告诉Spring这个方法将会返回一个对象,该对象要注册为Spring上下文的bean。值得注意的是:默认情况下,Spring中的bean都是单例的。
目前,LeagueOfLegends这个bean是非常简单的,因为它没有其他的依赖,下面我们给它添加一个依赖关系。
package chapter2.practice2;public class Computer {public void installGame() {System.out.println("Install the game...");} }
package chapter2.practice2;public class LeagueOfLegends implements Game {private Computer computer;public void play() {computer.installGame();} }
现在LeagueOfLegends类中依赖于Computer类,那么我们怎么在GanmerConfig配置类将其注入呢?
1)通过构造器注入
package chapter2.practice2;public class LeagueOfLegends implements Game {private Computer computer;public LeagueOfLegends(Computer computer) {this.computer = computer;}public void play() {computer.installGame();} }
package chapter2.practice2;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;@Configuration public class GamerConfig {/*** 构造器注入* @return*/@Beanpublic Game lolGames() {return new LeagueOfLegends(apComputer());}/*** 构造器注入* @return*/@Beanpublic Game loGames(Computer computer) {return new LeagueOfLegends(computer);}@Bean Computer apComputer() {return new Computer();} }
2)通过set方法注入
package chapter2.practice2;public class LeagueOfLegends implements Game {private Computer computer;public void setComputer(Computer computer) {this.computer = computer;}public LeagueOfLegends() {}public LeagueOfLegends(Computer computer) {this.computer = computer;}public void play() {computer.installGame();} }
package chapter2.practice2;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;@Configuration public class GamerConfig {/*** 构造器注入* @return*/@Beanpublic Game lolGames() {return new LeagueOfLegends(apComputer());}/*** 构造器注入* @return*/@Beanpublic Game loGames(Computer computer) {return new LeagueOfLegends(computer);}/*** set方法注入* @param computer* @return*/@Beanpublic Game lGames(Computer computer) {LeagueOfLegends game = new LeagueOfLegends();game.setComputer(computer);return game;}@Bean Computer apComputer() {return new Computer();} }
转载于:https://www.cnblogs.com/dandelZH/p/8698325.html
通过Java代码装配bean相关推荐
- java引入bean代码_通过java代码装配bean
importorg.springframework.beans.factory.annotation.Qualifier;importorg.springframework.context.annot ...
- java 装配_Spring 通过Java代码装配bean
1. 背景 尽管在很多场景下通过组件扫描和自动装配实现Spring的自动化扫描配置是更为推荐的方式,但在有些情况下自动化扫描的方案行不通,如想要将第三方库中的组件装配到自己的应用中.在这种情况下必须通 ...
- Spring实战——通过Java代码装配bean
上篇说的是无需半行xml配置完成bean的自动化注入.这篇仍然不要任何xml配置,通过Java代码也能达到同样的效果. 这么说,是要把上篇的料拿出来再煮一遍? 当然不是,上篇我们几乎都在用注解的方式如 ...
- Spring装配bean的三种方法:自动化装配,java代码装配,XML装配及它们的混合使用
一.自动化装配 首先,把可能被装配的类声明为组件类,告知spring要为这个类创建bean如: import org.springframework.stereotype.Component;@ Co ...
- 使用java方式装配Bean
首先创建一个项目 然后是项目名 下图: 创建完项目先配置pom.xml依赖关系 <?xml version="1.0" encoding="UTF-8"? ...
- Spring实战之二:装配Bean
2.1 Spring配置的可选方案 Spring提供了三种装配机制: 在XML中显式配置 在Java中显式配置 隐式的bean发现机制和自动装配 Best Practice:尽可能使用自动配置的机制, ...
- 《Spring实战》第四版读书笔记 第二章 装配Bean
2019独角兽企业重金招聘Python工程师标准>>> 在Spring中,对象无需自己查找或创建与其所关联的其他对象.相反,容器负责把需要相互协作的对象引用赋予各个对象. 创建应用对 ...
- 记下来 Spring 装配 Bean 的三种方式
ps:拿笔记一下,面试可能会考.依赖注入DI和面向切面编程AOP是Spring框架最核心的部分.这次主要是总结依赖注入的bean的装配方式. 前言 什么是依赖注入呢?也可以称为控制反转,简单的来说,一 ...
- spring(2)装配Bean
[0]README 0)本文部分文字描述转自:"Spring In Action(中/英文版)",旨在review spring(2)装配Bean 的相关知识: 1)在sprin ...
最新文章
- MVVM设计模式之精髓简化
- 有关short与int的重载[jase基础]
- 【Python入门】Python字典的11个方法超级详解
- delphi 回调函数例子 用函数过程作为参数
- python内置模块 (一)
- tcp丢包一定会断线吗_有遗传就一定会脱发吗
- linux spidev 应用_嵌入式Linux设备树语法总结
- active mq topic消费后删除_RabbitMQ的常见队列模型:simple、work、fanout、direct、topic等等...
- Linux shell 编程(四):变量
- 使用EasyUI加载树形菜单
- spring+jdbc+template+transaction实现
- FPGA入门之一位全加器的实现
- 浪潮服务器 虚拟光驱,玩转虚拟光驱:DAEMON TOOLS Pro
- 立创开源|PCIE X1转PCIE X16
- 区块链开发入门教程【加精】
- 想用linux又想windows,Linux对Windows说:停止吵架,和平共处
- 7.0高等数学五-高斯公式
- GIS 地图坐标系相互转换的方法学习笔记
- 三种近场通信的特点,以及未来近场通信技术的应用场景的分析和预测
- 《简约至上:交互式设计四策略》读书感悟
热门文章
- 易语言写c盘配置文件,易语言写配置文件的方法
- 嵌入式 linux restful,嵌入式 RESTful 框架 express.java
- java void eat_java匿名内部类
- php设置表单的字体,php表单标题怎么设置字体
- 如何删除写保护的文件_如何找回已删除或永久删除的Office Excel文件
- activiti 流程图乱码
- mysql命令:为mysql命令指定字符集
- mysql某元素为空_PHP - MySQL,认为$ result在某些时刻是空的,当时应该有元素
- java 正则表达式 替换字符串img标签的路径_python面试题汇总第06期-正则表达式(内附7题及答案)...
- win7蓝屏_win7电脑蓝屏怎么办