1.新建java project,然后新建spring文件夹,把六个基本的包复制进去,然后全选所有的包,右键bulid Path>add**;
2.src所包含的class,beans.xml
1 package com.java.service; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 import com.java.test.HelloWorld; 7 8 public class Test { 9 10 public static void main(String[] args) { 11 12 //连接beans 13 ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml"); 14 15 HelloWorld helloWorld=(HelloWorld) ac.getBean("helloWorld"); 16 17 helloWorld.say(); 18 } 19 20 }
package com.java.service; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.java.test.HelloWorld; public class Test { public static void main(String[] args) { //连接beans ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml"); HelloWorld helloWorld=(HelloWorld) ac.getBean("helloWorld"); helloWorld.say(); } }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans.xsd"> 6 7 <bean id="helloWorld" class="com.java.test.HelloWorld"></bean> 8 9 10 </beans>
运行结果: