Spring 是一个 IOC(DI)和AOP的框架

Spring 是什么
- 轻量级,Spring是非侵入性的,基于Spring开发的应用中对象可以不依赖于Spring的API
- 依赖注入
- 面向切面编程
- 容器,Spring是一个容器,因为它包含并且管理应用对象的生命周期
- 框架,Spring实现了使用简单的组建配置组合成一个复杂的应用,在Spring中可以使用XML和Java的注解组合这些对象
- 一站式,在IOC和AOP的基础换行可以整合各种企业应用的开源框架和优秀的第三方类库,SpringMVC,Spring JDBC,Mybatis…
Spring Hello World
// src/com/lijunyang/model/HelloWorld.javapublic class HelloWorld {private String name;public void setName(String name) {this.name = name;}public String getName() {return this.name;}public String hello() {System.out.println("Hello: " + this.name);}}// src/com/lijunyang/model/Main.javapublic class Main {public static void main(String[] args) {HelloWorld hello = new HelloWorld();hello.setName("lijunyang");hello.hello();// "Hello: lijunyang"}}
// src/applicationCentext.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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><bean id="helloWorld" class="com.lijunyang.model"><property name="name" value="Spring"></property></bean></beans>// src/com/lijunyang/model/Main.javapublic class Main {public static void main(String[] args) {// 创建 Spring IOC容器org.springframework.context.ApplicationContext ctx =new ClassPathXmlApplicationContext("applicationContext.xml");// 从容器中取出 Bean实例HelloWorld hello = (HelloWorld) ctx.getBean("helloWorld");hello.hello();// "Hello: Spring"}}
// src/com/lijunyang/model/HelloWorld.javapublic 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 String getName() {return this.name;}public String hello() {System.out.println("Hello: " + this.name);}}// src/com/lijunyang/model/Main.javapublic class Main {public static void main(String[] args) {org.springframework.context.ApplicationContext ctx =new ClassPathXmlApplicationContext("applicationContext.xml");// 此时输出// HelloWorld`s Constructor// setName: Spring}}
