码迷,mamicode.com
首页 > 编程语言 > 详细

Spring---IOC---扩展

时间:2020-06-20 15:40:03      阅读:45      评论:0      收藏:0      [点我收藏+]

标签:cap   text   tst   var   fine   load   resource   str   interface   

技术图片

1、Configuration Metadata

    As the preceding diagram shows, the Spring IoC container consumes a form of configuration metadata.

    Spring提供的Configuration Metadata方式:

        XML-based metadata;      

<?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>

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

</beans>

        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. To use these new features, see the @Configuration, @Bean, @Import, and @DependsOn annotations.

2、Instantiating a Container

    the container load configuration metadata from a variety of external resources, such as the local file system, the Java CLASSPATH, and so on.

---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>

---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>


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

 3、Using the Container

    The ApplicationContext is the interface for an advanced factory capable of maintaining a registry of different beans and their dependencies.

    By using the method T getBean(String name, Class<T> requiredType), you can retrieve instances of your beans.

// 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---IOC---扩展

标签:cap   text   tst   var   fine   load   resource   str   interface   

原文地址:https://www.cnblogs.com/anpeiyong/p/13168646.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!