在spring配置文件中,配置bean,
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置bean
class:bean的全类名,通过反射的方式在IOC容器中创建Bean,所以要求Bean中必须有
无参构造器
id:标识容器中的bean,id唯一
-->
<bean id="helloWorld" class="com.hcx.spring.beans.HelloWord">
<!--属性的注入 -->
<property name="name" value="Spring"></property>
</bean>
<!-- 通过构造方法配置Bean的属性 -->
<bean id="car" class="com.hcx.spring.beans.Car">
<constructor-arg value="Audi" index="0"></constructor-arg>
<constructor-arg value="Shanghai" index="1"></constructor-arg>
<constructor-arg value="300000" type="double"></constructor-arg>
</bean>
<!-- 使用构造器注入,可以指定参数的位置和参数类型,来区分重载构造器! -->
<bean id="car2" class="com.hcx.spring.beans.Car">
<constructor-arg value="BaoMa" type="java.lang.String"></constructor-arg>
<constructor-arg type="java.lang.String">
<value><![CDATA[<Shanghai^>]]></value>
</constructor-arg>
<constructor-arg type="int">
<value>250</value>
</constructor-arg>
</bean>
<bean id="person" class="com.hcx.spring.beans.Person">
<property name="name" value="Tom"></property>
<property name="age" value="24"></property>
<!-- 使用property元素的ref属性建立Bean间的关系 -->
<property name="car" ref="car2"></property>
</bean>
</beans>
对应的Bean:
HelloWorld.java
public class HelloWord {
private String name;
public void setName(String name){
System.out.println("setName:" + name);
this.name = name;
}
public void hello(){
System.out.println("hello world+"+ name);
}
public HelloWord(String name) {
super();
this.name = name;
}
public HelloWord() {
System.out.println("Helloworld's Constructor ...");
}
}
public class Car {
private String brand;
private String corp;
private double price;
private int maxSpeed;
//构造器1
public Car(String brand, String corp, double price) {
super();
this.brand = brand;
this.corp = corp;
this.price = price;
}
// 构造器2
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 + "]";
}
}<strong>
</strong>
在SpringIOC容器读取Bean配置创建Bean实例之前,必须对容器实例化。
Spring提供两种类型的IOC容器实现
---BeanFactory:IOC容器基本实现()
---ApplicationContext:提供了更多高级特性,是BeanFactory的子接口
主要使用ApplicationContext。
Main函数
public class Main {
public static void main(String[] args) {
/*
//创建HelloWorld的一个对象
HelloWord helloworld=new HelloWord();
//为name属性赋值
helloworld.setName("hcx");
*/
//使用Spring方式
//1.创建Spring的IOC容器对象
// ClassPathXmlApplicationContext是applicationcontext的实现类,从类路径加载
ApplicationContext ctx= new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从IOC容器中获取Beanj实例
HelloWord helloworld = (HelloWord) ctx.getBean("helloWorld");
//使用class方法获得bean:必须在xml配置文件中有唯一bean,否则报错
HelloWord h2= ctx.getBean(HelloWord.class);
// 调用helo方法
helloworld.hello();
//Car中因为有两个构造器,所有在xml配置文件中,要以type或index属性来标记参数类型
Car car=(Car) ctx.getBean("car2");
System.out.println(car);
}
}<strong>
</strong>
1156

被折叠的 条评论
为什么被折叠?



