Bean的三种创建方式

一、调用默认无参构造函数创建 ( 常用 ) 

bean对象:Car.java

package com.zj;

public class Car {

    private String brand;
    private double price;

    public Car(){
        System.out.println("Car对象创建了 ");
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", price=" + price +
                '}';
    }
}

配置文件:applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="car" class="com.zj.Car">
        <property name="brand" value="Audo"></property>
        <property name="price" value="300000"></property>
    </bean>


</beans>

测试函数:Main.java

package com.zj;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.SQLException;

public class Main {
    public static void main(String args[]) throws SQLException {

        ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");

        Car car = (Car) act.getBean("car");
        System.out.println(car);
    }
}

显示结果:

信息: Loading XML bean definitions from class path resource [applicationContext.xml]
Car对象创建了 
Car{brand='Audo', price=300000.0}

Process finished with exit code 0

注:默认情况下,若bean类中没有默认无参构造,则创建失败,报异常。

修改bean对象代码如下:

package com.zj;

public class Car {

    private String brand;
    private double price;

    public Car(String brand,double price){
        System.out.println("Car对象创建了 ");
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", price=" + price +
                '}';
    }
}

其他不变,显示结果如下:

Error:(8, 16) java: 无法将类 com.zj.Car中的构造器 Car应用到给定类型;
  需要: java.lang.String,double
  找到: 没有参数
  原因: 实际参数列表和形式参数列表长度不同

 

 

二、使用静态工厂中的方法创建

bean对象:Car.java

package com.zj;

public class Car {

    private String brand;
    private double price;

    public Car(){
        System.out.println("Car对象创建了 ");
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", price=" + price +
                '}';
    }
}

静态工厂方法:StaticFactory.java

package com.zj.factory;

import com.zj.Car;

public class StaticFactory {

    public static Car getCar(){
        return new Car();
    }
}

配置文件:applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="car" class="com.zj.factory.StaticFactory" factory-method="getCar"/>

</beans>

测试函数:Main.java

package com.zj;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.SQLException;

public class Main {
    public static void main(String args[]) throws SQLException {

        ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");

        Car car = (Car) act.getBean("car");
        System.out.println(car);
    }
}

显示结果:

信息: Loading XML bean definitions from class path resource [applicationContext.xml]
Car对象创建了 
Car{brand='null', price=0.0}

Process finished with exit code 0

 

 

 

三、使用实例工厂中的方法创建

bean对象:Car.java

package com.zj;

public class Car {

    private String brand;
    private double price;

    public Car(){
        System.out.println("Car对象创建了 ");
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", price=" + price +
                '}';
    }
}

实例工厂方法:InstanceFactory.java

package com.zj.factory;

import com.zj.Car;

public class InstanceFactory {

    public Car getCar(){
        return new Car();
    }
}

配置文件:applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--创建实例化工厂方法-->
    <bean id="instanceFactory" class="com.zj.factory.InstanceFactory"/>
    <!--调用-->
    <bean id="car" class="com.zj.factory.StaticFactory" factory-bean="instanceFactory" factory-method="getCar"/>

</beans>

测试函数 :Main.java

package com.zj;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.SQLException;

public class Main {
    public static void main(String args[]) throws SQLException {

        ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");

        Car car = (Car) act.getBean("car");
        System.out.println(car);
    }
}

显示结果:

信息: Loading XML bean definitions from class path resource [applicationContext.xml]
Car对象创建了 
Car{brand='null', price=0.0}

Process finished with exit code 0

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值