- 1.Spring框架简介
- 1.1 什么是框架?
- 1.2 Spring框架诞生的技术背景
- 1.3 Spring框架的核心思想
- 1.4 Spring框架的核心体系
- 1.5 Spring框架的特点
- 1.6我对Spring的理解
- 2.使用Spring完成第一个demo
- 2.1 操作步骤
- 2.2 Maven工程的创建
- 2.3 Maven工程如何引入jar包
- 2.4 使用Spring完成第一个demo
- 2.4.1 创建Maven工程
- 2.4.2 创建Spring配置文件
- 2.4.3 创建服务类
- 2.4.4 编写测试类
- 2.4.5 运行测试类
- 3.Spring常用注解
- `@Component`
- `@Service`
- `@Repository`
- `@Controller`
- `@Autowired`
- `@Qualifier`
- `@Resource`
- `@Value`
- `@PostConstruct` 和 `@PreDestroy`
- `@Bean`
- `@Configuration`
1.Spring框架简介
1.1 什么是框架?
框架是一种具备通用性的结构体,能够加速产品功能实现的过程。例如,建筑框架、写作文章的结构框架等。
1.2 Spring框架诞生的技术背景
在多年前的Web开发中,开发者面临诸多问题,尤其是在基于三层模型开发时,业务逻辑代码中充斥着大量手动创建对象的代码,导致高度耦合。为了解决这些问题,Spring框架应运而生。
1.3 Spring框架的核心思想
Spring框架通过引入工厂模式解决了对象创建和依赖管理的问题,降低了代码之间的耦合度。它的核心思想包括控制反转(IoC)和依赖注入(DI),使得对象的创建和依赖关系由Spring框架来管理,而非在代码中硬编码。
1.4 Spring框架的核心体系
Spring框架的核心体系由以下几个模块组成:
- spring-core:提供框架的基础功能,包括IoC和依赖注入。
- spring-beans:提供BeanFactory,实现工厂模式,解耦配置和依赖。
- spring-context:建立在core和beans模块之上,提供上下文管理,支持国际化、事件传播、资源加载等功能。
- spring-context-support:提供对第三方集成的支持,如缓存、邮件、调度、模板引擎等。
- spring-expression:提供强大的表达式语言,用于在运行时查询和操作对象图。
1.5 Spring框架的特点
- 方便解耦,简化开发:Spring作为一个大工厂,管理所有对象的创建和依赖关系。
- 方便集成各种优秀框架:Spring支持与多种优秀框架集成,如Struts2、Hibernate、MyBatis等。
- 降低Java EE API的使用难度:Spring对一些难用的Java EE API(如JDBC、JavaMail)提供了封装,简化了应用开发。
- 方便程序的测试:Spring支持JUnit4,可以通过注解方便地测试Spring程序。
- AOP编程的支持:Spring提供面向切面编程,简化权限拦截和运行监控等功能的实现。
- 声明式事务的支持:通过配置即可管理事务,无需手动编程。
1.6我对Spring的理解
- 非侵入式:Spring开发的应用对象可以不依赖于Spring的API。
- 控制反转(IoC):对象的创建和依赖关系的维护交给Spring管理。
- 依赖注入(DI):依赖的对象通过配置自动注入,无需手动设置。
- 面向切面编程(AOP):Spring提供AOP支持,简化权限拦截和监控等功能。
- 容器:Spring是一个容器,管理应用对象的生命周期。
- 组件化:Spring通过简单的组件配置组合复杂应用。
- 一站式:Spring整合了多种企业应用开源框架和第三方类库。
2.使用Spring完成第一个demo
2.1 操作步骤
- 导入Spring框架的jar包。
- 创建用于配置bean对象的xml文件。
- 编写测试代码,从Spring容器中获取对象。
2.2 Maven工程的创建
- 创建Maven工程并配置pom.xml文件。
- 通过Maven仓库搜索并引入Spring-context依赖。
2.3 Maven工程如何引入jar包
- 打开浏览器,搜索“maven rep”并进入Maven仓库。
- 在搜索栏输入“spring-context”,选择对应的Spring版本。
- 复制对应版本的坐标到项目的pom.xml文件中。
通过以上步骤,可以成功创建一个Maven工程并引入Spring框架,为后续的Spring应用开发打下基础。
2.4 使用Spring完成第一个demo
为了演示如何使用Spring框架完成一个简单的demo,我们将通过以下步骤来实现一个简单的Spring应用。
2.4.1 创建Maven工程
首先,我们需要创建一个Maven工程,并在pom.xml
文件中添加Spring相关的依赖。
<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.example</groupId><artifactId>spring-demo</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.18</version></dependency></dependencies>
</project>
2.4.2 创建Spring配置文件
在src/main/resources
目录下创建一个名为applicationContext.xml
的Spring配置文件。
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="helloService" class="com.example.service.HelloService"><property name="message" value="Hello, Spring!"/></bean></beans>
2.4.3 创建服务类
在src/main/java/com/example/service
目录下创建一个服务类HelloService
。
package com.example.service;import org.springframework.stereotype.Service;@Service
public class HelloService {private String message;public void setMessage(String message) {this.message = message;}public void showMessage() {System.out.println(message);}
}
2.4.4 编写测试类
在src/main/java/com/example
目录下创建一个测试类DemoTest
。
package com.example;import com.example.service.HelloService;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class DemoTest {public static void main(String[] args) {// 加载Spring配置文件并初始化Spring容器ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");// 从Spring容器中获取helloService的beanHelloService helloService = context.getBean("helloService", HelloService.class);// 调用helloService的方法helloService.showMessage();// 关闭Spring容器context.close();}
}
2.4.5 运行测试类
运行DemoTest
类,如果一切配置正确,你将在控制台看到输出:Hello, Spring!
。
这个简单的demo展示了如何使用Spring框架来管理bean的创建和依赖注入。通过Spring配置文件,我们定义了helloService
的bean,并在测试类中通过Spring容器获取该bean的实例,然后调用其方法。这个过程展示了Spring框架的IoC和DI特性。
3.Spring常用注解
Spring提供了一系列的注解来简化Java应用的开发。以下是一些常用的Spring注解及其代码演示。
@Component
用于标注一个类为Spring组件,通常用于非服务层的单例对象。
import org.springframework.stereotype.Component;@Component
public class MyComponent {public void doWork() {System.out.println("Doing some work...");}
}
@Service
用于标注服务层的组件。
import org.springframework.stereotype.Service;@Service
public class MyService {public void performService() {System.out.println("Performing service operations...");}
}
@Repository
用于标注数据访问层的组件,即DAO组件。
import org.springframework.stereotype.Repository;@Repository
public class MyRepository {public void accessData() {System.out.println("Accessing data...");}
}
@Controller
用于标注控制层的组件,即Spring MVC中的控制器。
import org.springframework.stereotype.Controller;@Controller
public class MyController {public String handleRequest() {System.out.println("Handling HTTP request...");return "responseView";}
}
@Autowired
用于自动注入依赖的组件。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component
public class MyComponent {@Autowiredprivate MyService myService;public void doWork() {myService.performService();}
}
@Qualifier
当有多个相同类型的bean时,用于指定@Autowired
要注入的确切的bean。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;@Component
public class MyComponent {@Autowired@Qualifier("specificService")private MyService myService;public void doWork() {myService.performService();}
}
@Resource
用于依赖注入,可以指定名称进行注入。
import javax.annotation.Resource;
import org.springframework.stereotype.Component;@Component
public class MyComponent {@Resource(name="myService")private MyService myService;public void doWork() {myService.performService();}
}
@Value
用于注入外部配置的值。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component
public class MyComponent {@Value("${my.property}")private String myProperty;public void doWork() {System.out.println("Property value: " + myProperty);}
}
@PostConstruct
和 @PreDestroy
用于标注在bean的生命周期中的初始化后和销毁前需要执行的方法。
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.stereotype.Component;@Component
public class MyComponent {@PostConstructpublic void init() {System.out.println("Bean is initialized.");}@PreDestroypublic void destroy() {System.out.println("Bean is destroyed.");}public void doWork() {System.out.println("Doing some work...");}
}
@Bean
用于在配置类中声明一个bean。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class AppConfig {@Beanpublic MyService myService() {return new MyService();}
}
@Configuration
用于标注一个类作为配置类,可以替代传统的XML配置文件。
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ComponentScan;@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {// 此类将扫描com.example包下的所有Spring组件
}
这些注解是Spring框架中常用的注解,通过它们可以大大简化Spring应用的开发工作。在实际的项目中,根据需要选择合适的注解来使用。