您的位置:首页 > 编程语言 > Java开发

Spring中通过stter方法,构造器两种形式对属性进行初始化

2016-11-18 21:13 274 查看
1:Car类

package com.study.bean;

public class Car {

private String brand;

private String corp;

private double price;

private int maxSpeed;

public Car() {

}

public String getBrand() {

return brand;

}

public void setBrand(String brand) {

this.brand = brand;

}

public String getCorp() {

return corp;

}

public void setCorp(String corp) {

this.corp = corp;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

public int getMaxSpeed() {

return maxSpeed;

}

public void setMaxSpeed(int maxSpeed) {

this.maxSpeed = maxSpeed;

}

public Car(String brand, String corp, int maxSpeed) {

super();

this.brand = brand;

this.corp = corp;

this.maxSpeed = maxSpeed;

}

public Car(String brand, String corp, double price) {

super();

this.brand = brand;

this.corp = corp;

this.price = price;

}

public String toString() {

return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price

+ ", maxSpeed=" + maxSpeed + "]";

}

}

2:applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 配置bean class:bean的全类名,通过反射的方式在IOC容器实例化对象,中id是唯一的-->

<bean id="helloWorld" class="com.study.bean.HelloWorld">

<property name="name" value="Spring" />

</bean>

<!--通过构造方法配置属性 -->

<bean id="car" class="com.study.bean.Car">

<constructor-arg value="奔驰" index="0"></constructor-arg>

<constructor-arg value="上海" index="1"></constructor-arg>

<constructor-arg value="300000.0" index="2"></constructor-arg>

<property name="maxSpeed" value="100" />

</bean>

<bean id="car2" class="com.study.bean.Car">

<constructor-arg value="奔驰" ></constructor-arg>

<constructor-arg value="上海" ></constructor-arg>

<constructor-arg value="100" ></constructor-arg>

<property name="price" value="5000000" />

</bean>

</beans>

3:javaBean

package com.study.bean;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestMain {

public static void main(String[] args) {

// HelloWorld helloworld=new HelloWorld();

// helloworld.setName("Spring");

// helloworld.hello();

//1:创建Spring的IOC容器对象 ApplicationContext是IOC容器的接口,

//ClassPathXmlApplicationContext是ApplicationContext接口的实现类,从类路径下加载

ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");

//2:从IOC容器中获取Bean实例 通过id获取,

//HelloWorld helloWorld=(HelloWorld)ctx.getBean("helloWorld");

//通过类名从IOC容器中获取Bean实例,要求一个类只能有一个Bean实例

//HelloWorld helloWorld2=(HelloWorld) ctx.getBean(HelloWorld.class);

//3:调用hello方法

//helloWorld.hello();

Car car=(Car) ctx.getBean("car");

System.out.println(car);

Car car2=(Car) ctx.getBean("car2");

System.out.println(car2);

//helloWorld2.hello();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐