spring快速入门(四)

时间:2021-10-11 11:04:10

一、在spring快速入门(三)的基础上,我们来了解BeanFactory及配置。

  Client

package com.murong.client;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.murong.action.UserAction;
import com.murong.action.UserActionImpl; public class Client { public static void main(String[] args) { BeanFactory bf = new ClassPathXmlApplicationContext("applicationContext.xml");
UserAction userAction = (UserAction) bf.getBean("userActionImpl");
userAction.addUser("久伴",24);
}
}

  首先BeanFactory是一个接口 ,有不同的实现类、当然还有其他的实现类,比如  ApplicationContext

package com.murong.client;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.murong.action.UserAction;
import com.murong.action.UserActionImpl; public class Client { public static void main(String[] args) { /*BeanFactory bf = new ClassPathXmlApplicationContext("applicationContext.xml");*/
ApplicationContext bf = new ClassPathXmlApplicationContext("applicationContext.xml");
UserAction userAction = (UserAction) bf.getBean("userActionImpl");
userAction.addUser("久伴",24);
}
}