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();
}
}
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();
}
}
相关文章推荐
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- Spring源码学习之: 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- Spring中bean的初始化和销毁之前的方法XML和注解的两种形式
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作