《Spring攻略(第2版)》——1.3 调用构造程序创建Bean
Spring 创建 调用 攻略 1.3 bean
2023-09-11 14:17:34 时间
本节书摘来自异步社区《Spring攻略(第2版)》一书中的第1章,第1.3节,作者: 【美】Gary Mak , Josh Long , Daniel Rubio著,更多章节内容可以访问云栖社区“异步社区”公众号查看
1.3 调用构造程序创建Bean1.3.1 问题
你想要调用构造程序在Spring IoC容器中创建一个Bean,这是创建Bean最常见和直接的方法。这和Java中使用new操作符创建对象相同。
1.3.2 解决方案
通常,当你为一个Bean指定了class属性,就将要求Spring IoC容器调用构造程序创建Bean实例。
1.3.3 工作原理
假定你打算开发一个在线销售产品的购物应用程序。首先,你创建一个Product类,这个类有多个属性,比如产品名称和价格。因为商店中有许多类型的产品,所以你定义Product类为抽象类,用于不同产品子类的扩展。
package com.apress.springrecipes.shop; public abstract class Product { private String name; private double price; public Product() {} public Product(String name, double price) { this.name = name; this.price = price; // Getters and Setters public String toString() { return name + " " + price;
然后你创建两个产品子类:Battery和Disc。每个类都有自己的属性。
package com.apress.springrecipes.shop; public class Battery extends Product { private boolean rechargeable; public Battery() { super(); public Battery(String name, double price) { super(name, price); // Getters and Setters package com.apress.springrecipes.shop; public class Disc extends Product { private int capacity; public Disc() { super(); public Disc(String name, double price) { super(name, price); // Getters and Setters
为了在Spring IoC容器中定义一些产品,创建如下Bean配置文件:
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-3.0.xsd" bean id="aaa" property name="name" value="AAA" / property name="price" value="2.5" / property name="rechargeable" value="true" / /bean bean id="cdrw" property name="name" value="CD-RW" / property name="price" value="1.5" / property name="capacity" value="700" / /bean /beans
如果没有指定元素,将会调用默认的不带参数的构造程序。然后对于每个元素,Spring将通过设值方法注入值。前述的Bean配置等价于如下代码片段:
Product aaa = new Battery(); aaa.setName("AAA"); aaa.setPrice(2.5); aaa.setRechargeable(true); Product cdrw = new Disc(); cdrw.setName("CD-RW"); cdrw.setPrice(1.5); cdrw.setCapacity(700);
相反,如果有一个或者多个元素,Spring将调用匹配参数的最合适的构造程序。
beans ... bean id="aaa" constructor-arg value="AAA" / constructor-arg value="2.5" / property name="rechargeable" value="true" / /bean bean id="cdrw" constructor-arg value="CD-RW" / constructor-arg value="1.5" / property name="capacity" value="700" / /bean /beans
因为Product类和子类在构造程序上没有歧义,前述的Bean配置等价于下面的代码片段:
Product aaa = new Battery("AAA", 2.5); aaa.setRechargeable(true); Product cdrw = new Disc("CD-RW", 1.5); cdrw.setCapacity(700); 你可以编写下面的Main类从Spring IoC容器读取产品进行测试: package com.apress.springrecipes.shop; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Product aaa = (Product) context.getBean("aaa"); Product cdrw = (Product) context.getBean("cdrw"); System.out.println(aaa); System.out.println(cdrw); }
异步社区 异步社区(www.epubit.com)是人民邮电出版社旗下IT专业图书旗舰社区,也是国内领先的IT专业图书社区,致力于优质学习内容的出版和分享,实现了纸书电子书的同步上架,于2015年8月上线运营。公众号【异步图书】,每日赠送异步新书。
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载
相关文章
- 面试(4)-spring-Spring面试题和答案
- STS创建spring boot项目,pom.xml文件第一行报错
- Spring 创建Bean的实例的3种方式
- 从观察者模式谈论 : Spring Boot中创建、发布和侦听自定义事件
- spring-cloud-sleuth+zipkin追踪服务实现(一)
- Spring源码分析(九)lazy-init 在Spring中是怎么控制加载的
- MAVEN_SPRING+SPRINGMVC+MYBATIS+FREEMARKER 整合
- Spring Boot之旅
- spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务
- Spring Boot devtool的使用
- 使用spring boot创建fat jar APP
- spring框架漏洞整理(Spring Framework漏洞)
- spring框架漏洞整理(Spring Data漏洞)
- 【SSM直击大厂】第十章:Spring AOP面向切面编程
- 浅析Spring Bean的循环依赖问题、单例对象初始化三步、spring的三级缓存+提前曝光机制
- spring mvc Spring Data Redis RedisTemplate [转]
- 用maven创建web项目(spring Mvc)
- web工程中spring+ibatis的单元测试--转载
- IDEA从零到精通(18)之IDEA中用https://start.spring.io创建springboot项目
- spring boot 静态资源的映射规则 (3) 欢迎页面映射
- spring boot (2) 配置swagger2核心配置 docket
- Spring boot 环境搭建
- Spring如何实现IoC( IDEA创建一个maven项目实现IoC)