日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring:《spring实战》读后感二

發布時間:2025/6/15 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring:《spring实战》读后感二 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 裝配(wiring)

? ? 創建應用對象之間協作關系的行為稱為裝配(wiring), 這也是依賴注入(DI)的本質.

2. spring裝配機制:? ? ?

? ? ? 1). 在XML中進行顯式配置。


? ? ? 2). 在Java中進行顯式配置。

public interface CompactDisc {void play(); }import org.springframework.stereotype.Component;/** @Component注解。 這個簡單的注解表明該類會作為組件類, * 并告知Spring要為這個類創建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應用上下文中如何創建bean的細節。*/ @Configuration public class CDPlayerConfig {/*@Bean注解會告訴Spring這個方法將會返回一個對象,該對象要注冊為Spring應用上下文中的bean。方法體中包含了最終產生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) // 自動創建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發現機制和自動裝配。

? ? ? ? ? spring從兩個角度來實現自動化裝配:? 1). 組建掃描(component scanning): spring會自動發現應用上下文中所創建的bean。

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 2). 自動裝配(autowiring):spring自動滿足bean之間的依賴。

public interface CompactDisc {void play(); }import org.springframework.stereotype.Component;/** @Component注解。 這個簡單的注解表明該類會作為組件類, * 并告知Spring要為這個類創建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將會掃描這個包以及這個包下的所有子包, 查找帶 有@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) // 自動創建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應用上下文中如何創建bean的細節。*/ @Configuration public class CDPlayerConfig {/*@Bean注解會告訴Spring這個方法將會返回一個對象,該對象要注冊為Spring應用上下文中的bean。方法體中包含了最終產生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) // 自動創建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, 就會創建這個bean, 否則的話, 這個bean會被忽略。

?

5. 處理自動裝配的歧義性

? ? ?發生歧義性的時候, Spring提供了多種可選方案來解決這樣的問題。 你可以將可選bean中的某一個設為首選(primary) 的
bean(使用@Primary注解), 或者使用限定符(qualifier) 來幫助Spring將可選的bean的范圍縮小到只有一個bean。

? ? ?Cake,Cookies都實現了Dessert接口,但是注入到Dessert類型時, 會將Cookies注入進去,避免了"org.springframework.beans.factory.NoUniqueBeanDefinitionException"

? ?

?

6. bean的作用域

?

?7. 運行時值注入

??如果希望避免硬編碼值, 而是想讓這些值在運行時再確定。如下,我們不希望把字符串寫死。

? 為了實現這些功能, Spring提供了兩種在運行時求值的方式:

? ? ? 1)。 屬性占位符(Property placeholder) 。?

? ? ? 占位符的形式為使用"${}"包裝的屬性名稱。

? ? ? ?為了使用占位符, 我們必須要配置一個PropertyPlaceholderConfigurer bean或PropertySourcesPlaceholderConfigurer bean。 從Spring3.1開始, 推薦使用PropertySourcesPlaceholderConfigurer, 因為它能夠基于Spring Environment及其屬性源來解析占位符。


? ? ? 2)。Spring表達式語言(SpEL ?Spring Expression Language)

? ? ?SpEL擁有很多特性, 包括:
? ? ? ? ? ? ?①使用bean的ID來引用bean;
? ? ? ? ? ? ?②調用方法和訪問對象的屬性;
? ? ? ? ? ? ?③對值進行算術、 關系和邏輯運算;
? ? ? ? ? ? ?④正則表達式匹配;
? ? ? ? ? ? ?⑤集合操作。

? ? SpEL表達式要放到“#{ ... }”之中

? ? ? ?SpEL可以表示字面值: 浮點數(#{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实战》读后感二的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。