• 1.开始Spring


    1 对Spring的认识

    为了实现控制反转,我们可以想象java创建了一个工厂类,工厂类可以去读取配置文件 比如一个.property文件。通过这个文件中的配置,反射创建一些对象,当外界需要某个类的对象的时候,就像这个工厂索取。

    但是存在一个问题,那就是每次索取都会创建一个新的对象,而不是单例的。于是java就弄了一个Map字典,维护这一个字典,只要用户需要的对象不存在,就把对象创建好装进这个字典。如果用户需要的对象在

    这个字典中存在,就直接从这个字典中取,这就实现了单例,我们将这个字典视为一个存放类对象的容器。它以后发展为我们所谓的ioc容器——ApplicationContext。

    2.一个最简单的Spring 控制反转案例如下:

    a.创建一个接口和接口的实现如下:

    package com.itheima.service;
    
    public interface IAccountService {
        void saveAccount();
    }
    
    package com.itheima.service.impl;
    
    import com.itheima.service.IAccountService;
    
    public class AccountServicePureImpl implements IAccountService {
        public void saveAccount() {
            System.out.println("朝辞白帝彩云间");
        }
    }

    b.在Resource下创建一个bean.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">
    
    
        <bean id="accountServicePureImpl" class="com.itheima.service.impl.AccountServicePureImpl"></bean>
    
    
        </beans>

    c.如果要调用这个接口的实现类 首先要创建容器

    package com.itheima.ui;
    
    import com.itheima.service.IAccountService;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Client {
        public static void main(String[] args) {
            //告诉容器去读bean.xml中的配置  ClassPathXmlApplicationContext 是容器的三个主要常用的实现类之一
            ApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");
            IAccountService ac=(IAccountService)context.getBean("accountServicePureImpl");
            ac.saveAccount();
        }
    }

     3.IOC有三种IOC方式 第一 set方法 注意必须要有set方法  不能只定义一个变量  第二 构造函数  第三注解注入

    package com.itheima.service.impl;
    
    import com.itheima.dao.IAccountDao;
    import com.itheima.service.IAccountService;
    
    public class AccountServiceImpl implements IAccountService {
        private IAccountDao accountDao=null;
        public void setAccountDao(IAccountDao accountDao) {
            this.accountDao = accountDao;
        }
    
    
        public void saveAccount() {
            accountDao.saveAccount();
        }
    }
    ---------------以上是实现类--------------------

    下面是配置

    <?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">
        <bean id="accountServiceImpl" class="com.itheima.service.impl.AccountServiceImpl">
            <property ref="accountDaoImpl" name="accountDao"></property>
        </bean>
        <bean id="accountDaoImpl" class="com.itheima.dao.impl.AccountDaoImpl"></bean>
    
    
        </beans>

    三生三世

  • 相关阅读:
    第三次作业
    第二实验
    第一次作业
    yii2 Modal的使用
    yii2 显示列表字段 的技巧
    YII2在使用activeForm设置默认值
    html基础1
    tomcat+redis实现session共享缓存
    linux部署mongodb及基本操作
    hadoop 常用命令
  • 原文地址:https://www.cnblogs.com/wholeworld/p/11531085.html
Copyright © 2020-2023  润新知