标签:
本文主要介绍了spring配置文件的配置:set方法,constructor构造函数注入各种类型的属性值,bean的继承,自动装配autowire以及通过spring提供的特殊bean进行了分散配置.
首先,配置文件beans.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" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-autowire="no"> <!-- <context:property-placeholder location="classpath:com/assembly/db.properties,classpath:com/assembly/db1.properties"/> --> <bean id="personService" class="com.assembly.PersonService" > <property name="name" > <value>siege</value> </property> <property name="bookList"> <list> <ref bean="book1"/> <ref bean="book1"/> <ref bean="book2"/> <ref bean="book2"/> </list> </property> <property name="bookSet"> <set> <ref bean="book1"/> <ref bean="book1"/> <ref bean="book2"/> <ref bean="book2"/> </set> </property> <property name="map"> <map> <entry key="11" value-ref="book1"/> <entry key="11" value-ref="book1"/> <entry key="22" value-ref="book2"/> <entry key="22" value-ref="book2"/> </map> </property> <property name="pro"> <props> <prop key="001" >hello</prop> <prop key="002">world</prop> </props> </property> </bean> <bean id="book1" class="com.assembly.Book"> <property name="title"> <value>CSS</value> </property> <property name="id"> <value>1</value> </property> </bean> <bean id="book2" class="com.assembly.Book"> <property name="title"> <value>JavaScript</value> </property> <property name="id"> <value>2</value> </property> </bean> <bean id="student" parent="personService" class="com.assembly.Student"> <property name="score"> <value>100</value> </property> </bean> <!-- 以下是通过构造函数进行属性注入 --> <bean id="worker" class="com.assembly.Worker"> <constructor-arg index="0" type="java.lang.String" value="cage"/> <constructor-arg index="1" type="int" value="24"/> </bean> <!--以下是关于自动装配的演示 --> <bean id="master" class="com.assembly.Master" autowire="byName"> <!--byType--> <!--constructor--> <!--autodetect 会采用byType或constructor装配方式--> <!--default default值即为本文件头部的default-autowire的值--> <property name="name" value="siege"/> </bean> <bean id="dog" class="com.assembly.Dog"> <property name="name" value="小黄"/> <property name="age" value="3"/> </bean> <!--以下是关于spring容器提供的特殊bean来完成分散配置 或者通过本文件头部context:property-placeholder进行配置(已注释部分)--> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>com/assembly/db.properties</value> <value>com/assembly/db1.properties</value> </list> </property> </bean> <bean id="dbutil" class="com.assembly.DBUtil"> <property name="driver" value="${driver}"/> <property name="name" value="${name}"/> <property name="pwd" value="${pwd}"/> <property name="url" value="${url}"/> </bean> <bean id="dbutil1" class="com.assembly.DBUtil"> <property name="driver" value="${user.driver}"/> <property name="name" value="${user.name}"/> <property name="pwd" value="${user.pwd}"/> <property name="url" value="${user.url}"/> </bean> </beans>Book.java
package com.assembly; public class Book { private String title; private int id; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public String toString() { return "bookId:"+this.id+",bookTitle:"+this.title; } }DBUtil.java
package com.assembly; public class DBUtil { private String driver; private String name; private String pwd; private String url; public String getDriver() { return driver; } public void setDriver(String driver) { this.driver = driver; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } }
package com.assembly; public class Dog { private String name; private int age; 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; } }
package com.assembly; public class Master { private String name; private Dog dog; public String getName() { return name; } public void setName(String name) { this.name = name; } public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } }
package com.assembly; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class PersonService{ private String name; private List<Book> bookList; private Set<Book> bookSet; private Map<String,Book> map; private Properties pro; public Properties getPro() { return pro; } public void setPro(Properties pro) { this.pro = pro; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Book> getBookList() { return bookList; } public void setBookList(List<Book> bookList) { this.bookList = bookList; } public Set<Book> getBookSet() { return bookSet; } public void setBookSet(Set<Book> bookSet) { this.bookSet = bookSet; } public Map<String, Book> getMap() { return map; } public void setMap(Map<String, Book> map) { this.map = map; } }
package com.assembly; public class Student extends PersonService { private int score; public int getScore() { return score; } public void setScore(int score) { this.score = score; } }Worker.java
package com.assembly; public class Worker { private String name; private int age; public String getName() { return name; } public Worker(String name, int age) { this.name = name; this.age = age; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Worker() { } }
driver=driver name=name pwd=pwd url=url
user.driver=driver1 user.name=name1 user.pwd=pwd1 user.url=url1
package com.assembly; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Properties; import java.util.Set; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("com/assembly/beans.xml"); PersonService p=(PersonService) ac.getBean("personService"); Student student=(Student) ac.getBean("student"); Worker worker= (Worker) ac.getBean("worker"); Master master=(Master) ac.getBean("master"); DBUtil dbutil=(DBUtil) ac.getBean("dbutil1"); String name=p.getName(); System.out.println(name); List<Book> bookList=p.getBookList(); Set<Book> bookSet=p.getBookSet(); Map<String,Book> map=p.getMap(); Properties pro=p.getPro(); System.out.println("********List******"); for(Book book:bookList){ System.out.println("bookId:"+book.getId()+" bookTitle:"+book.getTitle()); } System.out.println("********Set******"); for(Book book:bookSet){ System.out.println("bookId:"+book.getId()+" bookTitle:"+book.getTitle()); } System.out.println("********Map******"); for(Entry<String, Book> book:map.entrySet()){ System.out.println("bookId:"+book.getValue().getId()+" bookTitle:"+book.getValue().getTitle()); } System.out.println("********Properties******"); for(Entry<Object,Object> pp:pro.entrySet()){ System.out.println("key:"+pp.getKey()+" value:"+pp.getValue()); } System.out.println("********关于bean的继承*********"); System.out.println("score:"+student.getScore()+" name:"+student.getName()+"\r\n"+"list:"+student.getBookList()+"\r\n"+ "set:"+student.getBookSet()+"\r\n"+"map:"+student.getMap()+"\r\n"+"properties:"+student.getPro()); System.out.println("*********通过构造函数注入属性值********"); System.out.println("name:"+worker.getAge()+" age:"+worker.getName()); System.out.println("********关于自动装配autowire"); System.out.println("masterName:"+master.getName()+" dogName:"+master.getDog().getName()+" dogAge:"+master.getDog().getAge()); System.out.println("*******关于分散配置*********"); System.out.println("driver:"+dbutil.getDriver()+"\r\nname:"+dbutil.getName()+"\r\npwd:"+dbutil.getPwd()+"\r\nurl:"+dbutil.getUrl()); } }
siege ********List****** bookId:1 bookTitle:CSS bookId:1 bookTitle:CSS bookId:2 bookTitle:JavaScript bookId:2 bookTitle:JavaScript ********Set****** bookId:1 bookTitle:CSS bookId:2 bookTitle:JavaScript ********Map****** bookId:1 bookTitle:CSS bookId:2 bookTitle:JavaScript ********Properties****** key:002 value:world key:001 value:hello ********关于bean的继承********* score:100 name:siege list:[bookId:1,bookTitle:CSS, bookId:1,bookTitle:CSS, bookId:2,bookTitle:JavaScript, bookId:2,bookTitle:JavaScript] set:[bookId:1,bookTitle:CSS, bookId:2,bookTitle:JavaScript] map:{11=bookId:1,bookTitle:CSS, 22=bookId:2,bookTitle:JavaScript} properties:{002=world, 001=hello} *********通过构造函数注入属性值******** name:24 age:cage ********关于自动装配autowire masterName:siege dogName:小黄 dogAge:3 *******关于分散配置********* driver:driver1 name:name1 pwd:pwd1 url:url1
标签:
原文地址:http://blog.csdn.net/u010999240/article/details/43367991