Spring 框架

1.1.1   1. Spring 框架的概述

Spring 是一个开放源代码的设计层面框架,它解决的是业务逻辑层和其他各层的 松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。

Spring 的核心是控制反转(IoC 控制反转)和面向切面(AOP)。简单来说,Spring 是一个分层的 JavaSE/EEfull-stack(一站式) 轻量级开源框架。

1.1.2   2. Spring 框架的优点

1.方便解耦,简化开发,Spring 就是一个大工厂,可以将所有对象创建和依赖关 系维护,交给 Spring 管理。IOC 的作用。

2.AOP 编程的支持,Spring 提供面向切面编程,可以方便的实现对程序进行权限 拦截、运行监控等功能。(可扩展性)

3.声明式事务的支持,只需要通过配置就可以完成对事务的管理,而无需手动编 程。

4.方便程序的测试,Spring  Junit4 支持,可以通过注解方便的测试 Spring  序。

5.方便集成各种优秀框架,Spring 不排斥各种优秀的开源框架,其内部提供了对 各种优秀框架(如:Struts2 Hibernate MyBatis Quartz 等)的直接支持。

6.降低 JavaEE API 的使用难度,Spring   JavaEE 开发中非常难用的一些 API   JDBCJavaMail、远程调用等),都提供了封装,使这些 API 应用难度大大降 低。

1.2 Spring  IOC 核心技术

1.2.1   1.  什么是 IOC

IOC -- Inverse of Control ,控制反转,将对象的创建权力反转给 Spring 框架!!

控制反转(Inversion of Control ,缩写为 IoC),是面向对象编程中的一种设计 原则,可以用来减低计算机代码之间的耦合度。

解决问题:使用IOC 可以解决的程序耦合性高的问题!!Spring 的工厂读取配 置文件。

1.2.2   2. IOC 的程序入门

创建 Java 工程,导入坐标依赖

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.12</version>
    </dependency>
    <dependency>
        <groupId>j unit</groupId>
        <artifactId>j unit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>

编写接口和实现类,编写具体的实现方法

package com.qcby jy.service;

public interface UserService {

    public void hello();

}


package com.qcby jy.service;

public class UserServiceImpl implements UserService {
    @Override
    public void hello() {
        System.out.println("Hello IOC!!");
    }
}

编写 Spring 核心的配置文件,在 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">

<!--IOC 管理 bean-->
        <bean id="userService" class="com.qcbyjy.service.UserServiceImpl"
    />

</beans>

 log4j.properties 的配置文件拷贝到 resources 目录下,做为 log4j  日志配置 文件。

编写测试方法。

package com.qcby jy.test;

import com.qcby jy.service.UserService;
import org.j unit.Test;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;

public class Demo1 {

/**
* 入门程序
*/
    @Test
    public void run1(){
        // 使用 Spring 的工厂
        ApplicationContext applicationContext = new
        ClassPathXmlApplicationContext("applicationContext.xml");
        // 通过工厂获得类 :
        UserService userService = (UserService)
        applicationContext.getBean("userService");
        userService.hello();
    }

}

1.2.3   3. IOC 技术总结

ApplicationContext  口,工厂的接 口,使用该接口可以获取到具体的 Bean

象。该接口下有两个具体的实现类。  springCould   配置中心

ClassPathXmlApplicationContext ,加载类路径下的 Spring 配置文件。

FileSystemXmlApplicationContext ,加载本地磁盘下的 Spring 配置文件。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值