• 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);
        }
    }
  • 相关阅读:
    CentOS7 使用定时任务
    springboot整合mybatis+mysql+druid数据源
    js 防抖和节流,封装成函数
    vue 单页面项目卸载依赖和插件的方式
    Tomcat部署vue单页面项目
    linux DRM/KMS 测试工具 modetest、kmscude、igt-gpu-tools (二)
    linux DRM/KMS 测试工具 modetest、kmscude、igt-gpu-tools (一)
    软构设计模式简单归纳
    软件构造复习中关于不变性的一个疑问以及代码验证
    代码快照图与可变不可变
  • 原文地址:https://www.cnblogs.com/lwx521/p/7774010.html
Copyright © 2020-2023  润新知