zoukankan      html  css  js  c++  java
  • spring的核心容器ApplicationContext

    //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.xsd">
    <!--把对象的创建交给spring管理-->
    <bean id="accountService" class="com.hope.service.impl.AccountServiceImpl"/>
    <bean id="accountDao" class="com.hope.dao.impl.AccountDaoImpl"/>

    </beans>


    package com.hope.ui;


    import com.hope.dao.IAccountDao;
    import com.hope.service.IAccountService;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    /**
    * @author newcityman
    * @date 2019/11/18 - 14:23
    * 模拟一个表现层框架
    */
    public class Client {
    /**
    * 获取spring的ioc容器,根据id获取对象
    * ApplicationContext接口的三个实现类
    * 1、ClassPathXmlApplicationContext:它可以加载类路径下的配置文件,要求配置文件必须在类路径下面。不在的话,无法加载
    * 2、FileSystemXmlApplicationContext:它可以加载磁盘任意路径下的配置文件(必须有访问权限)
    * 3、AnnotationConfigApplicationContext:它是读取注解创建容器的方法
    * @param args
    */
    public static void main(String[] args) {
    //1、获取核心容器对象
    ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
    //2、根据id获取bean对象
    IAccountService as=(IAccountService)ac.getBean("accountService");
    IAccountDao ad = ac.getBean("accountDao",IAccountDao.class);
    System.out.println(as);
    System.out.println(ad);

    }
    }
  • 相关阅读:
    百度PaddlePaddle入门-14(多个CPU加速训练)
    Quantum Hierarchical State Machine (量子层级状态机)
    百度PaddlePaddle入门-13(网络优化)
    MachineLearning入门-9(数据准备)
    百度PaddlePaddle入门-12(损失函数)
    MachineLearning入门-8(数据可视化)
    百度PaddlePaddle入门-11(网络结构)
    页面生命周期
    JS控制开灯关灯
    JavaScript基础知识
  • 原文地址:https://www.cnblogs.com/newcityboy/p/11886485.html
Copyright © 2011-2022 走看看