Spring注入方式(2)

3、引用其他bean

  Bean经常需要相互协作完成应用程序的功能,bean之间必须能够互相访问,就必须在bean配置之间指定对bean的引用,可以通过节点<ref>或者ref来为bean属性指定对bean的引用,也可以在属性或者构造器里包含bean的声明,这样bean称为内部bean。

bean中引用其他bean,其中Car为对象。

 <!-- 通过构造方法配置bean属性 -->
<bean id="car" class="hello.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="person" class="hello.Person">
<property name="name" value = "Tom"></property>
<property name="age" value = "24"></property>
<property name="car" ref="car"></property>
</bean>

内部bean

 <bean id="person" class="hello.Person">
<property name="name" value = "Tom"></property>
<property name="age" value = "24"></property>
<!--
<property name="car" ref="car"></property>--> <!-- 内部bean -->
<property name="car">
<!-- 内部bean不能被外部bean使用 -->
<bean id="car3" class="hello.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="Changan"></constructor-arg>
<constructor-arg value="200000" type="double"></constructor-arg> </bean>
</property>
</bean>

4、集合属性

在Spring中可以通过<list>、<set>或者<map>来配置集合属性。

通过list配置集合属性

Person.java

 package com.spring;

 import java.util.List;

 public class Person {
private String name;
private int age;
private List<Car> cars;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]";
} }

Car.java

 package com.spring;

 public class Car {
private String brand;
private double price;
private int maxSpeed;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
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;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + ", maxSpeed=" + maxSpeed + "]";
} }

beans-collection.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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"> <bean id="car1" class="com.spring.Car">
<property name="brand" value="audi"></property>
<property name="price" value="400000"></property>
<property name="maxSpeed" value="240"></property>
</bean> <bean id="car2" class="com.spring.Car">
<property name="brand" value="baoma"></property>
<property name="price" value="700000"></property>
<property name="maxSpeed" value="270"></property>
</bean> <bean id="person" class="com.spring.Person">
<property name="name" value="Jerry"></property>
<property name="age" value="41"></property>
<property name="cars">
<list>
<ref bean="car1" />
<ref bean="car2" />
</list>
</property>
</bean>
</beans>

结果:

Spring注入方式(2)

通过map配置属性

Person.java

 package com.spring;

 import java.util.Map;

 public class Person {
private String name;
private int age;
private Map<String, Car> carMap;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Map<String, Car> getCarMap() {
return carMap;
}
public void setCarMap(Map<String, Car> carMap) {
this.carMap = carMap;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", carMap=" + carMap + "]";
}
}

beans-collection

 <?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"> <bean id="car1" class="com.spring.Car">
<property name="brand" value="audi"></property>
<property name="price" value="400000"></property>
<property name="maxSpeed" value="240"></property>
</bean> <bean id="car2" class="com.spring.Car">
<property name="brand" value="baoma"></property>
<property name="price" value="700000"></property>
<property name="maxSpeed" value="270"></property>
</bean> <bean id="person" class="com.spring.Person">
<property name="name" value="Jerry"></property>
<property name="age" value="41"></property>
<property name="carMap">
<map>
<entry key="1" value-ref="car1"></entry>
<entry key="2" value-ref="car2"></entry>
</map>
</property>
</bean>
</beans>

结果:

Spring注入方式(2)

配置Set属性,和配置list一样。

5、p名字空间注入,也需要setter方法。

Student.java

 package com.spring;

 public class Student {
private String name;
private String number; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
} @Override
public String toString() {
return "Student [name=" + name + ", number=" + number + "]";
} }

Main.java

 package com.spring;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("beans-p.xml");
Student student = (Student) ac.getBean("student");
System.out.println(student);
}
}

beans-p.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="student" class="com.spring.Student" p:name="Merry" p:number="1120141314"></bean> </beans>

执行结果

Spring注入方式(2)

6、property注入

Student.java

 package com.spring;

 import java.util.Properties;

 public class Student {
private String name;
private String number;
private Properties properties; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "Student [name=" + name + ", number=" + number + ", properties=" + properties + "]";
} }

beans-p.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:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<bean id="student" class="com.spring.Student">
<property name="name" value="Merry"></property>
<property name="number" value="1120141213"></property>
<property name="properties" >
<props>
<prop key="user">root</prop>
<prop key="psw">1234</prop>
<prop key="jdbcUrl">jdbc:mysql:///test</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
</props>
</property> </bean>
</beans>

执行结果

Spring注入方式(2)

7、c命名空间注入

上一篇:Oracle client客户端简易安装网上文档一


下一篇:Django1