Problem Description:
A very simple spring project that uses static factory methods to configure bean instances. The directory structure of the project is as follows:
code show as below:
Car.java
1 package com.tt.spring.beans.factory; 2 3 public class Car { 4 5 private String brand; 6 private double price; 7 8 public String getBrand() { 9 return brand; 10 } 11 public void setBrand(String brand) { 12 this.brand = brand; 13 } 14 public double getPrice() { 15 return price; 16 } 17 public void setPrice(double price) { 18 this.price = price; 19 } 20 21 public Car(){ 22 System.out.println("Car's Constructor..."); 23 } 24 25 26 27 public Car(String brand, double price) { 28 super(); 29 this.brand = brand; 30 this.price = price; 31 } 32 33 @Override 34 public String toString() { 35 return "Car [brand=" + brand + ", price=" + price + "]"; 36 } 37 38 39 }
StaticCarFactory.java
1 package com.tt.spring.beans.factory; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 /** 7 * Static factory method: you can directly call a static method of a certain class Return Bean instance 8 */ 9 public class StaticCarFactory { 10 11 12 private static Map<String,Car> cars = new HashMap<String, Car> (); 13 14 static { 15 cars.put("Audi", new Car ("Audi",300000 )); 16 cars.put("Ford", new Car("Ford",40000 )); 17 } 18 19 // Static factory method 20 public static Car getCar(String name){ 21 return cars.get(name); 22 23 } 24 25 }
beans-factory.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xmlns:util="http://www.springframework.org/schema/util" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 9 http://www.springframework.org/schema/util http: //www.springframework.org/schema/util/spring-util-4.0.xsd" > 10 11 <!-- Configure beans through static methods. Note that instead of configuring static factory method instances, but configuring bean instances --> 12 < bean id ="car1" 13 class ="com.tt.spring.beans.factory.StaticCarFactory" 14 factory-method ="getCar" > 15 < constructor-arg value ="Audi" ></constructor-arg> 16 </bean> 17 18 19 </beans>
Main.java:
1 package com.tt.spring.beans.factory; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 public class Main { 7 8 public static void main(String[] args){ 9 10 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-factory.xml"); 11 12 Car car1 = (Car) ctx.getBean("car1"); 13 System.out.println(car1); 14 } 15 }
Run the Main.java program, the console error is as follows:
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from Object to Car
at com.tt.spring.beans.factory.Main.main(Main.java:12)
Cause Analysis:
First, you need to install jre 1.5.0 and above.
Second,
set your installed jre in eclipse’s’Window”Preference”Java’ and’Install JREs’.
Third, in eclipse’s’Window”Preference ” Java ‘in,
‘ Compiler ‘was set up’ Compiler compliance level ‘of 5.0 or more
(the key is the third step, the compatibility level)
problem solved:
Since the jdk version I installed is 1.8.0, the problem should be the Compiler compliance level.
Go to Window->Preference->Java->Compiler and find Compiler compliance level=1.5
Change the Compiler compliance level to 1.8:
Run the Main.java code again, it runs normally, and the console displays the following information: