报错:Type mismatch: cannot convert from Object to Car
问题描述:
一个非常简单的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的更多相关文章
- 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. ...
- Type mismatch: cannot convert from Enumeration<String> to Enumeration<Object>
完整的错误信息: Description Resource Path Location TypeType mismatch: cannot convert from Enumeration<St ...
- 【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 ...
- 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 ...
- Type mismatch: cannot convert from java.sql.PreparedStatement to com.mysql.jdbc.PreparedStatement
Connection.prepareStatement()函数出错,提示: Type mismatch: cannot convert from java.sql.PreparedStatement ...
- 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 ...
- 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 ...
- (转)eclipse 报错 :The method list(String, Object[]) is ambiguous for the type BaseHibernateDao
背景:在开发过程中,经常遇到各种各样的编译问题,不断的总结,才能更好的提高效率. 描述 [报错] :The method list(String, Object[]) is ambiguous for ...
- bootstrap导航条报错 Uncaught TypeError: Cannot convert object to primitive value
原文: https://feiffy.cc/uncaught-typeerror-cannot-convert-object-to-primitive-value 最近发现我的博客页面移动端上下拉菜单 ...
随机推荐
- python之subprocess
官网介绍:https://docs.python.org/3/library/subprocess.html Popen(): [root@oracle scripts]# cat sub_popen ...
- 20145310《Java程序设计》第4次实验报告
20145310<Java程序设计>第4次实验报告 实验内容 搭建Android环境 运行Android 修改代码并输出自己的学号 实验步骤 搭建Android环境 安装Android S ...
- Python Matplotlib简易教程【转】
本文转载自:https://blog.csdn.net/Notzuonotdied/article/details/77876080 详情请见:Matplotlib python 数据可视化神器 简单 ...
- Servlet3.0与Spring
servlet filter listener web.xml DispatcherServlet web.xml 后期 servlet3.0我们去除web.xml 通过注解方式 同时需要s ...
- js 日期 处理 加减时分秒
1.日期处理 var _d = new Date("2018/01/01 12:00:00"); _d = new Date(_d.valueOf() + 60 * 1000);/ ...
- 【Detection】R-FCN: Object Detection via Region-based Fully Convolutional Networks论文分析
目录 0. Paper link 1. Overview 2. position-sensitive score maps 2.1 Background 2.2 position-sensitive ...
- Ubuntu安装zabbix
1.安装依赖包 安装mysql 安装nginx apt-get install php5-cli php5-cgi php5-fpm php5-mcrypt php5-mysql p ...
- IIS服务器管理学习
工欲善其事必先利其器 首先给服务器配上强力的软件,用于安全防护和监控. 公司服务器用的阿里云的ECS,已经有防护和监控了,之后又选择额外加了一个安全狗 为了监控服务器上系统的各项运行指标,又买了听云平 ...
- 前端打印功能实现及css设置
首先是使用下边代码,实现js局部打印功能.参数dom为需要打印的节点,为了保证页面功能的单一性,最好弹出一个新的预览页面完成打印功能. function print(dom){ var body = ...
- (转)全面认识一下.NET 4的缓存功能
很多关于.NET 4.0新特性的介绍,缓存功能的增强肯定是不会被忽略的一个重要亮点.在很多文档中都会介绍到在.NET 4.0中,缓存功能的增强主要是在扩展性方面做了改进,改变了原来只能利用内存进行缓存 ...