(一)新建一个java bean(HelloBean. java)
java代码
- package chb.demo.vo;
-
- public class HelloBean {
- private String helloWorld;
-
- public String getHelloWorld() {
- return helloWorld;
- }
-
- public void setHelloWorld(String helloWorld) {
- this.helloWorld = helloWorld;
- }
- }
-
(二)构造一个配置文件(beanConfig.xml)
xml 代码
- xml version="1.0" encoding="UTF-8"?>
- >
- <beans>
- <bean id="helloBean" class="chb.demo.vo.HelloBean">
- <property name="helloWorld">
- <value>Hello!chb!value>
- property>
- bean>
- beans>
(三)读取xml文件
1.利用ClassPathXmlApplicationContext
java代码
- ApplicationContext context = new ClassPathXmlApplicationContext("beanConfig.xml");
- HelloBean helloBean = (HelloBean)context.getBean("helloBean");
- System.out.println(helloBean.getHelloWorld());
2.利用FileSystemResource读取
java代码