1. 使用spring bean Factory 写代码。
- DrawingApp.java
package org.zcs.spring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class DrawingApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
BeanFactory factory = new FileSystemXmlApplicationContext("src/spring.xml");
Triangle triangle = (Triangle)factory.getBean("triangle");
triangle.draw();
}
}
- Triangle.java
package org.zcs.spring;
public class Triangle {
private String type;
private int height;
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public void draw() {
System.out.println(getType()+ " : There is a triangle. " );
}
}
- spring.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id = "triangle" class= "org.zcs.spring.Triangle" >
<property name="type" value = "this si my first say"></property>
</bean>
</beans>
2.笔记:
- 学习了如何使用spring中xml的bean管理类中的值。然后再调用此xml来调用bean factory中的对象。
- 学习了bean使用如何使用的。
- Applicationcontext 的使用和Property的初始化。
3.问题记录:
3.1 XmlBeanFactory过期问题:
三月 16, 2019 10:26:51 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@e2144e4: startup date [Sat Mar 16 10:26:51 CST 2019]; root of context hierarchy
三月 16, 2019 10:26:51 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [src/spring.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [src/spring.xml]; nested exception is java.io.FileNotFoundException: class path resource [src/spring.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:343)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:251)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:540)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:454)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at org.zcs.spring.DrawingApp.main(DrawingApp.java:11)
Caused by: java.io.FileNotFoundException: class path resource [src/spring.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:329)
... 13 more
- 解决办法:
// 过时处理逻辑,XmlBeanFactory在3.1以后已经被废弃,不再推荐使用
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("beanFactoryTest.xml"));
// 当前方案一:BeanFactory在启动的时候不会创建bean实例,而是在getBean()的时候,才会创建bean的实例
Resource resource = new ClassPathResource("applicationContext.xml");
beanFactory = new DefaultListableBeanFactory();
BeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader((BeanDefinitionRegistry) beanFactory);
beanDefinitionReader.loadBeanDefinitions(resource);
// 当前方案二:使用ApplicationContext
// (ApplicationContext在读取配置文件的时候,配置文件中的bena就会被初始化(不考虑bean的作用域))
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
3.2 ClassPathXmlApplicationContext找不到资源文件:
- 解决办法:
如果是用ApplicationContext ctx=new ClassPathXmlApplicationContext()也是这样是从当前路径寻找,xml文件应在当前目录下。
但是如果用FileSystemXmlApplicationContext呢,它是根据指定的路径来进行寻找,所以要把路径写完整。现在xml路径在src文件夹下。那就要写:ApplicationContext ctx=new FileSystemXmlApplicationContext(“src/applicationContext.xml”);
不过我发现这个可以把xml文件放到src目录下,就可了。ApplicationContext context = new ClassPathXmlApplicationContext(“spring.xml”);
3.2 xml 上的官方库文件引入模板
这个可以在官网的文档中找到。