Three ways to create a Bean

Keywords: xml Java Spring encoding

1, Call the default parameterless constructor to create (common)

bean object: Car.java

package com.zj;

public class Car {

    private String brand;
    private double price;

    public Car(){
        System.out.println("Car Object created ");
    }

    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 +
                '}';
    }
}

Configuration file: 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>

Test function: 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);
    }
}

Display results:

information: Loading XML bean definitions from class path resource [applicationContext.xml]
Car Object created 
Car{brand='Audo', price=300000.0}

Process finished with exit code 0

Note: by default, if there is no default no parameter construct in the bean class, the creation fails and an exception is reported.

Modify the bean object code as follows:

package com.zj;

public class Car {

    private String brand;
    private double price;

    public Car(String brand,double price){
        System.out.println("Car Object created ");
    }

    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 +
                '}';
    }
}

Otherwise, the results are as follows:

Error:(8, 16) java: cannot apply the constructor Car in class com.zj.Car to the given type;
  Need: java.lang.String,double
  Found: no parameters
  Reason: the length of actual parameter list and formal parameter list is different

 

 

2, Create using methods in a static factory

bean object: Car.java

package com.zj;

public class Car {

    private String brand;
    private double price;

    public Car(){
        System.out.println("Car Object created ");
    }

    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 +
                '}';
    }
}

Static factory method: StaticFactory.java

package com.zj.factory;

import com.zj.Car;

public class StaticFactory {

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

Configuration file: 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>

Test function: 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);
    }
}

Display results:

information: Loading XML bean definitions from class path resource [applicationContext.xml]
Car Object created 
Car{brand='null', price=0.0}

Process finished with exit code 0

 

 

 

3, Create using methods in the instance factory

bean object: Car.java

package com.zj;

public class Car {

    private String brand;
    private double price;

    public Car(){
        System.out.println("Car Object created ");
    }

    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 +
                '}';
    }
}

Instance factory method: InstanceFactory.java

package com.zj.factory;

import com.zj.Car;

public class InstanceFactory {

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

Configuration file: 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">

    <!--Method of building example chemical plant-->
    <bean id="instanceFactory" class="com.zj.factory.InstanceFactory"/>
    <!--call-->
    <bean id="car" class="com.zj.factory.StaticFactory" factory-bean="instanceFactory" factory-method="getCar"/>

</beans>

Test function: 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);
    }
}

Display results:

information: Loading XML bean definitions from class path resource [applicationContext.xml]
Car Object created 
Car{brand='null', price=0.0}

Process finished with exit code 0

 

 

Posted by dacio on Mon, 06 Jan 2020 21:16:04 -0800