我是Spring工具套件中的新手。最近,我从spring.io(spring-tool-suite-3.9.3.RELEASE-e4.7.3-win32-x86_64)
下载了它。我创建了示例Maven项目,当我运行它时。它向我显示以下错误消息:
控制台错误日志:
Mar 28, 2018 8:47:32 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1f17ae12: startup date [Wed Mar 28 20:47:32 BDT 2018]; root of context hierarchy
Mar 28, 2018 8:47:33 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/springpractice/first/test/beans/beans.xml]
Mar 28, 2018 8:47:33 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@4cdbe50f: defining beans [patient,address]; root of factory hierarchy
Mar 28, 2018 8:47:33 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@4cdbe50f: defining beans [patient,address]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'patient' defined in class path resource [com/springpractice/first/test/beans/beans.xml]: Invocation of init method failed; nested exception is org.springframework.beans.factory.support.BeanDefinitionValidationException: Couldn't find an init method named 'beaninit' on bean with name 'patient'
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.springpractice.first.test.App.main(App.java:10)
Caused by: org.springframework.beans.factory.support.BeanDefinitionValidationException: Couldn't find an init method named 'beaninit' on bean with name 'patient'
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1541)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1522)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
... 12 more
我创建了一个bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="patient" class="com.springpractice.first.test.Patient"
scope="singleton" init-method="beaninit">
<constructor-arg value="16" name="id"></constructor-arg>
<constructor-arg value="Jerry" name="name"></constructor-arg>
<property name="nid" value="123456"></property>
<property name="address" ref="address"></property>
</bean>
<bean id="address" class="com.springpractice.first.test.Address">
<constructor-arg name="street" value="Dhormotola"></constructor-arg>
<constructor-arg name="postcode" value="7400"></constructor-arg>
</bean>
</beans>
我的patient.java
看起来像是:
package com.springpractice.first.test;
public class Patient {
private int id;
private String name;
private int nid;
private Address address;
public Patient() {
// TODO Auto-generated constructor stub
}
public void beaninit() {
System.out.println("Patient created : " + this);
}
public void beandestroy() {
System.out.println("Patient is destroyed");
}
public Patient(int id, String name) {
// super();
this.id = id;
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public int getNid() {
return nid;
}
public void setNid(int nid) {
this.nid = nid;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Patient [id=" + id + ", name=" + name + ", nid=" + nid + ", address=" + address + "]";
}
public void speak() {
System.out.println("want to be played");
}
}
很多时候,我看到当我创建一个新项目并在第一次向我显示一些错误时运行它。但是当我关闭该项目( project -> close)
并重新打开它( project -> open)
时,没有错误存在。项目编译并运行正常。在这种情况下,这种情况正在发生。
所以,我想知道当我关闭并打开一个项目以及删除错误时,eclipse会发生什么。