-
對(duì)于資源文件中的值的引用,如何在配置類(lèi)中體現(xiàn)與配置文件相同功能。涉及的注解是@ImportResource和@Value
查看全部 -
關(guān)于@Bean的定義,以及與之前Bean生命周期對(duì)應(yīng)的初始化與銷(xiāo)毀部分的對(duì)應(yīng)應(yīng)用
查看全部 -
如何進(jìn)行自定義qualifier注解:用@Qualifier注解自定義的注解
查看全部 -
@Qualifier和@Resource的使用場(chǎng)景的區(qū)別,在實(shí)際情況中的使用需要斟酌的條件
查看全部 -
@Autowired在數(shù)組等集合類(lèi)中的使用
查看全部 -
自動(dòng)裝配 byname bytype byname 根據(jù)名稱(chēng) bytype 根據(jù)類(lèi)型查看全部
-
@TestJavaBased.java
?
@RunWith(BlockJUnit4ClassRunner.class)
public ? class TestJavaBased extends UnitTestBase{
????? public TestJavaBased(){
???????????? super("classpath*:spring-beanannotation.xml");
????? }
@Test
????? public void testG(){
???????????? StringStore ? store=super.getBean("stringStoreTest");
????? }
}
@Store.java
?
package ? com.imooc.annotation.javabased;
public interface Store<T> {
}
@StringStore.java
?
package ? com.imooc.annotation.javabased;
public class StringStore implements ? Store<String> {
}
@IntegerStore.java
?
package ? com.imooc.annotation.javabased;
public class IntegerStore implements ? Store<Integer> {
}
@StoreConfig.java
?
package ? com.imooc.annotation.javabased;
@Configuration
public class StoreConfig {
@Autowired
?????? private ? Store<String> s1;
?
?????? @Autowired
?????? private ? Store<Integer> s2;
??????
?????? @Bean
?????? public ? StringStore stringStore(){
????????????? return ? new StringStore();
?????? }
??????
?????? @Bean
?????? public ? IntegerStore integerStore(){
????????????? return ? new IntegerStore();
?????? }
??????
?????? @Bean(name="stringStoreTest")
?????? public ? Store stringStoreTest(){//相當(dāng)于xml:<bean ? id="stringStoreTest" class="com.imooc.annotation.javabased.Store" ? ></bean>
????????????? syso("s1:"+s1.getClass().getName());
????????????? syso("s2:"+s2.getClass().getName());
????????????? return ? new IntegerStore();
?????? }
}
@spring-beanannotation.xml
?
<context:component-scan ? base-package="com.imooc.annotation" ></context:component>
?CustomAutowireConfigurer(自定義qualifier注解類(lèi)型,不常用)
(1)CustomAutowireConfigurer是BeanFactoryPostProcessor的子類(lèi),通過(guò)它可以注冊(cè)自己的qualifier注解類(lèi)型(即使沒(méi)有使用Spring的@Qualifier 注解)
(2)該AutowireCandidateResolver決定自動(dòng)裝配的候選者:
a)每個(gè)bean定義autowired-candidate值
b)任何<bean/>中的default-autowire-candidates
c)@Qualifier注解及使用CustomAutowireConfigurer的自定義類(lèi)型
查看全部 -
(1)@Bean默認(rèn)是單例的,為了指定范圍,使用@Scope注解;
(2)bean的作用域?:singleton、prototype、request、session、global、session
(3)singleton、prototype(每次請(qǐng)求都會(huì)創(chuàng)建一個(gè)新的對(duì)象,為了區(qū)分,應(yīng)該查看對(duì)象的hashcode,而類(lèi)的hashcode是一樣的)
(4)@Scope可以采用proxyMode 來(lái)指定哪種代理方式
查看全部 -
加載資源文件以及讀取資源文件的兩種方式。
方式一:通過(guò)配置文件
(1)<context:property-placeholder location="" ></context:property> 引入加載資源文件;
(2)通過(guò)${key}方式獲取配置文件中的值;
示例:將classpath:/com/acme/jdbc.properties配置文件中jdbc的url,username,password的值分別獲取并裝配到DriverManagerDataSource類(lèi)中的url,username,password屬性中去。
方式二:通過(guò)注解方式
(1)將一個(gè)用于裝載資源文件的類(lèi)用@Configuration注解,并通過(guò)注解@ImportResource引入配置文件;
(2)獲取配置文件中的值是通過(guò)@Value("${key}")的方式獲取值,@Value要標(biāo)注到屬性上,才能自動(dòng)將配置文件的值裝配到屬性上。
注意: 用@Value獲取配置文件值的時(shí)候要注意,如果配置文件中key為“username”,此時(shí)spring會(huì)讀取登錄當(dāng)前操作系統(tǒng)的用戶名,所以此種情況要注意,不要用username做key
查看全部 -
運(yùn)行結(jié)果biz
查看全部 -
name指定名稱(chēng)
查看全部 -
Advisors?
spring中<aop:advisor>配合transactional advice使用(大多數(shù)情況下)
查看全部 -
shcema-defined aspects 只支持單例模式
查看全部 -
Introductions簡(jiǎn)介聲明?
查看全部 -
利用@ImportResource、@Value加載資源文件:
查看全部
舉報(bào)