zoukankan      html  css  js  c++  java
  • Spring--多人开发


    在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;
    10     }
    11     public void setGrade(int grade) {
    12         this.grade = grade;
    13     }
    14     public Clazz() {
    15         System.out.println("Clazz类被初始化");
    16         // TODO Auto-generated constructor stub
    17     }
    18     public String getName() {
    19         return name;
    20     }
    21     public void setName(String name) {
    22         this.name = name;
    23     }
    24     @Override
    25     public String toString() {
    26         return "Student [name=" + name + ", grade=" + grade + "]";
    27     }
    28     
    29 }

      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() {
    10         return clazz;
    11     }
    12     public void setClazz(Clazz clazz) {
    13         this.clazz = clazz;
    14     }
    15     public Student() {
    16         System.out.println("Student类被初始化");
    17         // TODO Auto-generated constructor stub
    18     }
    19     public String getName() {
    20         return name;
    21     }
    22     public void setName(String name) {
    23         this.name = name;
    24     }
    25     public int getAge() {
    26         return age;
    27     }
    28     public void setAge(int age) {
    29         this.age = age;
    30     }
    31     @Override
    32     public String toString() {
    33         return "Student [name=" + name + ", age=" + age + ",grade=" + clazz.getGrade()+clazz.getName() +"]";
    34     }
    35 }

    ② 定义两个配置文件,分别配置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">
    10         <property name="name" value="信息与计算科学"></property>
    11         <property name="grade" value="08"></property>
    12     </bean>
    13     
    14 </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">
    10         <property name="name" value="fengzi"></property>
    11         <property name="age" value="23"></property>
    12         <property name="clazz" ref="clazz"></property>
    13     </bean>
    14     
    15 </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         
    10 //        ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
    11         ApplicationContext ac=new ClassPathXmlApplicationContext(new String[]{"beanStudent.xml","beanClazz.xml"});
    12         System.out.println(ac.getBean("student"));
    13     }

    14 } 

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

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

  • 相关阅读:
    day7 面向对象 静态方法 类方法 属性方法 类的特殊成员方法 元类 反射 异常处理
    day6 面向对象 封装 继承 多态 类与实例在内存中的关系 经典类和新式类
    day5 time datetime random os sys shutil json pickle shelve xml configparser hashlib subprocess logging re正则 python计算器
    kafka常用操作命令
    linux基础
    django学习1——初识web应用程序
    mysql数据库(三)——pymysql模块
    mysql数据库(二)——表的查询
    mysql数据库(一)
    Python常用模块——re模块
  • 原文地址:https://www.cnblogs.com/zerotomax/p/5700885.html
Copyright © 2011-2022 走看看