代码
app-context-mmp.xml
DeclareSpringComponents.java
public class DeclareSpringComponents {
public static void main(String[] args) {
/* 实现了ApplicationContext接口 */
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("spring/app-context-mmp.xml");
//ctx.refresh();//一定要写上!!!
MessageRender render = ctx.getBean("render",MessageRender.class);
render.render();
ctx.close();
}
}
1、SAXParseException:cvc-elt.1: 找不到元素 “beans” 的声明
现在也不知道啥原因!!!
(1):spring5之后就不再需要写版本号了
参考:https://www.cnblogs.com/caotao0918/p/10053308.html
没解决!!!
(2):IDE疯了,重启,maven update
IDE是STS4,重启没效果,update没效果,反倒是发现了一个默认版本太低的问题,参考https://blog.csdn.net/wudinaniya/article/details/74389195
没解决!!!
参考:https://blog.csdn.net/guoquanyou/article/details/79003160
实在是崩溃了!!!于是重新写吧!!!
重写一个xml:mmp.xml,内容是一样的。
接下来运行,就是下面这个错,还好,错换了一个:
五月 04, 2019 10:56:04 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring/mmp.xml]
Exception in thread "main" java.lang.IllegalStateException: org.springframework.context.support.GenericXmlApplicationContext@41906a77 has not been refreshed yet
at org.springframework.context.support.AbstractApplicationContext.assertBeanFactoryActive(AbstractApplicationContext.java:1070)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1088)
at chap03.com.jay.config.DeclareSpringComponents.main(DeclareSpringComponents.java:13)
2、GenericXmlApplicationContext@41906a77 has not been refreshed yet
仔细检查检查代码,发现少了一行:[ ctx.refresh(); ],加上后再运行,还是报错
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'render' available
检查代码,发现少了@Service(value="render")的value:【value="render"】
最后运行成功,可爱的hello world!!!
五月 04, 2019 11:00:39 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring/mmp.xml]
五月 04, 2019 11:00:39 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericXmlApplicationContext@52a86356: startup date [Sat May 04 11:00:39 CST 2019]; root of context hierarchy
Hello World!!!
五月 04, 2019 11:00:40 上午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.GenericXmlApplicationContext@52a86356: startup date [Sat May 04 11:00:39 CST 2019]; root of context hierarchy
最后是完整的代码
接口
//接口MessageProvider
public interface MessageProvider {
String getMessage();
}
//接口MessageRender
public interface MessageRender {
void render();
/* MessageProvider是MessageRender的依赖项 */
void setMessageProvider(MessageProvider provider);
MessageProvider getMessageProvider();
}
实现类
//HelloWorldMessageProvider bean
@Component(value="provider")
public class HelloWorldMessageProvider implements MessageProvider{
@Override
public String getMessage() {
return "Hello World!!!";
}
}
//HelloWorldMessageRender bean
@Service(value="render")
public class HelloWorldMessageRender implements MessageRender{
public MessageProvider messageProvider;
@Override
public void render() {
if (messageProvider==null) {
throw new RuntimeException("you must set the property of messageProvider of class"+HelloWorldMessageRender.class.getName());
}
System.out.println(messageProvider.getMessage());
}
@Override
@Autowired
public void setMessageProvider(MessageProvider provider) {
messageProvider = provider ;//fuck be careful when coding!!!
}
@Override
public MessageProvider getMessageProvider() {
return this.messageProvider;
}
}
xml配置:参考上面的mmp.xml
运行类:参考上面DeclareSpringComponents.java,主语修改配置文件名字,并加上ctx.refresh()。
走不通的路,换一条吧。