Spring的概述
- 什么是Spring
Spring是一个分层的SE/EEfull-stack(一站式)轻量级的框架
-
Spring:SE/EE 开发的一站式框架
一站式框架:有 EE 开发的每一层的解决方案
WEB层: Spring MVC Service层: Spring 的 Bean 管理,Spring 声明式事务 DAO层: Spring 的jdbc 模板,Spring 的ORM 模块
Spring入门(IOC/控制反转)
- IOC: Inversion of Control(控制反转)
控制反转:将对象的创建权反转给(交给)Spring
也就是一般程序中我们需要手动new 一个对象,而在 Spring 框架中,由 Spring 帮我们new 新对象,我们只需要向Spring拿即可。
- Spring jar包
5.X版本的Spring 需要可以引入四个基本依赖jar和两个日志类的依赖jar:
其他版本的Spring Jar 包也类似,只是版本号不一样,可以在官方的服务器上自行下载:
http://repo.spring.io/release/org/springframework/(Spring jar 以及 各种依赖包)
https://repo.spring.io/list/libs-release-local/org/springframework/spring/(Spring 各版本jar包)
IOC的基本思想
- 如果底层的实现切换了,需要修改源码,是否能有不用修改源码而对程序进行扩展的方法。
将实现类交给Spring管理(IOC和DI)
-
IOC:控制反转,将对象的创建权反转(交给)给了Spring。
- DI:依赖注入,在IOC的环境下,Spring管理这个类的时候将类的依赖的属性注入(设置)进来。也就是在配置文件中给类的属性赋值,这样修改初值就不改动实现类源码。
PS:需要给依赖属性增加Set()方法,Spring才可以注入。
- DI:依赖注入,在IOC的环境下,Spring管理这个类的时候将类的依赖的属性注入(设置)进来。也就是在配置文件中给类的属性赋值,这样修改初值就不改动实现类源码。
接口类和实现类的代码如下:
接口 Service:
package demo;
public interface Service {
void save();
}
实现类 ServiceImpl:
package demo;
public class ServiceImpl implements Service{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void save() {
System.out.println("service已经被访问");
}
}
测试类 test:
package demo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
@Test
public void test(){
//读取Spring配置
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
//从Spirng中通过控制反转获取新对象。
Service service = (Service) applicationContext.getBean("Service");
service.save();
}
}
在src目录下新建一个 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">
<!--Spring的基本配置,IOC 和 DI-->
<!--配置bean名称和类的路径-->
<bean name="Service" class="demo.ServiceImpl">
<!--DI的基本配置,依赖注入,给类里面的属性赋值-->
<property name="name" value="kk" />
</bean>
</beans>
同时为了避免之后运行时出现log4j的警告,再新建一个log4j.properties :
# Configure logging for testing: optionally with log file
log4j.rootLogger=WARN, stdout
# log4j.rootLogger=WARN, stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
Spirng 的工程类
- 工厂类的结构图:
- 老版本的工厂类:BeanFactory
调用getBean 的时候,才会生成类的实例。
- 新版本的工程类:ApplicationContext
加载配置文件的时候,就会将Spring管理的类全部实例化。
-
ApplicationContext有两个实现类
ClassPathXmlApplicationContext :加载类路径下的配置文件(项目中src里的文件)
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“ApplicationContext.xml”);
FileSystemXmlApplicationContext :加载文件系统下的配置文件(本地磁盘下的文件)
ApplicationContext applicationContext = new FileSystemXmlApplicationContext(“E:\\ApplicationContext.xml”);