Tester.java
1 package test; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 import user.Dao; 6 7 public class Tester { 8 9 public static void main( String[] args){ 10 11 /*Resource r = new ClassPathResource("beans.xml"); 12 BeanFactory factory = new XmlBeanFactory(r);*/ 13 14 @SuppressWarnings("resource") 15 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); 16 17 user.Dao dao = (Dao) context.getBean("userDao"); 18 dao.register( new user.Bean()); 19 20 } 21 22 }
换成这种写法需要增加两个 jar 包:
spring-beans-3.2.2.RELEASE.jar
spring-context-3.2.2.RELEASE.jar
这个方法常用。