欢迎加入技术交流群:278947305Spring配置文件
<!-- 通过构造方法来配置bean 的属性 --> <bean id="car" class="com.spring2.Car"> <constructor-arg value="奥迪" index="0"></constructor-arg> <constructor-arg value="北京" index="1"></constructor-arg> <constructor-arg value="3000" type="int"></constructor-arg> </bean> <bean id="cars" class="com.spring2.Car"> <constructor-arg value="卡布奇偌" type="java.lang.String"></constructor-arg> <constructor-arg value="上海" type="java.lang.String"></constructor-arg> <constructor-arg value="50000.2" type="double"></constructor-arg> </bean>
测试类
package com.spring2; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestCar { public static void main(String[] args) { ApplicationContext ctx =new ClassPathXmlApplicationContext("appcotion.xml"); Car car = (Car) ctx.getBean("car"); System.out.println(car); car = (Car) ctx.getBean("cars"); System.out.println(car); } }
package com.spring2; /** * spring 是注入 * 通过构造方法来配置 bean的属性 * @author Striver date 2014年4月7日 */ public class Car { private String brand; private String corp; private double price; private int maxSpeed; public Car(String brand, String corp, double price) { super(); this.brand = brand; this.corp = corp; this.price = price; } public Car(String brand, String corp, int maxSpeed) { super(); this.brand = brand; this.corp = corp; this.maxSpeed = maxSpeed; } @Override public String toString() { return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price + ", maxSpeed=" + maxSpeed + "]"; } }
Spring4新特征
最新推荐文章于 2023-05-29 10:32:38 发布