Spring入門第七課
阿新 • • 發佈:2017-05-20
actor strac 關系 並不是 null ont dao ble init
Bean之間的關系:繼承和依賴。
繼承Bean配置
Spring允許繼承bean的配置,被繼承的bean稱為父bean,繼承這個父bean的Bean稱為子Bean。
子Bean從父Bean中繼承配置,包括Bean的屬性配置。
子Bean也可以覆蓋從父Bean繼承過來的配置。
父Bean可以作為配置模板,也可以作為Bean實例,若只想把父Bean作為模板,可以設置<bean>的abstract屬性為true,這樣Spring將不會實例化這個bean。(抽象Bean,抽象Bean不能被實例化,只能被繼承)
並不是<bean>元素裏的所有屬性都會被繼承,比如:autowire,abstract等。
也可以忽略父Bean的class屬性,讓子Bean指定自己的類,而共享相同的屬性配置。但此時abstract必須為true。
<?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="address" class="logan.spring.study.autowire.Address" p:city="Beijing" p:street="WuDaoKou"></bean> <bean id="address2" class="logan.spring.study.autowire.Address" p:city="Beijing" p:street="DaZhongSi"></bean> </beans>
package logan.spring.study.relation;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import logan.spring.study.autowire.Address; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-relation.xml"); Address address = (Address) ctx.getBean("address"); System.out.println(address); address = (Address) ctx.getBean("address2"); System.out.println(address); } }
下面是輸出結果:
五月 20, 2017 4:03:58 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContex[email protected]: startup date [Sat May 20 16:03:58 CST 2017]; root of context hierarchy 五月 20, 2017 4:03:58 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [beans-relation.xml] Address [city=Beijing, street=WuDaoKou] Address [city=Beijing, street=DaZhongSi]
可以看到兩個address的內容大部分都是一樣的,所以,我們可以如下使用關系,讓第二個address繼承第一個address
如下配置
<?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="address" class="logan.spring.study.autowire.Address" p:city="Beijing1" p:street="WuDaoKou"></bean> <bean id="address2" p:street="DaZhongSi" parent="address"></bean> </beans>
下面是輸出結果
五月 20, 2017 4:08:07 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContex[email protected]: startup date [Sat May 20 16:08:07 CST 2017]; root of context hierarchy 五月 20, 2017 4:08:07 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [beans-relation.xml] Address [city=Beijing1, street=WuDaoKou] Address [city=Beijing1, street=DaZhongSi]
下面看Bean之間的依賴關系
Spring允許用戶通過depends-on屬性設定Bean前置依賴的Bean,前置依賴的Bean會在本Bean實例化之前創建好。
如果前置依賴於多個Bean,則可以通過都好,空格的方式配置Bean的名稱。
<?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="address" class="logan.spring.study.autowire.Address" p:city="Beijing1" p:street="WuDaoKou"></bean> <bean id="address2" p:street="DaZhongSi" parent="address"></bean> <bean id="person" class="logan.spring.study.autowire.Person" p:name="Tom" p:address-ref="address"></bean> </beans>
package logan.spring.study.relation; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import logan.spring.study.autowire.Address; import logan.spring.study.autowire.Person; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-relation.xml"); Address address = (Address) ctx.getBean("address"); System.out.println(address); address = (Address) ctx.getBean("address2"); System.out.println(address); Person person = (Person) ctx.getBean("person"); System.out.println(person); } }
下面是輸出結果
五月 20, 2017 4:23:24 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContex[email protected]: startup date [Sat May 20 16:23:24 CST 2017]; root of context hierarchy 五月 20, 2017 4:23:25 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [beans-relation.xml] Address [city=Beijing1, street=WuDaoKou] Address [city=Beijing1, street=DaZhongSi] Person [name=Tom, address=Address [city=Beijing1, street=WuDaoKou], car=null]
可以看到car是null,如果我必須要car不是null,也就是說person依賴於null,那麽如何做?
如下做法:
<?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="address" class="logan.spring.study.autowire.Address" p:city="Beijing1" p:street="WuDaoKou"></bean> <bean id="address2" p:street="DaZhongSi" parent="address"></bean> <bean id="person" class="logan.spring.study.autowire.Person" p:name="Tom" p:address-ref="address" depends-on="car"></bean> </beans>
輸出錯誤結果
五月 20, 2017 4:27:50 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContex[email protected]: startup date [Sat May 20 16:27:50 CST 2017]; root of context hierarchy 五月 20, 2017 4:27:50 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [beans-relation.xml] 五月 20, 2017 4:27:50 下午 org.springframework.context.support.ClassPathXmlApplicationContext refresh 警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘car‘ available Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘car‘ available at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687) at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1207) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:296) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) at logan.spring.study.relation.Main.main(Main.java:14)
如下配置就不會報錯
<?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="address" class="logan.spring.study.autowire.Address" p:city="Beijing1" p:street="WuDaoKou"></bean> <bean id="address2" p:street="DaZhongSi" parent="address"></bean> <bean id="car" class="logan.spring.study.autowire.Car" p:brand="Audi" p:price="300000"></bean> <bean id="person" class="logan.spring.study.autowire.Person" p:name="Tom" p:address-ref="address" depends-on="car"></bean> </beans>
Spring入門第七課