8Spring初步----青软S2SH(笔记)
例子:
bean.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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"> <bean id="book" class="com.qst.chapter08.Book"> <property name="name" value="SSH开发" /> <property name="author" value="qst" /> <property name="publishHouse" value="某出版社" /> <property name="price" value="70.25" /> </bean> </beans>
测试代码
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.core.io.ClassPathResource; public class BeanFactoryExample { public static void main(String[] args) { // 根据配置文件创建ClassPathResource对象 ClassPathResource resource = new ClassPathResource("com/qst/chapter08/bean.xml"); // 创建BeanFactory对象 DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory); reader.loadBeanDefinitions(resource); // 从BeanFactory对象中,根据id获取具体对象 Book book = (Book) factory.getBean("book"); System.out.println(book.getName()); System.out.println(book.getAuthor()); System.out.println(book.getPublishHouse()); System.out.println(book.getPrice()); } }
ApplicationContext的例子
public class ApplicationContextExample { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("com/qst/chapter08/bean.xml"); Book book = (Book) context.getBean("book"); System.out.println(book.getName()); System.out.println(book.getAuthor()); System.out.println(book.getPublishHouse()); System.out.println(book.getPrice()); context.close(); } }
例如
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"> <!--配置bean--> </beans>
测试类不变,需要改变bean和配置文件,测试类里的调用方法不变:
bean中加一个构造方法,配置文件中加入这个构造方法的相关配置
public class Order { private int id; private String orderNo; // 订单编号 private double amount; // 订单金额 private Object[] myArray; private List<?> myList; private Set<?> mySet; private Map<?, ?> myMap; private Properties myProp; public Order(int id, String orderNo) { this.id = id; this.orderNo = orderNo; }
//其他省略
}
<bean id="order" class="com.qst.chapter08.config.Order"> <constructor-arg name="id" value="100" /> <constructor-arg name="orderNo"> <value>ABCDEF</value> </constructor-arg> <property name="amount"> <value>5678.123</value> </property> <!--其他省略--> </bean>
示例如下:
public class Order { private int id; private String orderNo; // 订单编号 private double amount; // 订单金额 private Object[] myArray; private List<?> myList; private Set<?> mySet; private Map<?, ?> myMap; private Properties myProp; public Order(int id, String orderNo) { this.id = id; this.orderNo = orderNo; } public int getId() { return id; } public String getOrderNo() { return orderNo; } public void setOrderNo(String orderNo) { this.orderNo = orderNo; } public double getAmount() { return amount; } public void setAmount(double amount) { this.amount = amount; } public Object[] getMyArray() { return myArray; } public void setMyArray(Object[] myArray) { this.myArray = myArray; } public List<?> getMyList() { return myList; } public void setMyList(List<?> myList) { this.myList = myList; } public Set<?> getMySet() { return mySet; } public void setMySet(Set<?> mySet) { this.mySet = mySet; } public Map<?, ?> getMyMap() { return myMap; } public void setMyMap(Map<?, ?> myMap) { this.myMap = myMap; } public Properties getMyProp() { return myProp; } public void setMyProp(Properties myProp) { this.myProp = myProp; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"> <bean id="order" class="com.qst.chapter08.config.Order"> <constructor-arg name="id" value="100" /> <constructor-arg name="orderNo"> <value>ABCDEF</value> </constructor-arg> <property name="amount"> <value>5678.123</value> </property> <property name="myArray"> <array> <value>1234</value> <ref bean="orderItem1" /> </array> </property> <property name="myList"> <list> <value>1234</value> <ref bean="orderItem1" /> </list> </property> <property name="mySet"> <set> <value>1234</value> <ref bean="orderItem1" /> </set> </property> <property name="myMap"> <map> <entry key="1" value="1234" /> <entry key="2" value-ref="orderItem1" /> </map> </property> <property name="myProp"> <props> <prop key="aaa">AAA</prop> <prop key="bbb">BBBBB</prop> </props> </property> </bean> <bean id="orderItem1" class="com.qst.chapter08.config.OrderItem" > <property name="id" value="1" /> <property name="product" value="电视机" /> <property name="price" value="3000" /> <property name="count" value="4" /> </bean> </beans>
这里的继承指的是配置信息的重用,跟面向对象里的继承毫无关系.
前置依赖是指,有时要求Bean A的初始化必须在Bean B的初始化之后,而B并不是A的属性.这种情况下,无法通过ref来保证B先初始化,所以Spring提供了depends-on.
这里重点说一下by Type,by Type方式只根据类型与否匹配来决定是否注入依赖关系,加入A实例有setB(B b)方法,而Spring配置文件中恰好有一个类型B的Bean实例,则Spring会注入此实例,如果容器总没有一个类型为B的实例或多于一个的B实例,则都会抛出异常.