Spring框架--简单介绍

简介

Spring是分层的Java SE/EE应用full-stack轻量级开源框架,以IOC(Inverse Of Control:控制反转)和AOP(Aspect Oriented Programming:面向切面编程)为内核,提供了展现层Spring MVC和持久层 SpringJDBC以及业务层事务管理众多的企业级应用技术,还能整合开源世界众多著名的第三方框架和类库,逐渐成为使用最多的Java EE企业应用开源框架

在面向对象编程中,其基本思路是实现对象之间的低耦合,高内聚,而在传统的Java代码编写中,如果有某个对象A依赖于其他对象B,在使用时,需要先创建所依赖的对象B,传入对象A,然后才能对对象A进行操作,这种方式提高了代码的耦合度,十分不利于重构和维护
采用spring的依赖注入,可以促进代码的松散耦合。借助这种方式,对象无需知道依赖来自何处,或者依赖的实现方式,确保了低耦合

传统方式,需要先用new关键词,或者是通过工厂类来创建一个所依赖的对象,传入当前对象:

public class T2 {
    private T1 t1;
    public void setT1(T1 t1) {
        this.t1 = t1;
    }
    private String p1 = "this is T2: ";
    public void print(){
        System.out.println(p1);
        t1.print();
    }
}

如上,T2类依赖于T1类,当需要调用T2的方法时,需要先创建T1对象:

 @Test
    public void testT2_T1(){
        T1 t1 = new T1();
        T2 t2 = new T2();
        t2.setT1(t1);
        t2.print();
    }

IOC,Inversion of Control,控制反转,不是什么技术,而是一种设计思想,它包括依赖注入(Dependency Injection,简称DI)和依赖查找(Dependency Lookup)。在Java开发中,Ioc意味着将设计好的对象交给容器控制,而不是传统的在对象内部直接控制DI—Dependency Injection,即“依赖注入”。

把对象的创建交给spring

1.引入依赖

<?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.tulun</groupId>
    <artifactId>SpringTest</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.39</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>

    </dependencies>


</project>

2.创建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-3.0.xsd">

    <!--把对象的创建交给spring-->
    <bean id="accountService" class="com.service.impl.AccountServiceImpl"></bean>
    <bean id="accountDao" class="com.dao.impl.AccountDaoImpl"></bean>
</beans>

3.账户持久层实现类

public class AccountDaoImpl implements AccountDao {
    public void saveAccount(){
        System.out.println("保存了账户");
    }
}

4.账户业务层实现类

public class AccountServiceImpl implements AccountService {
    public AccountServiceImpl(){
    }
    private AccountDao accountDao = new AccountDaoImpl();
    public void saveAccount() {
        accountDao.saveAccount();
    }
}

5.账户持久层接口

public interface AccountDao {
    /**
     * 模拟保存账户
     */
     void saveAccount();
}

6.账户业务层接口

public interface AccountService {
    /**
     * 模拟保存账户
     */
    void saveAccount();
}

7.测试类:模拟一个表现层,调用业务层

public class Client {
    public static void main(String[] args) {
        //1.获取核心容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2.根据id获取bean对象
        AccountService as = (AccountService)ac.getBean("accountService");
        AccountDao dao = ac.getBean("accountDao",AccountDao.class);

        System.out.println(as);
        System.out.println(dao);

    }
}

运行结果:
在这里插入图片描述

ApplicationContext三个常用实现类

ClassPathXmlApplicationContest :他可以加载类路径下配置文件,要求配置文件必须在类路径下,不在的话,加载不了
FileSystemXmlApplicationContest:它是可以加载磁盘任意路径下的配置文件(必须有访问权限)
AnnotationConfigApplicationContest:它是用于读取注解创建容器的

核心容器的两个接口引发的问题:
ApplicationContest: 单例对象适用
他在构建核心容器时,创建对象采取的策略是立即加载的方式,只要读取完,配置文件就马上创建配置文件中配置对象

BeanFactory: 多例对象适用
他在构建核心容器时,创建对象才起的策略是延迟加载的方式,什么时候根据id获取对象了,什么时候才真正创建对象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值