基于注解整合時(shí) 先把src--main--resources--bean.xml中的約束改成下面的,導(dǎo)入約束時(shí)需要多導(dǎo)入一個(gè) context名稱空間下的約束。 由于我們使用了注解配置饱溢,此時(shí)不能在繼承 JdbcDaoSupport狰住,需要自己配置一個(gè) JdbcTemplate 。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 告知 spring 創(chuàng)建容器時(shí)要掃描的包 配置所有需要標(biāo)簽而不是在beans的約束中民假,
而是一個(gè)名為context名稱空間的約束中 這樣它才能掃描到目錄下面的每個(gè)文件的注解 -->
<context:component-scan base-package="com.itheima"></context:component-scan>
<!-- 配置 dbAssit -->
<bean id="dbAssit" class="com.itheima.dbassit.DBAssit">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置數(shù)據(jù)源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///spring_day02"></property>
<property name="user" value="root"></property>
<property name="password" value="1234"></property>
</bean>
</beans>
以往的XML配置:
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
scope="" init-method="" destroy-method="">
<property name="" value="" 或者ref=""><property>
</bean>
用于創(chuàng)建對(duì)象:相當(dāng)于在XML中編寫(xiě)了一個(gè)<bean>
標(biāo)簽實(shí)現(xiàn)的功能一樣意鲸。
@Component
:把當(dāng)前類對(duì)象存入到spring容器中 讓spring來(lái)管理, 相當(dāng)于XML中配置一個(gè)bean算色。
為了更加清晰 于是Spring提供了以下3個(gè)注解 效果與Component是一樣的
@Controller
:一般用于表現(xiàn)層的注解
@Service
:一般用在業(yè)務(wù)層的注解
@Repository
:一般用于在持久層的注解
注意:
如果注解里有一個(gè)value屬性抬伺,那么這個(gè)屬性是可以不寫(xiě)的,如果有兩個(gè)屬性灾梦,那么value就得寫(xiě)峡钓,例如: @Component(value="accountService")
等同于 @Component("accountService")
value:用于指定bean的ID,當(dāng)我們不寫(xiě)時(shí)斥废,它的默認(rèn)值是當(dāng)前類名且首字母改小寫(xiě)椒楣。
用于注入數(shù)據(jù)的:相當(dāng)于在XML中的<bean>
中編寫(xiě)了一個(gè)<property>
標(biāo)簽是一樣的
@Autowired
:自動(dòng)按照類型注入 只要容器中有唯一的一個(gè)bean對(duì)象類型和要注入的變量類型匹配就可以注入成功
@Qualifier
:按照類型的注入基礎(chǔ)之上再按名稱注入 它在給類成員注入時(shí)不能單獨(dú)使用 ,必須配合和@Autowired使用牡肉。但是給方法參數(shù)注入時(shí)可以
@Resource
:直接按照bean的id注入 它可以獨(dú)立使用 name屬性是bean的id 例如:@Resource(name=accountDao)
注意:
Value
:用于注入基本類型和String類型的數(shù)據(jù)
value屬性:用于指定數(shù)據(jù)的值捧灰。它可以使用spring中SpEL (也就是spring中的EL表達(dá)式)
SpEL的寫(xiě)法:${表達(dá)式}
用于改變作用范圍的:相當(dāng)于在<bean>
標(biāo)簽中使用了scope
一樣。
Scope
:與于指定bean的作用范圍
屬性:value:指定范圍的取值统锤,常用取值:singleton(默認(rèn))(單例)和prototype(多例)
和生命周期相關(guān)的:相當(dāng)于在<bean>
標(biāo)簽中使用了init-menthod
和destriy-menthod
一樣毛俏。
PreDestroy
:指定銷毀方法。
PostConstruct
:指定初始化方法饲窿。
Spring新注解
@Configurtion
:指定當(dāng)前類是一個(gè)spring配置類(配置類的意思就是它的作用和bean.xml是一樣的)
@ComponentScan
:通過(guò)注解指定是Spring在創(chuàng)建容器時(shí)要掃描的包煌寇。。作用和在 spring 的 xml 配置文件中的:<context:component-scan base-package="com.itheima"/>
是一樣的逾雄。
實(shí)例代碼:
@Configuration
@ComponentScan("com.itheima")
public class SpringConfiguration {
}
@Bean
:該注解只能寫(xiě)在方法上阀溶,用于把當(dāng)前方法的返回值作為bean對(duì)象存入到Spring的ioc容器中,name:給當(dāng)前@Bean 注解方法創(chuàng)建的對(duì)象指定一個(gè)名稱(即 bean 的 id)。
@PropertySource
:用于加載.properties 文件中的配置鸦泳。例如我們配置數(shù)據(jù)源時(shí)银锻,可以把連接數(shù)據(jù)庫(kù)的信息寫(xiě)到 properties 配置文件中,就可以使用此注解指定 properties 配置文件的位置.value[]:用于指定 properties 文件位置做鹰。如果是在類路徑下击纬,需要寫(xiě)上 classpath:
@PropertySource("classpath:jdbc.properties")
public class JdbcConfig{
}
@Import
:用于導(dǎo)入其他配置類,在引入其他配置類時(shí)钾麸,可以不用再寫(xiě)@Configuration 注解更振。當(dāng)然,寫(xiě)上也沒(méi)問(wèn) 題,value[]:用于指定其他配置類的字節(jié)碼饭尝。
@Import({ JdbcConfig.class})
public class SpringConfiguration {
}
Spring 整合 Junit
在測(cè)試類中肯腕,每個(gè)測(cè)試方法都有以下兩行代碼:
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); IAccountService as = ac.getBean("accountService",IAccountService.class);
這兩行代碼的作用是獲取容器,如果不寫(xiě)的話钥平,直接會(huì)提示空指針異常乎芳。所以又不能輕易刪掉。
junit 是無(wú)法實(shí)現(xiàn)的,因?yàn)樗约憾紵o(wú)法知曉我們是否使用了 spring 框架奈惑,更不用說(shuō)幫我們創(chuàng)建 spring 容器了吭净。不過(guò)好在,junit 給我們暴露 了一個(gè)注解肴甸,可以讓我們替換掉它的運(yùn)行器寂殉。 這時(shí),我們需要依靠 spring 框架原在,因?yàn)樗峁┝艘粋€(gè)運(yùn)行器友扰,可以讀取配置文件(或注解)來(lái)創(chuàng)建容器。我 們只需要告訴它配置文件在哪就行了.
第一步:導(dǎo)入spring整合的junit的jar包坐標(biāo)庶柿。
第二步:使用junit提供的一個(gè)注解把原有的main方法替換村怪,替換成spring提供的@Runwith
@RunWith(SpringJUnit4ClassRunner.class)
public class AccountServiceTest {
}
第三步:告訴spring的運(yùn)行器,spring和ioc創(chuàng)建是基于xml還是注解 并說(shuō)明位置浮庐。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:bean.xml"})
public class AccountServiceTest {
}
@ContextConfiguration
注解:
locations 屬性:用于指定配置文件的位置甚负。如果是類路徑下,需要用 classpath:表明
classes 屬性:用于指定注解的類审残。當(dāng)不使用 xml 配置時(shí)梭域,需要用此屬性指定注解類的位置。
第四步:使用@Autowired 給測(cè)試類中的變量注入數(shù)據(jù)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:bean.xml"})
public class AccountServiceTest {
@Autowired
private IAccountService as ;
}