1.Spring学习笔记_HelloWorld(by尚硅谷_佟刚)

一、Spring是什么

Spring 是一个开源框架。
Spring 为简化企业级应用开发而生(主要针对EJB2来说),使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能。

Spring 是一个 IOC(DI) 和 AOP 容器框架。


二、具体描述Spring

轻量级:Spring 是非侵入性的 - 基于 Spring 开发的应用中的对象可以不依赖于 Spring 的 API
依赖注入(DI --- dependency injection、IOC)
面向切面编程(AOP --- aspect oriented programming)
容器: Spring 是一个容器, 因为它包含并且管理应用对象的生命周期
框架: Spring 实现了使用简单的组件配置组合成一个复杂的应用. 在 Spring 中可以使用 XML 和 Java 注解组合这些对象

一站式:在 IOC 和 AOP 的基础上可以整合各种企业应用的开源框架和优秀的第三方类库 (实际上 Spring 自身也提供了展现层的 SpringMVC 和 持久层的 Spring JDBC)


三、Spring模块

1.Spring学习笔记_HelloWorld(by尚硅谷_佟刚)_第1张图片


四、安装 SPRING TOOL SUITE

SPRING TOOL SUITE 是一个 Eclipse 插件,利用该插件可以更方便的在 Eclipse 平台上开发基于 Spring 的应用。
安装方法说明(springsource-tool-suite-3.4.0.RELEASE-e4.3.1-updatesite.zip):
1.Help --> Install New Software...
2.Click Add... 
3.In dialog Add Site dialog, click Archive... 
4.Navigate to springsource-tool-suite-3.4.0.RELEASE-e4.3.1-updatesite.zip  and click  Open 
5.Clicking OK in the Add Site dialog will bring you back to the dialog 'Install' 
6.Select the xxx/Spring IDE that has appeared 
7.Click Next  and then Finish 
8.Approve the license 
9.Restart eclipse when that is asked

1.Spring学习笔记_HelloWorld(by尚硅谷_佟刚)_第2张图片


五、搭建 Spring 开发环境

1、创建maven工程

2、配置pom.xml引入spring的jar包


    
        org.springframework
        spring-context
        4.3.4.RELEASE
    
1.Spring学习笔记_HelloWorld(by尚硅谷_佟刚)_第3张图片

3、Spring 的配置文件:

一个典型的 Spring 项目需要创建一个或多个 Bean 配置文件, 这些配置文件用于在 Spring IOC 容器里配置 Bean. Bean 的配置文件可以放在 classpath 下, 也可以放在其它目录下

applicationContext.xml




    
        
    

4、建立Spring项目

public class HelloWorld {

	private String user;
	
	public HelloWorld() {
		System.out.println("HelloWorld's constructor...");
	}
	
	public void setUser(String user) {
		System.out.println("setUser:" + user);
		this.user = user;
	}
	
	public HelloWorld(String user) {
		this.user = user;
	}

	public void hello(){
		System.out.println("Hello: " + user);
	}
	
}
public class Main {
	
	public static void main(String[] args) {
		
//		HelloWorld helloWorld = new HelloWorld();
//		helloWorld.setUser("Tom");
//		helloWorld.hello(); 
		
		//1.创建Spring的IOC容器对象
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		//2.从IOC容器中获取bean实例
		HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
		
		//3.调用hello方法
		helloWorld.hello();
		
	}
	
}
5、运行结果



你可能感兴趣的:(spring)