在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. 使用GIT进行源码管理 —— 在VisualStudio中使用GIT

    GIT作为源码管理的方式现在是越来越流行了,在VisualStudio 2012中,就通过插件的现实对GIT进行了官方支持,并且这个插件在VS2013中已经转正.本文在这里简单的介绍一下如何在Visu ...

  2. AEAI HR_v1.5.2升级说明,开源人力资源管理系统

    1.升级说明 本次AEAI HR升级内容主要是针对数通畅联推出AEAI ECP企业云联平台而升级的,其中对AEAI HR的各模块进行扩展,同时增加了移动门户版功能及为AEAI ECP提供数据服务接口. ...

  3. [转载】——故障排除:Shared Pool优化和Library Cache Latch冲突优化 (文档 ID 1523934.1)

    原文链接:https://support.oracle.com/epmos/faces/DocumentDisplay?_adf.ctrlstate=23w4l35u5_4&id=152393 ...

  4. CSS3:text-overflow实现文字截取,超出部分显示省略号

    1. 概述 使用text-overflow:ellipsis对溢出文本显示省略号有两个好处, 一是不用通过后端程序截取: 二是有利于SEO. 2. text-overflow的属性 clip: 当对象 ...

  5. 关于Android中new Notification

    目前 Android 已经不推荐使用下列方式创建 Notification实例: Notification notification = new Notification(R.drawable.ic_ ...

  6. freeradius整合AD域作anyconncet认证服务器

    一.服务器要求 Radius服务器:centos6.6.hostname.selinux  disabled.stop iptables AD域服务器:Windows Server 2008 R2 E ...

  7. 搭建基于PHP的www服务器

    安装MySQL #!/bin/bash mount |grep "/dev/sr0" if [ "$?" != 0 ];then mount /dev/sr0 ...

  8. BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]

    1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 786  Solved: 391[Submit][S ...

  9. xcode8权限以及相关设置

    我们需要打开info.plist文件添加相应权限的说明,否则程序在iOS10上会出现崩溃. 具体如下图: QQ20160914-0.png 注意,添加的时候,末尾不要有空格麦克风权限:Privacy ...

  10. 使用jOrgChart插件, 异步加载生成组织架构图

    jOrgChart插件是一个用来实现组织结构图的Jquery的插件- 一.特点 1.支持拖拽修改子节点: 2.支持节点缩放展示: 3.方便修改css定义样式: 4.超轻量型: 5.兼容性好,基本支持所 ...