报错:Type mismatch: cannot convert from Object to Car

问题描述:

一个非常简单的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代码,运行正常,控制台显示如下信息:

原文地址:https://www.cnblogs.com/TTTTT/p/6404683.html