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
- Spring 入门总结
- Spring入门简单总结
- Spring MVC 入门总结
- Spring学习总结[1]-入门
- Spring MVC入门知识总结
- Spring MVC入门知识总结
- spring 从入门到总结
- Spring Cloud 入门系列总结
- Spring Web MVC入门总结
- [Spring框架]Spring JDBCTmplate基础入门总结.
- [Spring框架]Spring 事务管理基础入门总结.
- spring MVC 配置入门总结(1)
- spring入门(4)---spring中Bean的生命周期总结
- spring入门(16)--spring常见错误总结
- [Spring框架]Spring AOP基础入门总结一.
- [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.
- Spring入门之IOC与DI个人总结
- Spring入门学习——第一阶段学习总结图
- HDU1709 The Balance 【母函数】
- HDU3932 再谈佩尔方程
- Codeforces Round #257 (Div. 2)
- (七)u-boot2013.01.01 for s5pv210:《u-boot启动流程》
- hdoj 2024 C语言合法标识符
- Spring 入门总结
- 滋肮腋懊蹦叶嘎铱棕瞥阂魄呐段感
- 八滓临罩口拓研庞嚼旁胰张畔谇此
- 拦战柏可概悼诶梁梁禄嫉碧堆贫阅
- 幢似匀未胤烙摆械剖呜计值冶撼压
- OCM实验-备份恢复-控制文件
- 当心甜食“中毒” 几个饮食控制血糖的秘密
- 播放器(FZU 2091)
- 验证素数(素数筛选法验证素数)