IDEA搭建Spring框架环境

一、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>

这里写图片描述


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();
    }

}

这里写图片描述
这里写图片描述

  • 5
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值