Spring 配置Bean

来源:互联网 发布:deepin linux 假死 编辑:程序博客网 时间:2024/05/22 11:01

在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"><span style="white-space:pre"></span><constructor-arg value="Audi" index="0"></constructor-arg><span style="white-space:pre"></span><constructor-arg value="Shanghai" index="1"></constructor-arg><span style="white-space:pre"></span><constructor-arg value="300000" type="double"></constructor-arg></bean><span style="white-space:pre"></span><!-- 使用构造器注入,可以指定参数的位置和参数类型,来区分重载构造器! --><span style="white-space:pre"></span><bean id="car2" class="com.hcx.spring.beans.Car"><span style="white-space:pre"></span><constructor-arg value="BaoMa" type="java.lang.String"></constructor-arg><span style="white-space:pre"></span><constructor-arg type="java.lang.String"><span style="white-space:pre"></span><value><![CDATA[<Shanghai^>]]></value><span style="white-space:pre"></span></constructor-arg><span style="white-space:pre"></span><constructor-arg  type="int"><span style="white-space:pre"></span><value>250</value><span style="white-space:pre"></span></constructor-arg><span style="white-space:pre"></span></bean><span style="white-space:pre"></span><span style="white-space:pre"></span><bean id="person" class="com.hcx.spring.beans.Person"><span style="white-space:pre"></span><property name="name" value="Tom"></property><span style="white-space:pre"></span><property name="age" value="24"></property><span style="white-space:pre"></span><!-- 使用property元素的ref属性建立Bean间的关系 --><span style="white-space:pre"></span><property name="car" ref="car2"></property><span style="white-space:pre"></span></bean></beans>

 

对应的Bean:

HelloWorld.java

public class HelloWord {<span style="white-space:pre"></span>private String name;<span style="white-space:pre"></span>public void setName(String name){<span style="white-space:pre"></span>System.out.println("setName:" + name);<span style="white-space:pre"></span>this.name = name;<span style="white-space:pre"></span>}<span style="white-space:pre"></span><span style="white-space:pre"></span>public void hello(){<span style="white-space:pre"></span>System.out.println("hello world+"+ name);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public HelloWord(String name) {<span style="white-space:pre"></span>super();<span style="white-space:pre"></span>this.name = name;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public HelloWord() {<span style="white-space:pre"></span>System.out.println("Helloworld's Constructor ...");<span style="white-space:pre"></span>}<span style="white-space:pre"></span><span style="white-space:pre"></span>}

public class Car {private String brand;private String corp;private double price;private int maxSpeed;        //构造器1public Car(String brand, String corp, double price) {super();this.brand = brand;this.corp = corp;this.price = price;}// 构造器2public Car(String brand, String corp, int maxSpeed) {super();this.brand = brand;this.corp = corp;this.maxSpeed = maxSpeed;}@Overridepublic 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>


0 0
原创粉丝点击