問題描述:
一個非常簡單的spring項目,用靜態工廠方法配置bean實例。項目的目錄結構如下:
代碼如下:
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 * 靜態工廠方法:直接調用某一個類的靜態方法就可以返回Bean的實例 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 //靜態工廠方法 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 <!-- 通過靜態方法來配置bean.注意不是配置靜態工廠方法實例,而是配置bean實例 --> 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 }
運行Main.java程序,控制台報錯如下:
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)
原因分析:
第一, 需要裝 jre1.5.0及以上的版本
第二, 在eclipse的'Window' 'Preference' 'Java'里,
'Install JREs'里設置你裝的jre
第三,在eclipse的'Window' 'Preference' 'Java'里,
'Compiler'里設'Compiler compliance level'為5.0以上
(關鍵是第三步, 兼容級別)
問題解決:
由於我安裝的jdk版本是1.8.0的,所以問題應該出在Compiler compliance level上。
進入Window->Preference->Java->Compiler,發現Compiler compliance level=1.5
將Compiler compliance level改成1.8:
再運行Main.java代碼,運行正常,控制台顯示如下信息: