1.项目总体概要
1)导入Spring的7个核心jar包
2)编写java类
3)配置Spring的xml文件,实例化java类
4)加载Spring的xml文件,测试java类是否被实例化
2. 导入Spring的7个核心jar包
核心jar包我在百度云上面已经进行了分享,可以自行下载
https://pan.baidu.com/s/1i4Cqr6t
3. 编写java类
编写HelloSpring类
/**
* 第一个测试Spring的例子
* @author 49882
*
*/
public class HelloSpring {
public void sayHello() {
System.out.println("hello Spring");
}
}
4. 配置Spring的xml文件,实例化java类
Spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 实例化了一个类的对象 hellospring -->
<bean id="hellospring" class="com.hlf.spring.HelloSpring"></bean>
</beans>
5. 加载Spring的xml文件,测试java类是否被实例化
测试类
package com.hlf.service;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hlf.spring.HelloSpring;
public class testHelloSpring {
public static void main(String[] args) {
//1 加载 spring.xml
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
//2 得到配置文件中实例化的类的对象
HelloSpring hs= (HelloSpring) context.getBean("hellospring");
//3 调用该对象的方法
hs.sayHello();
}
}
执行结果
6.Spring4_01的项目下载
实例下载 https://pan.baidu.com/s/1hsOJNyk