标签:
1.下载
① Spring的下载
Spring下载地址:http://repo.springsource.org/libs-release-local/org/springframework/spring
本次下载的是:4.0.4
目前最新版本4.3.0
② commons-logging的下载
Spring依赖Apache的logging;
http://commons.apache.org/proper/commons-logging/download_logging.cgi
搭建HelloWorld;
package com.spring.test; /** * 学生类 */ public class Student { public void getName(){ System.out.println("学生的NAME"); } }
package com.spring.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class Main { public void t(){ /** * ApplicationContext:接口,应用的上下文; * 常用的实现类: * 1.ClassPathXmlApplicationContext:从类加载路径下搜索配置文件,及src下; * 2.FileSystemXmlApplicationContext:从文件系统的相对路径或绝对路径下查找;默认在该路径下[E:\work\workspace\Spring_7.3\beans.xml]; * 3.XmlWebApplicationContext: */ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); Student stu1 = (Student) applicationContext.getBean("student");//获得bean的第1种方法,需要强制转换
Student stu = applicationContext.getBean("student",Student.class);//获得bean的第2种方法,无需强制装换 stu.getName(); } public static void main(String[] args) { Main m = new Main(); m.t(); } }
【输出】 七月 15, 2016 10:06:44 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6c069ec: startup date [Fri Jul 15 22:06:44 CST 2016];…… 七月 15, 2016 10:06:44 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [beans.xml] 学生的NAME
<?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-3.1.xsd"> <bean name="student" class="com.spring.test.Student"> </bean> </beans>
ApplicationContext:接口,应用的上下文;继承BeanFactory;
其有两个主要的实现类:
1.ClassPathXmlApplicationContext:从类加载路径下搜索配置文件,即src下;
2.FileSystemXmlApplicationContext:从文件系统的相对路径或绝对路径下查找;
标签:
原文地址:http://www.cnblogs.com/devan/p/5674894.html