zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    webpack和webpack-dev-server安装配置
    webpack和webpack-dev-server安装配置
    Json和Jsonp
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    读undo 导致db file sequential read
    direct path read 研究
    SpringCloud的版本
    521忘记送大家礼物了,补上!
  • 原文地址:https://www.cnblogs.com/lwx521/p/7774010.html
Copyright © 2011-2022 走看看