首页 > 编程语言 > 详细

Spring-初始Spring-02

时间:2020-11-29 22:58:28      阅读:35      评论:0      收藏:0      [点我收藏+]

参考文档


reference:

技术分享图片


core:

技术分享图片


Hello Version 5.3.1:

技术分享图片


1.2.容器概述

The org.springframework.context.ApplicationContext interface represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans.

org.springframework.context.ApplicationContext接口代表Spring IoC容器,并负责实例化,配置和组装Bean。

The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata.

容器通过读取配置元数据,获取有关要实例化、配置和组装哪些对象的指令。

The configuration metadata is represented in XML, Java annotations, or Java code.

配置元数据以XML,Java注解或Java代码表示。

1.2.1.配置元数据

Configuration Metadata


For information about using other forms of metadata with the Spring container:

  • Annotation-based configuration: Spring 2.5 introduced support for annotation-based configuration metadata.
  • Java-based configuration: Starting with Spring 3.0, many features provided by the Spring JavaConfig project became part of the core Spring Framework. Thus, you can define beans external to your application classes by using Java rather than XML files.

有关使用Spring容器中其他形式的元数据的信息:

  • 基于注解的配置:Spring 2.5引入了基于注解配置元数据的支持。
  • 基于Java的配置:从Spring 3.0开始,Spring JavaConfig项目提供的许多功能已成为核心Spring Framework的一部分。因此,你可以使用Java而不是XML文件来定义你应用程序类外部的bean。

the basic structure of XML-based configuration metadata:

基于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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="..." class="...">  
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions go here -->

</beans>

1.2.2.实例化容器

Instantiating a Container


The location path or paths supplied to an ApplicationContext constructor are resource strings that let the container load configuration metadata from a variety of external resources, such as the local file system, the Java CLASSPATH, and so on.

提供给ApplicationContext构造器的(本地一个或多个)路径是资源字符串,它可让容器从各种外部资源加载配置元数据,例如本地文件系统,JavaCLASSPATH等。


java实现加载配置的元数据:

ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");

the service layer objects (services.xml) configuration file:

服务层对象services.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- services -->

    <bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
        <property name="accountDao" ref="accountDao"/>
        <property name="itemDao" ref="itemDao"/>
        <!-- additional collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions for services go here -->

</beans>

the data access objects daos.xml file:

数据访问对象daos.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="accountDao"
        class="org.springframework.samples.jpetstore.dao.jpa.JpaAccountDao">
        <!-- additional collaborators and configuration for this bean go here -->
    </bean>

    <bean id="itemDao" class="org.springframework.samples.jpetstore.dao.jpa.JpaItemDao">
        <!-- additional collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions for data access objects go here -->

</beans>

Composing XML-based Configuration Metadata

组成基于XML配置的元数据


It can be useful to have bean definitions span multiple XML files. Often, each individual XML configuration file represents a logical layer or module in your architecture.

使bean定义跨越多个XML文件可能很有用。通常,每个单独的XML配置文件在你的体系结构中都代表一个逻辑层或一个模块。


You can use the application context constructor to load bean definitions from all these XML fragments. This constructor takes multiple Resource locations, as was shown in the [previous section]. Alternatively, use one or more occurrences of the <import/> element to load bean definitions from another file or files. The following example shows how to do so:

你可以使用application context的构造函数,从这些XML片段中加载bean的定义。如上[一节中]所示,该构造函数具有多个资源位置 。或者使用一个或多个<import/>元素从另一个文件中加载bean的定义。以下示例显示了如何执行此操作:

<beans>
    <import resource="services.xml"/>
    <import resource="resources/messageSource.xml"/>
    <import resource="/resources/themeSource.xml"/>

    <bean id="bean1" class="..."/>
    <bean id="bean2" class="..."/>
</beans>

1.2.3.使用容器

Using the Container


The ApplicationContext lets you read bean definitions and access them,

ApplicationContext使你能够查看bean的定义并访问它们。

// create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");

// retrieve configured instance
PetStoreService service = context.getBean("petStore", PetStoreService.class);

// use configured instance
List<String> userList = service.getUsernameList();

Spring-初始Spring-02

原文:https://www.cnblogs.com/2020-6-12/p/14057077.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!