1. 程式人生 > >Spring中屬性配置(配置Properties屬性值(通過props,props子節點 )為屬性賦值)

Spring中屬性配置(配置Properties屬性值(通過props,props子節點 )為屬性賦值)

1:com.beans.Car

package com.beans;


public class Car {
   private String brand;
   private String corp;
   private int price;
   private int maxSpeed;
   
public Car() {
super();
// TODO Auto-generated constructor stub
}
public Car(String brand, String corp, int price, int maxSpeed) {
super();
this.brand = brand;
this.corp = corp;
this.price = price;
this.maxSpeed = maxSpeed;
}
public String toString() {
return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price
+ ", maxSpeed=" + maxSpeed + "]";
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getCorp() {
return corp;
}
public void setCorp(String corp) {
this.corp = corp;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getMaxSpeed() {
return maxSpeed;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
   


}

2:com.beans.Person

package com.beans;


public class Person {
  private String name;
  private int age;
  private Car car;
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 Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}


public Person() {
super();
}
public Person(String name, int age, Car car) {
super();
this.name = name;
this.age = age;
this.car = car;
}
public String toString() {
return "Person [name=" + name + ", age=" + age + ", car=" + car + "]";
}


  
}

  3:com.beans.Main

package com.beans;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Main {


public static void main(String[] args) {
//建立HelloWorld一個物件
HelloWorld helloWorld=new HelloWorld();
//為屬性賦值
helloWorld.setName("Spring入門");
//呼叫方法
helloWorld.hello();
  //1:建立Spring的IOC容器物件
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
 //2:從IOC容器中獲取Bean例項
HelloWorld helloWorld1=(HelloWorld)ctx.getBean("helloWorld");
 //3:呼叫hello方法
helloWorld1.hello();
Car car=(Car)ctx.getBean("car");
System.out.println(car);
Person person=(Person)ctx.getBean("person");
System.out.println(person);
Person person1=(Person)ctx.getBean("person1");
System.out.println(person1);
Person person2=(Person)ctx.getBean("person2");
System.out.println(person2);

}


}

  4:com.beans.collection.Person

package com.beans.collection;


import java.util.List;


import com.beans.Car;


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 Person() {
super();
}
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
public String toString() {
return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]";
}

}

 5:

package com.beans.collection.NewPerson;


import java.util.Map;


import com.beans.Car;


public class NewPerson {
private String name;
 private int age;
 private Map<String,Car> cars;
@Override
public String toString() {
return "NewCar [name=" + name + ", age=" + age + ", cars=" + 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 Map<String, Car> getCars() {
return cars;
}
public void setCars(Map<String, Car> cars) {
this.cars = cars;
}
 
}

 6:com.beans.collection.DataSource

package com.beans.collection;


import java.util.Properties;


public class DataSource {
   private Properties properties;//properties是HashTable的一個子類


public Properties getProperties() {
return properties;
}


public void setProperties(Properties properties) {
this.properties = properties;
}


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

  7: com.beans.collection.Main

package com.beans.collection;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Main {


public static void main(String[] args) {
  //1:建立Spring的IOC容器物件
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
 //2:從IOC容器中獲取Bean例項
Person person3=(Person)ctx.getBean("person3");
System.out.println(person3);
NewPerson person4=(NewPerson)ctx.getBean("person4");
System.out.println(person4);
DataSource dataSource=(DataSource)ctx.getBean("dataSource");
System.out.println(dataSource);
System.out.println(dataSource.getProperties());
}


}

8: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"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
                    
  <!-- 配置bean class:bean的全類名,通過反射的方式在IOC容器例項化物件,中id是唯一的-->
  <!-- 通過setter方法配置屬性-->
   <bean id="helloWorld" class="com.beans.HelloWorld">
   <property name="name" value="Json"/>
   </bean>
   <!-- 通過構造方法-配置屬性-->
    <bean id="car" class="com.beans.Car">
    <constructor-arg value="賓士" index="0"></constructor-arg>
    <constructor-arg value="上海" index="1"></constructor-arg>
    <constructor-arg value="300000" index="2"></constructor-arg>
    <constructor-arg value="180" index="3"></constructor-arg>
    </bean>
    <bean id="car1" class="com.beans.Car">
      <property name="brand" value="法拉利"/>
   <property name="corp" value="廣東"/>
   <property name="price" value="900000"/>
   <property name="maxSpeed" value="199"/>
    </bean>
  <bean id="person" class="com.beans.Person">
   <property name="name" value="Tom"/>
   <property name="age" value="24"/>
   <!--引用外部bean  -->
   <property name="car" ref="car"></property>
   </bean>
    <bean id="person1" class="com.beans.Person">
   <property name="name" value="Smith"/>
   <property name="age" value="25"/>
   <!--引用內部bean  -->
   <property name="car">
      <bean class="com.beans.Car">
         <constructor-arg value="Ford"></constructor-arg>
         <constructor-arg value="Changan"></constructor-arg>
          <constructor-arg value="200000"></constructor-arg>
         <constructor-arg value="100"></constructor-arg>
      </bean>
       </property>
       </bean>
       <bean id="person2" class="com.beans.Person">
          <constructor-arg value="Bob"></constructor-arg>
          <constructor-arg value="21"></constructor-arg>
          <constructor-arg ref="car"></constructor-arg>
      </bean>
      <!-- 測試集合屬性 1(list) -->
    <bean id="person3" class="com.beans.collection.Person">
          <property name="name" value="Alice"/>
          <property name="age" value="19"/>
          <property name="cars">
            <list>
                <ref bean="car"></ref>
                <ref bean="car1"></ref>
            </list>
          </property>
      </bean>
      <!-- 測試集合屬性 1(Map) -->
    <bean id="person4" class="com.beans.collection.NewPerson">
          <property name="name" value="Alice"/>
          <property name="age" value="19"/>
          <property name="cars">
            <map>
                <entry key="AA" value-ref="car"></entry>
                <entry key="BB" value-ref="car1"></entry>
            </map>
          </property>
      </bean>
      <!-- 配置Properties屬性值(通過props,props子節點 )為屬性賦值-->
      <bean id="dataSource" class="com.beans.collection.DataSource">
        <property name="properties">
           <props>
           <prop key="username">root</prop>
           <prop key="password">1234</prop>
           <prop key="url">jdbc:mysql://localhost:3306/mydatabase</prop>
           <prop  key="driver">com.mysql.jdbc.Driver</prop> </props>
          </property>
      </bean>
   <!-- 配置bean的方法(3種)反射,工廠設計模式-->
</beans>