《Spring攻略(第2版)》——1.3 调用构造程序创建Bean

简介: 假定你打算开发一个在线销售产品的购物应用程序。首先,你创建一个Product类,这个类有多个属性,比如产品名称和价格。因为商店中有许多类型的产品,所以你定义Product类为抽象类,用于不同产品子类的扩展。

本节书摘来自异步社区《Spring攻略(第2版)》一书中的第1章,第1.3节,作者: 【美】Gary Mak , Josh Long , Daniel Rubio著,更多章节内容可以访问云栖社区“异步社区”公众号查看

1.3 调用构造程序创建Bean

1.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" class="com.apress.springrecipes.shop.Battery">
     <property name="name" value="AAA" />
     <property name="price" value="2.5" />
     <property name="rechargeable" value="true" />
   </bean>

   <bean id="cdrw" class="com.apress.springrecipes.shop.Disc">
     <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" class="com.apress.springrecipes.shop.Battery">
     <constructor-arg value="AAA" />
     <constructor-arg value="2.5" />
     <property name="rechargeable" value="true" />
   </bean>

   <bean id="cdrw" class="com.apress.springrecipes.shop.Disc">
     <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);
   }
}
相关文章
|
22天前
|
缓存 Java Spring
Spring 框架中 Bean 的生命周期
Spring 框架中 Bean 的生命周期
30 1
|
1月前
|
XML Java 开发者
Spring Boot中的bean注入方式和原理
Spring Boot中的bean注入方式和原理
50 0
|
1月前
|
XML 缓存 Java
Spring源码之 Bean 的循环依赖
循环依赖是 Spring 中经典问题之一,那么到底什么是循环依赖?简单说就是对象之间相互引用, 如下图所示: 代码层面上很好理解,在 bean 创建过程中 class A 和 class B 又经历了怎样的过程呢? 可以看出形成了一个闭环,如果想解决这个问题,那么在属性填充时要保证不二次创建 A对象 的步骤,也就是必须保证从容器中能够直接获取到 B。 一、复现循环依赖问题 Spring 中默认允许循环依赖的存在,但在 Spring Boot 2.6.x 版本开始默认禁用了循环依赖 1. 基于xml复现循环依赖 定义实体 Bean java复制代码public class A {
|
1月前
|
存储 NoSQL Java
Spring Boot统计一个Bean中方法的调用次数
Spring Boot统计一个Bean中方法的调用次数
33 1
|
2月前
|
Java 索引 Spring
spring创建bean的过程
spring创建bean的过程
|
14天前
|
前端开发 Java 数据库连接
Spring系列文章1:Spring入门程序
Spring系列文章1:Spring入门程序
|
2月前
|
缓存 Java 数据库
优化您的Spring应用程序:缓存注解的精要指南
优化您的Spring应用程序:缓存注解的精要指南
45 0
|
6天前
|
Java 数据库连接 开发者
浅谈Spring的Bean生命周期
浅谈Spring的Bean生命周期
15 1
|
11天前
|
XML Java 数据格式
Bean工厂探秘:解析Spring底层工厂体系BeanFactory的神奇之道
Bean工厂探秘:解析Spring底层工厂体系BeanFactory的神奇之道
19 0
Bean工厂探秘:解析Spring底层工厂体系BeanFactory的神奇之道
|
21天前
|
XML Java 程序员
作为Java程序员还不知道Spring中Bean创建过程和作用?
作为Java程序员还不知道Spring中Bean创建过程和作用?
13 0