框架:高度抽取可重用代码的一种设计,具有高度的通用性,形成某一个领域的解决方案。
Spring:是一个容器(可以管理所有组件(类))的框架,主要功能有IOC和AOP两大块。
Spring是分层的Java SE/EE 一站式轻量级开源框架,以IOC(Inverse of Control,控制反转)和AOP(Aspect Oriented Programming,面向切面编程)为核心。
IOC指的是将对象的创建权给Spring去创建。使用Spring之前,对象的创建都是由我们使用new创建,而使用Spring之后,对象的创建都交给了Spring框架。AOP用来封装多个类的公共行为,将那些与业务无关的,却为业务模块所共同调用的逻辑封装起来,减少系统的重复代码,降低模块之间的耦合性。另外,AOP还可以解决一些系统层面上面的问题,比如日志、事务和权限等。
在Spring中,认为一切Java类都是资源,而资源都是类的实例对象(Bean),容纳并管理这些Bean的是Spring所提供的IOC容器,所以Spring是一种基于Bean的编程。Spring使得很多复杂的代码变得优雅和简洁,避免了 EJB 臃肿、低效的开发模式,极大的方便项目的后期维护、升级和扩展
Spring的优良特性:
(1)基于Spring开发的应用中的对象可以不依赖Spring的API
(2)依赖注入(DI):IOC最经典的实现
(3)面向切面编程:AOP
(4)容器:Spring是一个容器,因为它包含并且管理应用对象的声明周期
(5)组件化:Spring使用简单的组件配置组合成一个复杂的应用,
(6)一站式:在IOC和AOP的基础上整合各种类库
IOC和DI:
编写一个HelloSpring:
(1)导入依赖pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sf.jin</groupId>
<artifactId>SpringIoc</artifactId>
<version>1.0-SNAPSHOT</version>
<name>SpringIoc</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--spring框架的5个基础包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
(2) 编写组件
package com.sf.jin;
/**
* @author JinJian
* @version 1.0
* @date 2021/12/7 22:33
*/
public class HelloSpring {
public void hello(){
System.out.println("Hello Spring");
}
}
(3)添加配置spring.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 id="helloSpring" class="com.sf.jin.HelloSpring"></bean>
</beans>
(4)编写测试类
package com.sf.jin;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author JinJian
* @version 1.0
* @date 2021/12/7 22:35
*/
public class SpringTest01 {
@Test
public void test(){
// 1.加载spring的配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
// 2.取出Bean容器中的实例
HelloSpring helloService = (HelloSpring) context.getBean("helloSpring");
// 3.调用bean方法
helloService.hello();
}
}
|