Day21 SSM之SpringIOC

Spring的介绍

  • 1)Spring是什么?
    Spring 是分层的 Java SE/EE 应用 full-stack 轻量级开源框架
    full-stack Service Dao web
    轻量级 按需添加模块
    开源 可以获取源代码
    以 IOC- (Inverse Of Control:反转控制)和 AOP- (Aspect Oriented Programming:面向切面编程)
    为内核
  • (2)有什么特点?
    提供了展现层 SpringMVC
    持久层 Spring JDBC
    还能整合开源世界众多著名的第三方框架和类库
    业务层事务管理 AOP
    方便解耦,简化开发 IOC
    Java源码是经典学习范例
    逐渐成为使用最多的 Java EE 企业应用开源框架

Spring架构体系

(1)Test:用于测试使用
(2)Core container:核心容器,就是用于装配Java Bean对象
(3)AOP:切面编程
(4)Aspects:提供了与AspectJ的集成
(5)Data access:数据访问。用于访问操作我们的数据库。支持持久层的操作。jdbcTemplate mybatis
(6)Web:用于支持数据展示层,支持http请求
(7)Transactions:用于支持事物处理。用于解决业务层的事物处理问题。 编程式事务管理和声明式事务管理
在这里插入图片描述

Spring的IOC理论

  • (1)什么是IOC
    控制反转- (Inversion of Control,缩写为IoC)
    》把原来new对象的这种方式转换成了,spring通过反射创建对象的方式
    》spring创建完的对象放到一个容器中,谁需要就给谁注入进去- (获取对象并赋值给引用)
    简单说:把创建对象和管理对象的权利交给spring
    在这里插入图片描述
    Spring的IOC入门-环境搭建
  • (1)创建Project maven
  • (2)创建模块module maven
  • (3)配置依赖

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">

    <!--    要让 spring容器给我创建一个person对象-->
    <!--    配置类名,用于反向创建对象-->
    <!--    同时给一个编号方便查找-->
    <bean id="person" class="com.ssm.pojo.Person" >
        <property name="eat" value="吃面包"></property>
        <property name="wat" value="玩游戏"></property>
    </bean>
</beans>

Person

package com.ssm.pojo;

public class Person {
    
    
    private String eat;
    private String wat;

    public String getEat() {
    
    
        return eat;
    }

    public void setEat(String eat) {
    
    
        this.eat = eat;
    }

    public String getWat() {
    
    
        return wat;
    }

    public void setWat(String wat) {
    
    
        this.wat = wat;
    }
}

TestIOC

import com.ssm.pojo.Person;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class testIoc{
    
    

    @Test
    public  void test01(){
    
    
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //System.out.println(applicationContext.getBean("person"));
        Person person = (Person) applicationContext.getBean("person");
        System.out.println(person.getEat());

    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/wlj1442/article/details/109109852