Spring 配置Bean

在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 容器

在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>


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值