spring:《spring实战》读后感二
1. 裝配(wiring)
? ? 創(chuàng)建應用對象之間協(xié)作關系的行為稱為裝配(wiring), 這也是依賴注入(DI)的本質.
2. spring裝配機制:? ? ?
? ? ? 1). 在XML中進行顯式配置。
? ? ? 2). 在Java中進行顯式配置。
public interface CompactDisc {void play();
}import org.springframework.stereotype.Component;/** @Component注解。 這個簡單的注解表明該類會作為組件類, * 并告知Spring要為這個類創(chuàng)建bean。 */
public class SgtPeppers implements CompactDisc{@Overridepublic void play() {System.out.println("config the beatles sing sgt. Pepper lonely hearts club band2");}
}public class CDPlayer{private CompactDisc cd;public CDPlayer(CompactDisc cd){this.cd = cd;}public void play() {cd.play();}
}import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/** @Configuration注解表明這個類是一個配置類,
該類應該包含在Spring應用上下文中如何創(chuàng)建bean的細節(jié)。*/
@Configuration
public class CDPlayerConfig {/*@Bean注解會告訴Spring這個方法將會返回一個對象,該對象要注冊為Spring應用上下文中的bean。方法體中包含了最終產(chǎn)生bean實例的邏輯。*/@Beanpublic CompactDisc sgtPeppers(){return new SgtPeppers();}@Beanpublic CDPlayer cdPlayer(CompactDisc cd){return new CDPlayer(cd);}
}import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class) // 自動創(chuàng)建Spring的應用上下文
/* @ContextConfiguration會告訴它需要在CDPlayerConfig中加載配置。
因為CDPlayerConfig類中包含了@ComponentScan
*/
@ContextConfiguration(classes=CDPlayerConfig.class)
public class MyTest {@Autowiredprivate CompactDisc cd;@Autowiredprivate CDPlayer player;@Testpublic void test01(){cd.play(); player.play();}
}
? ? ? 3). 隱式的bean發(fā)現(xiàn)機制和自動裝配。
? ? ? ? ? spring從兩個角度來實現(xiàn)自動化裝配:? 1). 組建掃描(component scanning): spring會自動發(fā)現(xiàn)應用上下文中所創(chuàng)建的bean。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 2). 自動裝配(autowiring):spring自動滿足bean之間的依賴。
public interface CompactDisc {void play(); }import org.springframework.stereotype.Component;/** @Component注解。 這個簡單的注解表明該類會作為組件類, * 并告知Spring要為這個類創(chuàng)建bean。 */ @Component public class SgtPeppers implements CompactDisc{@Overridepublic void play() {System.out.println("the beatles sing sgt. Pepper lonely hearts club band");} }import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;/** 組件掃描默認是不啟用的* @ComponentScan注解啟用了組件掃描 * @ComponentScan默認會掃描與配置類相同的包。Spring將會掃描這個包以及這個包下的所有子包, 查找?guī)?有@Component注解的類。*/ @Configuration @ComponentScan public class CDPlayerConfig {}import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class) // 自動創(chuàng)建Spring的應用上下文 /* @ContextConfiguration會告訴它需要在CDPlayerConfig中加載配置。 因為CDPlayerConfig類中包含了@ComponentScan */ @ContextConfiguration(classes=CDPlayerConfig.class) public class MyTest {@AutowiredCompactDisc cd;@Testpublic void test01(){cd.play(); } }?當然如果你喜歡使用xml啟動組件掃描,那么如下xml配置可以代替@ComponentScan
?3. 在JavaConfig中引用xml配置
? ?
public interface CompactDisc {void play(); }import java.util.List;public class BlankDisc implements CompactDisc{private String title;private String artist;private List<String> tracks;public BlankDisc(String title, String artist, List<String> tracks) {super();this.title = title;this.artist = artist;this.tracks = tracks;}@Overridepublic void play() {System.out.println("xml,javaconfig混合使用");System.out.println("Playing "+title+" by"+artist);for(String track : tracks){System.out.println("-Track: "+track);}} }public class CDPlayer{private CompactDisc cd;public CDPlayer(CompactDisc cd){this.cd = cd;}public void play() {cd.play();} }import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;/** @Configuration注解表明這個類是一個配置類, 該類應該包含在Spring應用上下文中如何創(chuàng)建bean的細節(jié)。*/ @Configuration public class CDPlayerConfig {/*@Bean注解會告訴Spring這個方法將會返回一個對象,該對象要注冊為Spring應用上下文中的bean。方法體中包含了最終產(chǎn)生bean實例的邏輯。*/@Beanpublic CDPlayer cdPlayer(CompactDisc cd){return new CDPlayer(cd);} }import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.ImportResource;@Configuration @Import(CDPlayerConfig.class) @ImportResource("classpath:applicationContext.xml") public class SoundSystemConfig {}import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class) // 自動創(chuàng)建Spring的應用上下文 /* @ContextConfiguration會告訴它需要在CDPlayerConfig中加載配置。 因為CDPlayerConfig類中包含了@ComponentScan */ @ContextConfiguration(classes=SoundSystemConfig.class) public class MyTest {//@Autowired//private CompactDisc cd;@Autowiredprivate CDPlayer player;@Testpublic void test01(){player.play();} }?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.xsd"><!-- bean definitions here --><bean id="blankDisc" class="com.ag.test5.BlankDisc"><constructor-arg value="sgt. Pepper"/><constructor-arg><value>the beatles</value></constructor-arg><constructor-arg><list><value>getting better</value><value>fixing a hole</value></list></constructor-arg></bean> </beans>4. 條件化的bean
? ??Spring 4引入了一個新的@Conditional注解, 它可以用到帶有@Bean注解的方法上。 如果給定的條件計算結果為true, 就會創(chuàng)建這個bean, 否則的話, 這個bean會被忽略。
?
5. 處理自動裝配的歧義性
? ? ?發(fā)生歧義性的時候, Spring提供了多種可選方案來解決這樣的問題。 你可以將可選bean中的某一個設為首選(primary) 的
bean(使用@Primary注解), 或者使用限定符(qualifier) 來幫助Spring將可選的bean的范圍縮小到只有一個bean。
? ? ?Cake,Cookies都實現(xiàn)了Dessert接口,但是注入到Dessert類型時, 會將Cookies注入進去,避免了"org.springframework.beans.factory.NoUniqueBeanDefinitionException"
? ?
?
6. bean的作用域
?
?7. 運行時值注入
??如果希望避免硬編碼值, 而是想讓這些值在運行時再確定。如下,我們不希望把字符串寫死。
? 為了實現(xiàn)這些功能, Spring提供了兩種在運行時求值的方式:
? ? ? 1)。 屬性占位符(Property placeholder) 。?
? ? ? 占位符的形式為使用"${}"包裝的屬性名稱。
? ? ? ?為了使用占位符, 我們必須要配置一個PropertyPlaceholderConfigurer bean或PropertySourcesPlaceholderConfigurer bean。 從Spring3.1開始, 推薦使用PropertySourcesPlaceholderConfigurer, 因為它能夠基于Spring Environment及其屬性源來解析占位符。
? ? ? 2)。Spring表達式語言(SpEL ?Spring Expression Language)
? ? ?SpEL擁有很多特性, 包括:
? ? ? ? ? ? ?①使用bean的ID來引用bean;
? ? ? ? ? ? ?②調(diào)用方法和訪問對象的屬性;
? ? ? ? ? ? ?③對值進行算術、 關系和邏輯運算;
? ? ? ? ? ? ?④正則表達式匹配;
? ? ? ? ? ? ?⑤集合操作。
? ? SpEL表達式要放到“#{ ... }”之中
? ? ? ?SpEL可以表示字面值: 浮點數(shù)(#{3.14159}), String(#{'hello'})值以及Boolean(#{true})值。
? ? ? ?SpEL還可以引用bean(#{sgtPeppers})、 屬性(#{sgtPeppers.artist})和方法(#{artistSelector
.selectArtist()})
? ? ? ?SpEL還可以使用T()在表達式中使用類型。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 用來操作表達式值的SpEL運算符
| 運算符類型 | 運 算 符 |
| 算術運算 | +、 -、 * 、 /、 %、 ^ |
| 比較運算 | < 、 > 、 == 、 <= 、 >= 、 lt 、 gt 、 eq 、 le 、 ge |
| 邏輯運算 | and 、 or 、 not 、 │ |
| 條件運算 | ?: (ternary) 、 ?: (Elvis) |
| 正則表達式 | matches |
? ? ? 例如: #{2 * T(java.lang.Math).PI ?* ?circle.redius} ? ?// 使用了乘法運算符(*)
? ? ?盡量讓表達式簡潔
?
? ? ??
?
總結
以上是生活随笔為你收集整理的spring:《spring实战》读后感二的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring:《spring实战》读后感
- 下一篇: 设计模式:选择排序(select sor