Spring中属性配置(配置Properties属性值(通过props,props子节点 )为属性赋值)
2017-03-04 14:48
323 查看
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>
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>
相关文章推荐
- Speing中配置Properties属性值 ,使用props和prop子节点来为Properties属性赋值
- ApplicationContextAware获取Spring上下文,通过基类获取properties中的配置属性
- 获取spring中配置的properties属性名和属性值
- 根据Spring上下文对象xxxContext获得*.properties属性文件中的配置属性值
- Spring代码实例系列-09:通过Spring PropertyPlaceholderConfigurer将properties配置的属性注入到xml配置文件中
- Spring通过.properties文件引入属性配置
- 配置spring的配置list,map,set,props集合属性
- Spring 通过配置文件注入 properties文件
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- 如何通过样式来处理根据自身其他属性内容赋值其他属性值的方法研究
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- Java中如何获取spring中配置的properties属性文件内容
- Java中如何获取spring中配置的properties属性文件内容
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- 解决Spring配置文件无法读取properties属性问题
- Spring环境搭建之:通过PropertyPlaceholderConfigurer加载属性配置文件:
- Java中如何获取spring中配置的properties属性文件内容
- Spring+Hibernage通过jdbc.properties配置
- spring 注入java.util.Properties 属性两种xml中的配置练习