Spring

github resolve spring-projects/spring-framework#27432. Added Assert.isFalse … by Melancholic · Pull Request #27514 · spring-projects/spring-framework · GitHub

spring官网  Spring Framework

spring下载地址  http://repo.spring.io/release/org/springframework/spring

meavn Spring web MVC依赖包

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.3.10</version>
</dependency>

1.2优点

轻量级 非入侵式IOC 控制反转AOP面向切面编程的框架

1.3组成

1.4 拓展

Spring Boot

   快速开发脚手架,快速开发单个微服务

  约定大于配置

Spring Cloud

  基于Spring Boot实现的

2 IOC理论推导

 1.UserDao接口

 2.UserDaoImpl实现类

2.1UserDaoMysqlImpl实现类

 3.UserService业务接口

 4.UserServiceImpl业务实现类

以前没有在UserServiceImpl中建立set方法,需要程序员管理对象的创建。

 private UserDao userDao=new UserDaoImpl();
 UserService userService=new UserServiceImpl();
       // ((UserServiceImpl) userService).setUserDao(new UserDaoImpl());
        userService.getUser();

 

在UserServiceImpl中建立set方法动态选择注入 UserDao接口的实例对象

使得选择权放在了用户手中

这种变化相当于控制反转,从原来的我们管理对象的创建到被动的接受对象,系统耦合性降低。

/        用户调用业务层不接触Dao层
        UserService userService=new UserServiceImpl();
        ((UserServiceImpl) userService).setUserDao(new UserDaoImpl());
        userService.getUser();
  private UserDao userDao;

    public UserDao getUserDao() {
        return userDao;
    }
//利用set控制值的注入
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

ioc是Spring框架的核心内容,使用很多方式完美的实现了Ioc,可以使用xml配置,亦可以使用注解,新版本的Spring也可以零配置实现Ioc。

控制反转ioc是一种描述(XML或者注解)并通过第三方去生产或产生特定的对象。在Sring中实现控制反转的是ioc容器,其实现方式是依赖注入(Dependency injection DI)。

Spring

代码Spring-01-ioc

 beans.xml

<!--使用Sring来创建对象,在Spring这些都成为Bean-->
<!--bena=对象
类型 变量名=new 类型();
Hello hello=new Hello();
id hello
class =new 的对象
property 给对象设值

-->
<?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">
        <bean id="userDaoImpl" class="com.yang.dao.UserDaoImpl"/>
        <bean id="userDaoMysqlImpl" class="com.yang.dao.UserDaoMysqlImpl"/>
    <bean id="userServiceImpl" class="com.yang.service.UserServiceImpl">
        <!--ref设置引用值-->
        <property name="userDao" ref="userDaoMysqlImpl">

        </property>
    </bean>
</beans>

 

上一篇:2.Spring快速入门


下一篇:(II)第十五节:泛型依赖注入