beans.xml文件
//spring根据类HelloWorld来new一个helloWorld对象,根据id来去对象
<?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="helloWorld" class="com.java1234.test.HelloWorld"></bean>
</beans>
HelloWorld类
package com.java1234.test;
public class HelloWorld {
public void say(){
System.out.println("Spring他大爷");
}
}
测试类Test
package com.java1234.service;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.java1234.test.HelloWorld;
public class Test {
public static void main(String[] args) {
//加载beans.xml
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
//获取spring管理的bean
HelloWorld helloWorld=(HelloWorld)ac.getBean("helloWorld");
helloWorld.say();
}
}