1:Spring是什么
1:Spring是一个开源框架;
2:Spring为简化企业级应用开发而生,使用Spring可以使简单的javaBean实现只有EJB才能实现的功能;
3: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)
Spring 模块如下
Spring HelloWorld 目录结构如下:
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<!-- 配置Bean -->
<bean id="helloWorld" class="com.spring.beans.HelloWorld">
<property name="name" value="spring"></property>
</bean>
</beans>
HelloWorld.java
package com.spring.beans;
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public void hello(){
System.out.println("Hello " + name);
}
}
Main.java
package com.spring.beans;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
/*//创建HelloWorld的一个对象
HelloWorld helloWorld = new HelloWorld();
//为name属性赋值
helloWorld.setName("wanghao");
*/
//1:创建Spring的IOC容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld helloWorld = (HelloWorld) ac.getBean("helloWorld");
//2:从IOC容器中获取IOC容器对象
//3:调用hello方法
helloWorld.hello();
}
}