一、spring 框架概念
spring 是众多开源 java 项目中的一员,基于分层的 javaEE 应用一站式轻量
级开源框架,主要核心是 Ioc(控制反转/依赖注入) 与 Aop(面向切面)两大技
术,实现项目在开发过程中的轻松解耦,提高项目的开发效率。
在项目中引入Spring可以带来以下好处:
1.降低组件之间的耦合度,实现软件各层之间的解耦。
2.可以使用容器提供的众多服务,如:事务管理服务、消息服务等等。
3.当我们使用容器管理事务时,开发人员就不再需要手工控制事务.也不需处理复
杂的事务传播。
4.容器提供单例模式支持,开发人员不再需要自己编写实现代码。
5.容器提供了 AOP 技术,利用它很容易实现如权限拦截、运行期监控等功能。
二、Spring 源码架构
Spring 总共大约有 20 个模块, 由 1300 多个不同的文件构成。 而这些组件被分别整合在核心容器(Core Container) 、 Aop(Aspect OrientedProgramming) 和设备支持(Instrmentation) 、 数据访问及集成(DataAccess/Integeration) 、Web、 报文发送(Messaging) 、 测试 6 个模块集合中。
三、Spring 框架环境搭建
1.maven 创建普通 java 工程并调整整体工程环境
2.坐标 依赖添加 spring 框架核心坐标添加
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
3.编写 bean
package com.shsxt.service;
public class HelloService {
public void hello(){
System.out.println("hello spring");
}
}
4.spring 配置文件的编写
在 src 下新建 xml 文件,并拷贝官网文档提供的模板内容到 xml 中,配置bean 到 xml 中,把对应 bean 纳入到 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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
xmlns 即 xml namespace xml 使用的命名空间
xmlns:xsi 即 xml schema instance xml 遵守的具体规范
xsi:schemaLocation 本文档 xml 遵守的规范 官方指定
-->
<bean id="helloService" class="com.shsxt.service.HelloService"></bean>
</beans>
5.验证 spring 框架环境是否搭建成功
package com.shsxt.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloServiceTest {
@Test
public void test1() throws Exception {
/**
* 1.加载Spring的配置文件
* 2.取出Bean容器中的实例
* 3.调用bean方法
*/
// 1.加载Spring的配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
// 2.取出Bean容器中的实例
HelloService helloService = (HelloService) context.getBean("helloService");
// 3.调用bean方法
helloService.hello();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
2018-04-11 IT技术有感