• Bean的基于XML配置方式


    基于XML配置

    Beans.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <beans xmlns="http://www.springframework.org/schema/beans"       1.默认命名空间
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        2.xsi标准命名空间,用于指定自定义命名空间的Schema文件
        xmlns:p="http://www.springframework.org/schema/p"            3.声明p命名空间
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
             http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
    <bean id="car" class="com.smart.ditype.Car"                      4.id为bean的名称,可以通过getbean("car")获取容器中的Bean,class为Bean的类名
          p:brand="红旗123&amp;CA72"                                  5.采用p命名空间配置Bean属性值
          p:maxSpeed="100"
          p:price="20000.00"/>
    </beans>

    Car.class

    package com.smart.ditype;
    
    public class Car {
        public String brand;
        private String corp;    
        private double price;
        private int maxSpeed;
    
        public Car() {}    
        public Car(String brand, double price) {
            this.brand = brand;
            this.price = price;
        }    
    
        public Car(String brand, String corp, double price) {
            this.brand = brand;
            this.corp = corp;
            this.price = price;
        }
        public Car(String brand, String corp, int maxSpeed) {
            this.brand = brand;
            this.corp = corp;
            this.maxSpeed = maxSpeed;
        }
    
        public String getBrand() {
            return brand;
        }
    
        public void setBrand(String brand) {
            this.brand = brand;
        }
    
        public int getMaxSpeed() {
            return maxSpeed;
        }
    
        public void setMaxSpeed(int maxSpeed) {
            this.maxSpeed = maxSpeed;
        }
    
        public double getPrice() {
            return price;
        }
    
        public void setPrice(double price) {
            this.price = price;
        }
        
        public String toString(){
            return "brand:"+brand+"/maxSpeed:"+maxSpeed+"/price:"+price;
        }
    
    }

    Test

    package com.smart.ditype;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.testng.annotations.*;
    import  static  org.testng.Assert.*;
    
    public class DiTypeTest {
        public ApplicationContext factory = null;
    
        private static String[] CONFIG_FILES = { "com/smart/ditype/beans.xml" };
    
        @BeforeClass
        public void setUp() throws Exception {
            factory = new ClassPathXmlApplicationContext(CONFIG_FILES);
        }
    
        @Test
        public void testCar(){
            Car car = (Car)factory.getBean("car");
            assertNotNull(car);
            System.out.println(car);
        }
    }
  • 相关阅读:
    EEPROM芯片AT2402驱动
    FPGA 状态机(FSM)的三段式推荐写法
    1602液晶驱动
    Bresenham快速画直线算法
    I2C总线驱动程序
    从数据库中取时间类型显示
    C# 页面关联类似模式窗口
    C# 页面javascript 页面跳转刷新
    网页有趣的时间显示控件
    DataSet
  • 原文地址:https://www.cnblogs.com/lwx521/p/7774010.html
Copyright © 2020-2023  润新知