一、调用默认无参构造函数创建 ( 常用 )
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