1. 程式人生 > >spring bean 配置

spring bean 配置

1.屬性注入

class:

package com.cmr.spring;
public class HelloWorld {
private String name;
public void setName(String name) {
System.out.println("設定name");
this.name = name;
}
public void hello(){
System.out.println("Hello:"+name);
}
public HelloWorld(String name) {
super();
this.name = name;
}
public HelloWorld() {

super();
}
}

bean配置:

<bean id="car" class="com.cmr.spring.Car">
<constructor-arg value="bmw" index="0"></constructor-arg>
<constructor-arg value="shanghai" index="1"></constructor-arg>
<constructor-arg value="300000" index="2"></constructor-arg>
</bean>

2.構造器注入:

package com.cmr.spring;


public class Car {

private String brand;

private String crop;

private double price;

private int maxSpeed;


public Car(String brand, String crop, double price) {
super();
this.brand = brand;
this.crop = crop;
this.price = price;
}


public Car(String brand, String crop, int maxSpeed) {
super();

this.brand = brand;
this.crop = crop;
this.maxSpeed = maxSpeed;
}




@Override
public String toString() {
return "Car [brand=" + brand + ", crop=" + crop + ", price=" + price
+ ", maxSpeed=" + maxSpeed + "]";
}


}

bean配置:

<bean id="car" class="com.cmr.spring.Car">
<constructor-arg value="bmw" index="0"></constructor-arg>
<constructor-arg value="shanghai" index="1"></constructor-arg>
<constructor-arg value="300000" index="2"></constructor-arg>
</bean>
 <bean id="car2" class="com.cmr.spring.Car">
<constructor-arg value="bmw" type="java.lang.String"></constructor-arg>
<constructor-arg value="shanghai" type="java.lang.String"></constructor-arg>
<constructor-arg value="300" type="int"></constructor-arg>
</bean> 

3.引用其他bean

package com.cmr.spring;


public class Person {
private String name;

private String tel;

private Car car;


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public String getTel() {
return tel;
}


public void setTel(String tel) {
this.tel = tel;
}


public Car getCar() {
return car;
}


public void setCar(Car car) {
this.car = car;
}


@Override
public String toString() {
return "Person [name=" + name + ", tel=" + tel + ", car=" + car + "]";
}


}

bean:配置

<bean id="person" class="com.cmr.spring.Person">
<property name="name" value="cmr"></property>
<property name="tel" value="18888888888"></property>
<property name="car" ref="car"></property>
</bean>

內部bean配置

<bean id="person2" class="com.cmr.spring.Person">
<property name="name" value="cmr"></property>
<property name="tel" value="18888888888"></property>
<property name="car">
<bean class="com.cmr.spring.Car">
<property name="brand" value="audi"></property>
<property name="crop" value="guangzhou"></property>
<property name="price" value="3000000"></property>
<property name="maxSpeed" value="230"></property>
</bean>
</property>
</bean>

4.list集合

package com.cmr.spring;


import java.util.List;


public class NewPerson {
private String name;

private List<Car> car;


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public List<Car> getCar() {
return car;
}


public void setCar(List<Car> car) {
this.car = car;
}


public NewPerson() {
super();
}


@Override
public String toString() {
return "NewPerson [name=" + name + ", car=" + car + "]";
}



}


bean配置:

<bean id="newPerson" class="com.cmr.spring.NewPerson">
<property name="name" value="ly"></property>
<property name="car">
<list >
<ref bean="car"/>
<ref bean="car2"/>
</list>
</property>
</bean>

也可將list共享

<util:list id="carlist" value-type="java.util.List">
<ref bean="car"/>
<ref bean="car2"/>
</util:list>

5.map集合

package com.cmr.spring;


import java.util.Map;


public class FuturePerson {
private Map<String, Car> car;


public Map<String, Car> getCar() {
return car;
}


public void setCar(Map<String, Car> car) {
this.car = car;
}


@Override
public String toString() {
return "FuturePerson [car=" + car + "]";
}





}

bean配置:

<bean id="futurePerson" class="com.cmr.spring.FuturePerson">
<property name="car">
<map>
<entry key="aa">
<ref bean="car"/>
</entry>
<entry key="bb">
<ref bean="car2"/>
</entry>
</map>
</property>
</bean>

5.<props>配置

package com.cmr.spring;


import java.util.Properties;


public class DataSource {

private Properties property;


public Properties getProperty() {
return property;
}


public void setProperty(Properties property) {
this.property = property;
}


@Override
public String toString() {
return "DataSource [property=" + property + "]";
}

}

bean配置:

<bean id="dataSource" class="com.cmr.spring.DataSource">
<property name="property">
<props>
<prop key="jdbc">mysql/jdbc</prop>
<prop key="user">root</prop>
<prop key="pwd">root</prop>
</props>
</property>
</bean>