1 基于注解的IOC配置
1.1 建立spring的xml配置檔案并開啟對注解的支援
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnL2MzM4EzNxkDM1IzMwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
注意:
基于注解整合時,導入限制時需要多導入一個 context 名稱空間下的限制。
由于我們使用了注解配置,此時不能在繼承 JdbcDaoSupport,需要自己配置一個 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 建立容器時要掃描的包 -->
<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>
<!-- 配置資料源 -->
<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>
1.2 常用注解
1.2.1 用于建立對象的
相當于bean标簽< bean id="" class="">
[email protected]
作用:
把資源讓 spring 來管理。相當于在 xml 中配置一個 bean。
屬性:
value:指定 bean 的 id。如果不指定 value 屬性,預設 bean 的 id 是目前類的類名。首字母小寫。
1.2.1.2 @Controller @Service @Repository
這三個注解都是針對一個的衍生注解,他們的作用及屬性都是一摸一樣的。隻不過是提供了更加明确的語義化。
@Controller:一般用于表現層的注解
@Service:一般用于業務層的注解
@Repository:一般用于持久層的注解
如果注解中有且隻有一個屬性要指派時,且名稱是value,value在指派時可以不寫
1.2.2 用于注入資料的
相當于:< property name="" ref="">
< property name="" value="">
1.2.2.1 @Autowired
作用:自動按照類型注入。當使用注解注入屬性時,set方法可以省略。它隻能注入其他bean類型。當有多個類型比對時,使用要注入的對象變量名稱作為bean的id,在spring容器查找,找到了就可以注入成功,找不到就會報錯。
1.2.2.2 @Qualifier
作用:在自動按照類型注入的基礎之上,再按照Bean的id注入。它在給字段注入時不能獨立使用,必須和@Autowire一起使用,但是在給方法參數注入時,可以獨立使用。
屬性:value:指定bean的id
1.2.2.3 @Resource
作用:直接按照Bean的id注入
屬性:name:指定bean的id
1.2.2.4 @Value
作用:注入基本資料類型和String類型資料的
屬性:value:用于指定值
1.2.3 用于改變作用範圍的
相當于:< bean id="" scope="">
1.2.3.1 @Scope
作用:指定bean的作用範圍
屬性:value:指定範圍的值
取值:singleton prototype request session globalsession
1.3 新注解說明
1.3.1 @Configuration
作用:
用于指定目前類是一個 spring 配置類, 當建立容器時會從該類上加載注解。 擷取容器時需要使用
AnnotationApplicationContext(有@Configuration 注解的類.class)。
屬性:
value:用于指定配置類的位元組碼
1.3.2 @ComponentScan
作用:
用于指定 spring 在初始化容器時要掃描的包。 作用和在 spring 的 xml 配置檔案中的:
<context:component-scan base-package=“com.itheima”/>是一樣的。
屬性:
basePackages:用于指定要掃描的包。和該注解中的 value 屬性作用一樣。
1.3.3 @Bean
作用:
該注解隻能寫在方法上,表明使用此方法建立一個對象,并且放入 spring 容器。
屬性:
name:給目前@Bean 注解方法建立的對象指定一個名稱(即 bean 的 id)。
1.3.4 @PropertySource
作用:
用于加載.properties 檔案中的配置。例如我們配置資料源時,可以把連接配接資料庫的資訊寫到
properties 配置檔案中,就可以使用此注解指定 properties 配置檔案的位置。
屬性:
value[]:用于指定 properties 檔案位置。如果是在類路徑下,需要寫上 classpath:
1.3.5
作用:
用于導入其他配置類,在引入其他配置類時,可以不用再寫@Configuration 注解。 當然,寫上也沒問題。
屬性:
value[]:用于指定其他配置類的位元組碼。
2 spring整合Junit
2.1 配置步驟
2.1.1 第一步:拷貝整合 junit 的必備 jar 包到 lib 目錄或者使用maven坐标的方式添加
2.1.2 第二步:使用@RunWith 注解替換原有運作器
@RunWith(SpringJUnit4ClassRunner.class)
public class AccountServiceTest {
}
2.1.3 第三步:使用@ContextConfiguration 指定 spring 配置檔案的位置
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:bean.xml"})
public class AccountServiceTest {
}
@ContextConfiguration 注解:
locations 屬性: 用于指定配置檔案的位置。如果是類路徑下,需要用 classpath:表明
classes 屬性: 用于指定注解的類。當不使用 xml 配置時,需要用此屬性指定注解類的位置
2.1.4 使用@Autowired 給測試類中的變量注入資料
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:bean.xml"})
public class AccountServiceTest {
@Autowired
private IAccountService as ;
}