Spring 1 IoC 1 开始

Spring  IoC容器的基础都在 org.springframework.beans 和 org.springframework.context 包中。提供了2类容器实现:org.springframework.beans.factory. BeanFactory 和 org.springframework.context. ApplicationContext,主要使用的是 ApplicationContext

经常使用的3个ApplicationContext的实现:
ClassPathXmlApplicationContextFileSystemXmlApplicationContextXmlWebApplicationContext

ApplicationContextcontext=new FileSystemXmlApplicationContext("c:/foo.xml");
ApplicationContextcontext=new ClassPathXmlApplicationContext("foo.xml");

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.1.xsd">
     <import resource=”resource1.xml”/> 
    <!-- oracle bean used for a few examples -->
    <bean id="oracle" name= "wiseworm" class="com.apress.prospring3.ch4.BookwormOracle" />
</beans>

import用于导入其他配置文件,<import>标签可以放在<beans>下的任何位置,没有顺序关系。可以使用下面格式:
file:C:/config/services.xml
classpath:/config/services.xml


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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd">
</beans>


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

PetStoreServiceImpl service = context.getBean("petStore", PetStoreServiceImpl.class);

List userList = service.getUsernameList();

public  class  DeclareSpringComponents {
         public  static  void  main(String[] args) {
              GenericXmlApplicationContext ctx =  new  GenericXmlApplicationContext();
              ctx.load(  "classpath:app-context-xml.xml"  );
                //ctx.load("classpath:app-context-annotation.xml");
              ctx.refresh();             
              
              MessageProvider messageProvider = ctx.getBean(  "messageProvider" , MessageProvider. class  );
              System.  out .println(messageProvider.getMessage());
       }
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值