1、spring framework
(1)访问https://repo.spring.io/release/
并按照/org/springframework/spring目录找下去
(2)也可以直接访问https://repo.spring.io/release/org/springframework/spring/
在这里,我选择下载了3.2.5.RELEASE版本
2、commons logging jar包
访问地址http://commons.apache.org/proper/commons-logging/
下载1.2版本
3、Hello World
1) 源码, jar文件:spring-framework-3.2.5.RELEASE
commons-logging-1.1.3.jar 日志 spring-beans-3.2.5.RELEASE.jar bean节点 spring-context-3.2.5.RELEASE.jar spring上下文节点 spring-core-3.2.5.RELEASE.jar spring核心功能 spring-expression-3.2.5.RELEASE.jar spring表达式相关
以上是必须引入的5个jar文件
2) Spring核心配置文件: applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="user" class="com.rk.spring.a_helloworld.User"></bean> </beans>
3) API
package com.rk.spring.a_helloworld; import org.junit.Test; 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.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class App { // 0. 直接创建User对象 @Test public void test() { User user = new User(); user.setId(100); user.setName("Tomcat"); System.out.println(user); } // 1. 通过工厂类得到IOC容器创建的对象 @Test public void testIOC() { // 把对象的创建交给spring的IOC容器 Resource resource = new ClassPathResource("com/rk/spring/a_helloworld/applicationContext.xml"); // 创建容器对象(Bean的工厂), IOC容器 = 工厂类 + beans.xml BeanFactory factory = new XmlBeanFactory(resource); // 得到容器创建的对象 User user = (User) factory.getBean("user"); System.out.println(user); } //2. (方便)直接得到IOC容器对象 @Test public void testAC() { // 得到IOC容器对象 ApplicationContext ac = new ClassPathXmlApplicationContext("com/rk/spring/a_helloworld/applicationContext.xml"); // 从容器中获取bean User user = (User) ac.getBean("user"); System.out.println(user); } }
时间: 2024-08-24 23:41:48