Spring是什么
Spring 是分层的 Java SE/EE 应用 full-stack 轻量级开源框架,以 IoC(Inverse Of Control:
反转控制)和 AOP(Aspect Oriented Programming:面向切面编程)为内核,提供了展现层 Spring
MVC 和持久层 Spring JDBC 以及业务层事务管理等众多的企业级应用技术,还能整合开源世界众多
著名的第三方框架和类库,逐渐成为使用最多的 Java EE 企业应用开源框架。
Spring是一个轻量级框架
- Spring是非侵入性框架:可以任意和第三方框架结合使用,不会排斥其他框架。
- Spring开发可以不依赖于SpringAPI:使用Spring的时候,不需要继承Spring提供的任何的类或者实现Spring提供的任何接口,我们就可以享有Spring提供的功能。
Spring的体系结构
Spring开发环境的搭建
- 导jar包,最基本的Spring的开发需要的jar包
commons-logging-1.1.3.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 - 在类路径src创建Spring的配置文件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.xsd">
<!--
bean:表示配置一个bean,这个bean会在IOC容器启动的时候,被IOC容器加载(实例化),并且在IOC容器中管理这个bean
- id : bean的名称,是一个唯一的名称
- class: 这个bean对应的类型的全类名
property : 这个标签表示给IOC容器中管理的这个bean 设置属性
- name : 属性名【对应的是JavaBean风格的属性名】
- value : 需要设置的属性值
细节:
- name : 属性名【对应的是JavaBean风格的属性名,也就是对应的setter方法的属性名】
-->
<bean id="person" class="com.wanbangee.helloworld.Person">
<property name="age" value="19"></property>
<property name="name" value="紫霞仙子"></property>
</bean>
</beans
- 编写测试程序
package com.wanbangee.helloworld;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
/**
* 我们在启动IOC容器的时候,是根据类路径下的XML文件来创建的
* -ClassPathXmlApplicationContext:
它是从类的根路径下加载配置文件 推荐使用这种
* - 如果Spring的配置文件路径并不是直接在类路径下,我们需要加上绝对路径
* - 如果Spring的配置文件没有在类路径下
* - 使用FileSystemXmlApplicationContext ,表示从文件系统加载xml配置文件
* - 在FileSystemXmlApplicationContext构造器中传入配置文件在文件系统的绝对地址
*/
//1 .启动SpringIOC容器,并获得这个容器对象
ApplicationContext act = new ClassPathXmlApplicationContext("hello-world.xml");
//2 .从IOC容器中获取bean对象
Person person = (Person) act.getBean("person");
//3 .使用bean
System.out.println(person);
}
}