Spring 入门总结

来源:互联网 发布:网络报案中心我要报案 编辑:程序博客网 时间:2024/06/07 06:05

下面的例子显示了基于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">    <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>

Spring的IoC容器实例化一个很简单。位置路径或路径提供给一个ApplicationContext构造函数实际上是资源字符串,使容器加载配置元数据等各种各样的外部资源的本地文件系统,从Java类路径,等等。

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

你可以使用应用程序上下文从所有这些构造函数加载bean定义XML片段。这个构造函数多个资源的位置,例如



<beans>    <import resource="services.xml"/>    <import resource="resources/messageSource.xml"/>    <import resource="/resources/themeSource.xml"/>    <bean id="bean1" class="..."/>    <bean id="bean2" class="..."/></beans>

Instantiation with a static factory method

<bean id="clientService"    class="examples.ClientService"    factory-method="createInstance"/>

public class ClientService {    private static ClientService clientService = new ClientService();    private ClientService() {}    public static ClientService createInstance() {        return clientService;    }}

Instantiation using an instance factory method

<!-- the factory bean, which contains a method called createInstance() --><bean id="serviceLocator" class="examples.DefaultServiceLocator">    <!-- inject any dependencies required by this locator bean --></bean><!-- the bean to be created via the factory bean --><bean id="clientService"    factory-bean="serviceLocator"    factory-method="createClientServiceInstance"/>

public class DefaultServiceLocator {    private static ClientService clientService = new ClientServiceImpl();    private DefaultServiceLocator() {}    public ClientService createClientServiceInstance() {        return clientService;    }}

One factory class can also hold more than one factory method as shown here:
<bean id="serviceLocator" class="examples.DefaultServiceLocator">    <!-- inject any dependencies required by this locator bean --></bean><bean id="clientService"    factory-bean="serviceLocator"    factory-method="createClientServiceInstance"/><bean id="accountService"    factory-bean="serviceLocator"    factory-method="createAccountServiceInstance"/>

public class DefaultServiceLocator {    private static ClientService clientService = new ClientServiceImpl();    private static AccountService accountService = new AccountServiceImpl();    private DefaultServiceLocator() {}    public ClientService createClientServiceInstance() {        return clientService;    }    public AccountService createAccountServiceInstance() {        return accountService;    }}




0 0
原创粉丝点击