import dao.LoginDao; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import service.LoginService; public class Login1 { public static void main(String[] args) { //获取核心容器对象 /* *ApplicationContext:构建核心容器时,创建对象采取的策略是采用立即加载的方式,也就是一读取完配置文件马上就创建配置文件中配置的对象(单例对象适用) *BeanFactory:构建核心容器时,创建对象延迟,什么时候获取id对象,什么时候创建对象(多例对象使用) * */ //ApplicationContext: //ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");//可以加载类路径下的配置文件,要求配置文件必须在类路径下(常用) //ApplicationContext ac = new FileSystemXmlApplicationContext("C:\Users\刘名远\Desktop\bean.xml");//可以加载磁盘任意路径下的配置文件(需要访问权限) //LoginService ls = (LoginService) ac.getBean("LoginServiceImpl");//根据spring配置文件中的id获取对象 //LoginDao ld = (LoginDao)ac.getBean("LoginDaoImpl"); // ClassPathResource resource = new ClassPathResource("bean.xml"); Resource resource = new ClassPathResource("bean.xml"); BeanFactory bf = new XmlBeanFactory(resource); LoginService ls=(LoginService)bf.getBean("LoginServiceImpl"); LoginDao ld=(LoginDao)bf.getBean("LoginDaoImpl"); System.out.println(ls); System.out.println(ld); } }
有问题或者指点请留言呀~