zoukankan      html  css  js  c++  java
  • Spring

    BeanFactory & ApplicationContext

    org.springframework.beans.factory.BeanFactory 是最基本的 Spring 容器接口,它提供了管理 Bean 的一些基本功能。 BeanFactory 接口包含如下几个基本方法:
      Object getBean(String name) throws BeansException;
      <T> T getBean(String name, Class<T> requiredType) throws BeansException;
      boolean containsBean(String name);
      Class<?> getType(String name) throws NoSuchBeanDefinitionException;

    org.springframework.context.ApplicationContext 是 org.springframework.beans.factory.BeanFactory 的子接口,除了提供 BeanFactory 所支持的功能,ApplicationContext 还提供了一些额外的功能,如国际化支持、事件机制、资源访问等。

    Spring 配置文件的基本结构

    <?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="..." class="..." >
            <property name="..." value="..." />
            <property name="..." ref="..." />
        </bean>
        
    </beans>

    实例化容器

    按文件系统路径加载配置文件:

    ApplicationContext appCtx = new FileSystemXmlApplicationContext("src/applicationContext.xml");

    按类路径加载配置多件:

    ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");

    加载多个配置文件:

    ApplicationContext appCtx = new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});

    使用 Spring 容器

    ExampleBean example = appCtx.getBean("example", ExampleBean.class);
  • 相关阅读:
    【LeetCode】226. Invert Binary Tree
    【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree
    【LeetCode】191. Number of 1 Bits
    【LeetCode】122. Best Time to Buy and Sell Stock II
    【LeetCode】100. Same Tree
    【LeetCode】237. Delete Node in a Linked List
    【LeetCode】136. Single Number
    【LeetCode】104. Maximum Depth of Binary Tree
    svn tree conflicts 解决方法
    sed详解
  • 原文地址:https://www.cnblogs.com/huey/p/4506754.html
Copyright © 2011-2022 走看看