Spring学习笔记(一)- Spring 的 HelloWorld

1. Spring 是什么

1. Spring 简介
  • Spring 是一个开源框架.
  • Spring 为简化企业级应用开发而生. 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.
  • Spring 是一个 IOC(DI) 和 AOP 容器框架.
2. 具体描述 Spring:
  • 轻量级:Spring 是非侵入性的 - 基于 Spring 开发的应用中的对象可以不依赖于 Spring 的 API
  • 依赖注入(DI — dependency injection、IOC)
  • 面向切面编程(AOP — aspect oriented programming)
  • 容器: Spring 是一个容器, 因为它包含并且管理应用对象的生命周期
  • 框架: Spring 实现了使用简单的组件配置组合成一个复杂的应用. 在 Spring 中可以使用 XML 和 Java 注解组合这些对象
  • 一站式:在 IOC 和 AOP 的基础上可以整合各种企业应用的开源框架和优秀的第三方类库 (实际上 Spring 自身也提供了展现层的 SpringMVC 和 持久层的 Spring JDBC)

2. Spring 模块

image

3. 搭建 Spring 开发环境

参考:

https://blog.csdn.net/csdnsjg/article/details/80152815

4. Spring HelloWorld 设计

工程目录:

image

1. 加入下列 jar 包
  • commons-logging-1.1.1.jar
  • spring-beans-4.0.0.RELEASE.jar
  • spring-context-4.0.0.RELEASE.jar
  • spring-core-4.0.0.RELEASE.jar
  • spring-expression-4.0.0.RELEASE.jar
2. HelloWorld 类设计
public class HelloWorld {
	
	private String name;
	
	public HelloWorld() {
		System.out.println("HelloWorld's Constructor ... ");
	}
	
	public void setName(String name) {
		System.out.println("setName: " + name);
		this.name = name;
	}
	
	public void hello() {
		System.out.println("Hello: " + name);
	}
	
}
3. 创建 Spring 配置文件 applicationContext.xml
<!-- 配置 bean -->
<bean id="helloWorld" class="www.xq.spring.beans.HelloWorld">
	<property name="name" value="spring"></property>
</bean>
3. 测试
public static void main(String[] args) {

		//以前我们为  HelloWorld 类中的 name 属性赋值
		/*HelloWorld helloWorld = new HelloWorld();
		helloWorld.setName("past Time");
		
		helloWorld.hello();*/
		
		//现在
		//1. 创建 Spring 的 IOC 容器对象
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		//2. 从 IOC 容器中获取 Bean 实例
		HelloWorld  helloWorld = (HelloWorld) ctx.getBean("helloWorld") ;
		
		//3. 调用 hello 方法
		helloWorld.hello();
	}
  1. 运行结果
HelloWorld's Constructor ... 
setName: Spring
Hello: Spring
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值