3. 注解
????3.1 @Component(把當(dāng)前類對(duì)象存入spring容器中) ??????
? ? 3.2?@Controller????@Service????@Repository??
? ??3.3 @Autowired + @Qualifier(在spring容器中尋找需要注入的類)
? ??3.4 @Resource(name = "XXX")
? ??3.5 @PreDestroy????@PostConstruct????@Scope
4. 基于注解的IOC實(shí)例
5. 對(duì)基于注解的IOC實(shí)例進(jìn)行優(yōu)化
? ??5.1?@Configuration(指定當(dāng)前類是一個(gè)配置類)
? ??5.2?@ComponentScan(創(chuàng)建容器時(shí)要掃描的包)
? ? 5.3?@Bean(把當(dāng)前方法的返回值存入spring容器中)
? ??5.4?@Import(導(dǎo)入其他的配置類)
? ? 5.5 @PropertySource(指出properties文件的位置)
? ? 5.6 優(yōu)化
? ? 5.7 @value(將外部配置文件的值動(dòng)態(tài)注入到Bean)
? ??5.8?@RunWith(SpringJUnit4ClassRunner.class)?+?@ContextConfiguration?
3. 注解
? ? 3.1?@Component(把當(dāng)前類對(duì)象存入spring容器中)
????????屬性:默認(rèn)值是當(dāng)前類名,且首字母改小寫。
????????作用:用于把當(dāng)前類對(duì)象存入spring容器中。
? ? ? ? 但是使用時(shí)需要告知spring在創(chuàng)建容器時(shí)要掃描的包
? ??????<context:component-scan base-package="com.liuming"></context:component-scan>
? ? 3.2?@Controller????@Service????@Repository
????????@Controller:一般用在表現(xiàn)層
????????@Service:一般用在業(yè)務(wù)層
????????@Repository:一般用在持久層
? ???????作用和屬性等同于@Component? ? ? ?
? ? 3.3 @Autowired + @Qualifier(在spring容器中尋找需要注入的類)
????????@Autowired:自動(dòng)按照類型注入(容器為map結(jié)構(gòu),注入時(shí)在map結(jié)構(gòu)中尋找相同的類型。但一個(gè)接口如果有兩個(gè)實(shí)現(xiàn)類時(shí),只使用@Autowired會(huì)報(bào)錯(cuò))
? ??????@Qualifier:指明要注入類的id(在給類成員注入時(shí),需要配合@Autowired一起使用,但給方法參數(shù)注入時(shí),可以獨(dú)立使用)
? ? 3.4 @Resource(name = "XXX")
? ? ? ? @Resource:直接按照bean的id注入,不是spring的注解,而是JDK的注解
? ? 3.5 @PreDestroy????@PostConstruct????@Scope
????????@PostConstruct在bean創(chuàng)建完成并且屬性賦值完成后來執(zhí)行初始化方法。
????????@PreDestroy注解在容器銷毀bean之前通知進(jìn)行清理工作。
? ??????@Scope用于指定bean的作用范圍
4. 基于注解的IOC實(shí)例
? ? ? ? 1. 使用注解將service類存到spring容器中。在容器中尋找需要注入的參數(shù) accountDao
????????2. 將accountDao存入spring容器中,同樣runner使用注解自動(dòng)注入。
? ? ? ? 3. 注入數(shù)據(jù)源。
????????4. 測(cè)試類進(jìn)行測(cè)試。
5. 對(duì)基于注解的IOC實(shí)例進(jìn)行優(yōu)化
????5.1?@Configuration
? ? ? ? 作用:指定當(dāng)前類是一個(gè)配置類
? ??????當(dāng)配置類作為AnnotationConfigApplicationContext對(duì)象創(chuàng)建的參數(shù)時(shí),該注解可以省略。
? ? ? ? 關(guān)于xml與注解的選擇:自己寫的類建議使用注解,jar包中的類建議使用xml
? ? 5.2?@ComponentScan
????????作用:用于通過注解指定spring在創(chuàng)建容器時(shí)要掃描的包
? ? ? ? 等價(jià)于<context:component-scan base-package="com.liuming"></context:component-scan>
????5.3 @Bean
? ? ? ? 作用:用于把當(dāng)前方法的返回值作為bean對(duì)象存入spring的ioc容器中
? ? ? ? 屬性:name 用于指定bean的id,默認(rèn)值為當(dāng)前方法的名稱
? ? 5.4?@Import
? ??????作用:用于導(dǎo)入其他的配置類
? ??????當(dāng)我們使用Import的注解之后,有Import注解的類就父配置類,而導(dǎo)入的都是子配置類
? ? 5.5 @PropertySource
? ??????作用:用于指定properties文件的位置
? ??????關(guān)鍵字:classpath,表示類路徑下
????5.6 優(yōu)化
? ? ? ? 1. 新增config.SpringConfiguration類取代bean.xml文件
? ? ? ? 2. 新增JdbcConfig進(jìn)一步解耦,這里注意@Qualifier的新用法
? ? @Value
????????作用:通過注解將常量、配置文件中的值、其他bean的屬性值注入到變量中,作為變量的初始值
????5.8?@RunWith(SpringJUnit4ClassRunner.class) +?@ContextConfiguration
? ? ? ? 應(yīng)用程序的入口:main()
? ? ? ? junit單元測(cè)試中,沒有main()也能執(zhí)行,這是因?yàn)閖unit集成了一個(gè)main(),該方法會(huì)判斷當(dāng)前測(cè)試類中哪些方法有@Test注解
? ? ? ? junit不會(huì)管我們是否采用了spring框架,在執(zhí)行測(cè)試方法時(shí)候,junit根本不知道我們是不是使用了spring,所以也就不會(huì)為我們讀取配置文件/配置類創(chuàng)建spring核心容器
? ? ? ? 由上可知:當(dāng)測(cè)試方法執(zhí)行時(shí),沒有Ioc容器,就算寫了Autowired注解,也無法注入
????????@RunWith:用于指定junit運(yùn)行環(huán)境,是junit提供給其他框架測(cè)試環(huán)境接口擴(kuò)展。
????????為了便于使用spring的依賴注入,spring提供了SpringJUnit4ClassRunner作為Junit測(cè)試環(huán)境
? ??????@ContextConfiguration Spring整合JUnit4測(cè)試時(shí),使用注解引入多個(gè)配置文件