问题描述:

一个非常简单的spring项目,用静态工厂方法配置bean实例。项目的目录结构如下:

代码如下:

Car.java

 package com.tt.spring.beans.factory;

 public class Car {

     private String brand;
private double price; 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;
} public Car(){
System.out.println("Car's Constructor...");
} public Car(String brand, double price) {
super();
this.brand = brand;
this.price = price;
} @Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]";
} }

StaticCarFactory.java

 package com.tt.spring.beans.factory;

 import java.util.HashMap;
import java.util.Map; /**
* 静态工厂方法:直接调用某一个类的静态方法就可以返回Bean的实例
*/
public class StaticCarFactory { private static Map<String,Car> cars = new HashMap<String, Car>(); static{
cars.put("Audi",new Car("Audi",300000));
cars.put("Ford", new Car("Ford",40000));
} //静态工厂方法
public static Car getCar(String name){
return cars.get(name); } }

beans-factory.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
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-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 通过静态方法来配置bean.注意不是配置静态工厂方法实例,而是配置bean实例 -->
<bean id="car1"
class="com.tt.spring.beans.factory.StaticCarFactory"
factory-method="getCar">
<constructor-arg value="Audi"></constructor-arg>
</bean> </beans>

Main.java:

 package com.tt.spring.beans.factory;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args){ ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-factory.xml"); Car car1 = (Car) ctx.getBean("car1");
System.out.println(car1);
}
}

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

报错:Type mismatch: cannot convert from Object to Car的更多相关文章

  1. Type mismatch: cannot convert from javax.servlet.http.Cookie[] to org.apache.tomcat.util.http.parser.Cookie[] 的一种可能

    今天用到Cookie时,写了一个Cookie数组,发现报错“Type mismatch: cannot convert from javax.servlet.http.Cookie[] to org. ...

  2. Type mismatch: cannot convert from Enumeration&lt;String&gt; to Enumeration&lt;Object&gt;

    完整的错误信息: Description Resource Path Location TypeType mismatch: cannot convert from Enumeration<St ...

  3. 【spring boot】spring boot 前台GET请求,传递时间类型的字符串,后台无法解析,报错:Failed to convert from type [java.lang.String] to type [java.util.Date]

    spring boot 前台GET请求,传递时间类型的字符串,后台无法解析,报错:Failed to convert from type [java.lang.String] to type [jav ...

  4. java.lang.Error: Unresolved compilation problems: Syntax error on token "return", delete this token Type mismatch: cannot convert from Init to String

    java.lang.Error: Unresolved compilation problems:   Syntax error on token "return", delete ...

  5. Type mismatch: cannot convert from java.sql.PreparedStatement to com.mysql.jdbc.PreparedStatement

    Connection.prepareStatement()函数出错,提示: Type mismatch: cannot convert from java.sql.PreparedStatement ...

  6. python中读取json文件报错,TypeError:the Json object must be str, bytes or bytearray,not ‘TextIOWrapper’

    利用python中的json读取json文件时,因为错误使用了相应的方法导致报错:TypeError:the Json object must be str, bytes or bytearray,n ...

  7. windows下安装pywin32报错:close failed in file object destructor:sys.excepthook is missing lost sys.stderr

    今晚要写搜索引擎作业,搭scrapy环境,遇到了下面问题: windows下安装pywin32报错:close failed in file object destructor:sys.excepth ...

  8. (转)eclipse 报错 :The method list(String, Object[]) is ambiguous for the type BaseHibernateDao

    背景:在开发过程中,经常遇到各种各样的编译问题,不断的总结,才能更好的提高效率. 描述 [报错] :The method list(String, Object[]) is ambiguous for ...

  9. bootstrap导航条报错 Uncaught TypeError: Cannot convert object to primitive value

    原文: https://feiffy.cc/uncaught-typeerror-cannot-convert-object-to-primitive-value 最近发现我的博客页面移动端上下拉菜单 ...

随机推荐

  1. awk二十问-【AWK学习之旅】

    ---===AWK学习之旅===--- 一行命令: 1.打印输入每行的字段总数: 最后一行的字段总数:END{print NF} 每行都显示字段总数: {print NF}   2.打印指定行: aw ...

  2. 20145324 Java实验二

    实验1: 先建立.java 在建立test 测试正常情况 测试出错情况 错误 修改后的代码 正确 测试边界情况 100时出错 修改后的代码 测试 实验2: 先建test在写,java 实验3:建模 练 ...

  3. hello java !

    我对于计算机性编程性质的课程一直没有很好的悟性,但功夫不服有心,最近自己学习视频课程,随时关注娄老师的博客,慢慢的对于java编程有了新的认识,也用eclipse软件进行了简单java的编译. 了解的 ...

  4. 什么是“QQ登录OAuth2.0”

    1. 什么是“QQ登录OAuth2.0 OAuth: OAuth(开放授权)是一个开放标准,允许用户授权第三方网站访问他们存储在另外的服务提供者上的信息,而不需要将用户名和密码提供给第三方网站或分享他 ...

  5. [CF1051F]The Shortest Statement

    题目大意:给定一张$n$个点$m$条有权边的无向联通图,$q$次询问两点间的最短路 $n\le100000$,$m\le100000$,$1\le100000$,$m$-$n\le20$. 首先看到$ ...

  6. cp 复制 mv剪切

    cp cp -p test.rb /home/test    将test.rb copy到test目录,并且保留原文件的属性cp -r Dir/ /home/test    将Dir目录copy到te ...

  7. 从0开始 数据结构 AC自动机 hdu 2222

    参考博客 失配指针原理 使当前字符失配时跳转到另一段从root开始每一个字符都与当前已匹配字符段某一个后缀完全相同且长度最大的位置继续匹配,如同KMP算法一样,AC自动机在匹配时如果当前字符串匹配失败 ...

  8. 使用size_t注意边界

    C++中的 size_t 表示数组的下标,一般为: typedef unsigned size_t; 在学习中我们一般使用int表示下标,这样在循环中可以判断边界x>=0 或x<=0,比如 ...

  9. linux一键安装mysql脚本

    #!/bin/sh if [ -s /etc/my.cnf ];then rm -rf /etc/my.cnf fi echo "------------------------------ ...

  10. jQuery实际案例⑤——仿京东侧边栏(楼层)

    楼层:①页面滑动到哪块儿“楼层”就显示到哪个:②点击某“楼层”页面滚动到对应的位置:③点击“返回”回到页面顶部 实现:①使用$(window).scroll(function(){ });  //监视 ...