在spring中, 可以定义多个配置文件。

例子:

①首先定义一个班级类和一个学生类

1 package com.fuwh.spring;

 2 
 3 public class Clazz {
 4     
 5     private String name;
 6     private int grade;
 7 
 8     public int getGrade() {
 9         return grade;
     }
     public void setGrade(int grade) {
         this.grade = grade;
     }
     public Clazz() {
         System.out.println("Clazz类被初始化");
         // TODO Auto-generated constructor stub
     }
     public String getName() {
         return name;
     }
     public void setName(String name) {
         this.name = name;
     }
     @Override
     public String toString() {
         return "Student [name=" + name + ", grade=" + grade + "]";
     }
     
 }

1 package com.fuwh.spring;

 2 
 3 public class Student {
 4     
 5     private String name;
 6     private int age;
 7     private Clazz clazz;
 8     
 9     public Clazz getClazz() {
         return clazz;
     }
     public void setClazz(Clazz clazz) {
         this.clazz = clazz;
     }
     public Student() {
         System.out.println("Student类被初始化");
         // TODO Auto-generated constructor stub
     }
     public String getName() {
         return name;
     }
     public void setName(String name) {
         this.name = name;
     }
     public int getAge() {
         return age;
     }
     public void setAge(int age) {
         this.age = age;
     }
     @Override
     public String toString() {
         return "Student [name=" + name + ", age=" + age + ",grade=" + clazz.getGrade()+clazz.getName() +"]";
     }
 }

② 定义两个配置文件,分别配置clazz(beanClazz)和student(studentClazz)

1 <?xml version="1.0" encoding="UTF-8"?>

 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7     <!-- services -->
 8     
 9     <bean id="clazz" class="com.fuwh.spring.Clazz" lazy-init="default">
         <property name="name" value="信息与计算科学"></property>
         <property name="grade" value="08"></property>
     </bean>
     
 </beans>

1 <?xml version="1.0" encoding="UTF-8"?>

 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7     <!-- services -->
 8     
 9     <bean id="student" class="com.fuwh.spring.Student" lazy-init="default">
         <property name="name" value="fengzi"></property>
         <property name="age" value="23"></property>
         <property name="clazz" ref="clazz"></property>
     </bean>
     
 </beans>

③接下来测试一下,由于使用了多个配置文件的方式,所以在实例化applicationContext的时候,需要给ClassPathXmlApplicationContext传递一个配置文件的数组

 1 package com.fuwh.spring;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class Spring01 {
 7 
 8     public static void main(String[] args) {
 9         
 //        ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
         ApplicationContext ac=new ClassPathXmlApplicationContext(new String[]{"beanStudent.xml","beanClazz.xml"});
         System.out.println(ac.getBean("student"));
     }

14 }

★也可以在beanStudent.xml文件中,使用如下命令,将beanClazz.xml文件引入进来。这样在实例化ApplicationContext的时候,就只需要传入beanStudent.xml文件即可。

【<import resource="beanClazz.xml"/>】

Spring--多人开发的更多相关文章

  1. 学习spring1--跟我一起学Spring 3(2)–开发环境配置

    http://www.importnew.com/13185.html#spring     首页 所有文章 资讯 Web 架构 基础技术 书籍 教程 我要投稿 更多频道 » - 导航条 - 首页 所 ...

  2. 转: 基于netty+ protobuf +spring + hibernate + jgroups开发的游戏服务端

    from: http://ybak.iteye.com/blog/1853335 基于netty+ protobuf +spring + hibernate + jgroups开发的游戏服务端 游戏服 ...

  3. 【Spring注解驱动开发】组件注册-@ComponentScan-自动扫描组件&指定扫描规则

    写在前面 在实际项目中,我们更多的是使用Spring的包扫描功能对项目中的包进行扫描,凡是在指定的包或子包中的类上标注了@Repository.@Service.@Controller.@Compon ...

  4. 【Spring注解驱动开发】自定义TypeFilter指定@ComponentScan注解的过滤规则

    写在前面 Spring的强大之处不仅仅是提供了IOC容器,能够通过过滤规则指定排除和只包含哪些组件,它还能够通过自定义TypeFilter来指定过滤规则.如果Spring内置的过滤规则不能够满足我们的 ...

  5. 【Spring注解驱动开发】使用@Scope注解设置组件的作用域

    写在前面 Spring容器中的组件默认是单例的,在Spring启动时就会实例化并初始化这些对象,将其放到Spring容器中,之后,每次获取对象时,直接从Spring容器中获取,而不再创建对象.如果每次 ...

  6. 【Spring注解驱动开发】使用@Lazy注解实现懒加载

    写在前面 Spring在启动时,默认会将单实例bean进行实例化,并加载到Spring容器中.也就是说,单实例bean默认在Spring容器启动的时候创建对象,并将对象加载到Spring容器中.如果我 ...

  7. 【Spring注解驱动开发】使用@Import注解给容器中快速导入一个组件

    写在前面 我们可以将一些bean组件交由Spring管理,并且Spring支持单实例bean和多实例bean.我们自己写的类,可以通过包扫描+标注注解(@Controller.@Servcie.@Re ...

  8. 【Spring注解驱动开发】在@Import注解中使用ImportSelector接口导入bean

    写在前面 在上一篇关于Spring的@Import注解的文章<[Spring注解驱动开发]使用@Import注解给容器中快速导入一个组件>中,我们简单介绍了如何使用@Import注解给容器 ...

  9. 【Spring注解驱动开发】面试官:如何将Service注入到Servlet中?朋友又栽了!!

    写在前面 最近,一位读者出去面试前准备了很久,信心满满的去面试.没想到面试官的一个问题把他难住了.面试官的问题是这样的:如何使用Spring将Service注入到Servlet中呢?这位读者平时也是很 ...

  10. 【Spring注解驱动开发】在@Import注解中使用ImportBeanDefinitionRegistrar向容器中注册bean

    写在前面 在前面的文章中,我们学习了如何使用@Import注解向Spring容器中导入bean,可以使用@Import注解快速向容器中导入bean,小伙伴们可以参见<[Spring注解驱动开发] ...

随机推荐

  1. React Native之坑总结(持续更新)

    React Native之坑总结(持续更新) Genymotion安装与启动 之前我用的是蓝叠(BlueStack)模拟器,跑RN程序也遇到了一些问题,都通过搜索引擎解决了,不过没有记录. 但是Blu ...

  2. 脱离spring集成cxf(基于nutz框架)

    什么是webService WebService是一种跨编程语言和跨操作系统平台的远程调用技术. 理论资料: http://blog.csdn.net/wooshn/article/details/8 ...

  3. Mybatis 查不到数据,总是返回Null

    mybatis突然查不到数据,查询返回的都是Null,但是 select count(*) from xxx查询数量,返回却是正常的. Preparing: SELECT id,a9004,a9005 ...

  4. OpenWRT镜像爬虫搭建本地源

    网上的爬虫不能用,还是先表达谢意,不过我比较懒不喜欢重复写别人写的教程,只贴出修改,怎么用自己看教程吧. 我自己改了一版可以正常爬: #!/usr/bin/env python #coding=utf ...

  5. 如何配置远程mysql服务器

    如何配置远程mysql服务器 分配用户权限 可以先看一下目前的用户权限状况: use mysql; select host,user,password from user; 然后分配新的权限给某一用户 ...

  6. hdu1695 GCD(莫比乌斯反演)

    题意:求(1,b)区间和(1,d)区间里面gcd(x, y) = k的数的对数(1<=x<=b , 1<= y <= d). 知识点: 莫比乌斯反演/*12*/ 线性筛求莫比乌 ...

  7. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  8. nginx配置

    先在 cd /usr/local/nginx/conf 目录下找到 nginx.conf 文件 user www www; worker_processes 8; error_log /home/ww ...

  9. Batis-iBatis基本操作(增删改查)

    Batis-iBatis基本操作(增删改查) 时间 2014-04-10 17:55:20  CSDN博客 原文  http://blog.csdn.net/mazhaojuan/article/de ...

  10. JavaScript 属性类型(数据属性和访问器属性)

    数据属性 数据属性包含一个数据值的位置.在这个位置可以读取和写入值.数据属性有 4 个描述其行为的特性. [[Configurable]]:表示能否通过 delete 删除属性从而重新定义属性,能否修 ...