zoukankan      html  css  js  c++  java
  • IOC(二)

    1.IOC思想基于IOC容器完成, IOC容器底层就是对象工厂

    2.Spring提供IOC容器实现的两种方式(两个接口):

      1).BeanFactory: IOC容器基本实现方式, 是Spring内部使用的接口, 不提供开发人员进行使用, 加载配置文件是不会创建对象, 在获取对象(使用)时才去创建对象;

      2).ApplicationContext: BeanFactory接口的子接口, 提供更多更强大的功能, 一般有开发人员进行使用, 加载配置文件时就会创建配置文件对象(这样就会让项目在开启时就创建好对象);

    3. ApplicationContext接口有两个实现类:

      

       两者的区别是:

      1).FileSystemXmlApplicationContext的路径参数以项目路径开始(从项目目录开始写路径):

    public class TestSpring5 {
    
        public static void main(String[] args){
    
            //1.加载Spring配置文件
            ApplicationContext context = new FileSystemXmlApplicationContext("conf/beans1.xml");
            //2.获取配置创建的对象
            User user = context.getBean("user", User.class);
    
            System.out.println(user);
            user.sayhi();
        }
    }

      2).ClassPathXmlApplicationContext的路径参数需写绝对路径(从盘符开始写, 前面加 file:):

    public class TestSpring5 {
    
        public static void main(String[] args){
    
            //1.加载Spring配置文件
            ApplicationContext context = new ClassPathXmlApplicationContext("file:D:\IntelliJIDEA\IntelliJ_IDEA_workspace\springDemo1015\conf\beans1.xml");
            //2.获取配置创建的对象
            User user = context.getBean("user", User.class);
    
            System.out.println(user);
            user.sayhi();
        }
    }
  • 相关阅读:
    C# Lambda表达式
    C# LINQ用法
    C# XML介绍及基本操作
    C# 装箱和拆箱
    C# 堆与栈
    C#中ref和out的区别
    C#中16进制string字符串的转16byte互转
    C#中把一个Struct结构转换成Byte[]的方法
    SqlServer中查询操作记录的方法
    asp.net中后台获取Post参数(Json)最简单的一种方法。
  • 原文地址:https://www.cnblogs.com/Ryan368/p/13853964.html
Copyright © 2011-2022 走看看