基于注解裝配
Spring架構為了友善開發者,給開發者提供了一系列的注解,來簡化開發的過程。
注解分别有@Component 、@Repository、@Service、@Controller、@Autowired、@Resource、@Qualifier
@Component注解
這個注解是一個泛化的概念,代表一個元件對象(Bean),@Component注解可以代替上面前6個注解使用,可以作用在任何層次上面使用。
@Repository注解
這個注解應用于資料通路層(DAO)辨別作為一個Bean,其标注的意思就是告訴Spring架構,給該類建立對應的執行個體對象
@Service注解
該注解标注在業務層,表示把該業務對象交給Spring管理,并給該類建立相應的執行個體對象。
@Controller注解
該注解用于辨別一個控制器類元件(在SpringMVC子產品中存在)
@Autowired注解
該注解對類的成員變量方法及構造方法進行标注,完成自動裝配工作,同時也消除了成員變量的setter方法和getter方法。
注意
1.預設是根據被标注的對象類型來注入的,如果找到,直接使用setter方法或者字段進行注入
2.如果Spring上下文中找到對個比對的類型,則在按照名字進行比對,比對不到者報錯。
3.可以使用@Qualifier(“other”) 注解來規定依賴的對象安裝bean的id和類型的組合方式進行比對。
@Resource注解
@Resource裝配順序:
①如果同時指定了name和type,則從Spring上下文中找到唯一比對的bean進行裝配,找不到則抛出異常。
②如果指定了name,則從上下文中查找名稱(id)比對的bean進行裝配,找不到則抛出異常。
③如果指定了type,則從上下文中找到類似比對的唯一bean進行裝配,找不到或是找到多個,都會抛出異常。
④如果既沒有指定name,又沒有指定type,則自動按照byName方式進行裝配;如果沒有比對,則回退為一個原始類型進行比對,如果比對則自動裝配。
@Resource的作用相當于@Autowired,隻不過@Autowired按照byType自動注入
@Resource注解和@Autowired注解差別:
@Autowired注解預設是根據對象的類型來裝配注入的 而@Resource注解預設是根據名稱來裝配注入
如果使用了上述注解我們者需要在主配置檔案編寫如下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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置Ioc注解解析器 掃描包-->
<context:component-scan base-package="com.iarchie.spring.auto"/>
<!-- 配置DI注解解析器 DI注入-->
<context:annotation-config/>
</beans>
注意關于Spring配置檔案的命名預設全局使用的是applicationContext.xml,但是我們編寫測試的時候可以這樣寫,如 XXX-context.xml Spring都會預設識别。
基于注解的IOC案例
1.我們建立立一個DAO層分别對應的一個接口一個實作類,在建立Service層,對應的一個接口一個實作類。
2.編寫DAO層
//DAO接口
package com.iarchie.spring.auto.dao;
public interface EmployeeDAO {
void sayHello();
}
//DAO實作類,實作上面的接口
import org.springframework.stereotype.Repository;
import com.iarchie.spring.auto.dao.EmployeeDAO;
/**
*
* <p>Title: EmployeeDAOImpl.java</p>
* <p>Description: DAO實作類 </p>
* @author iArchie
* @date 2019年3月1日
* @version 1.0
*/
@Repository //表示該層是DAO層并交給Spring容器管理并執行個體化,預設bean名字為employeeDAO
public class EmployeeDAOImpl implements EmployeeDAO {
@Override
public void sayHello() {
System.out.println("EmployeeDAO 方法.....");
}
}
3.編寫業務層
4.編寫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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置Ioc注解解析器 -->
<context:component-scan base-package="com.iarchie.spring.auto"/>
<!-- 配置DI注解解析器 -->
<context:annotation-config/>
</beans>
//Service接口
package com.iarchie.spring.auto.service;
public interface EmployeeService {
void sayHello();
}
//實作上面的Service接口
package com.iarchie.spring.auto.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.iarchie.spring.auto.dao.EmployeeDAO;
import com.iarchie.spring.auto.service.EmployeeService;
/**
*
* <p>Title: EmployeeServiceImpl.java</p>
* <p>Description: 業務層 </p>
* @author iArchie
* @date 2019年3月1日
* @version 1.0
*/
@Service //貼上該注解表示業務層,交給Spring管理并執行個體化,預設bean名字為employeeService
public class EmployeeServiceImpl implements EmployeeService {
//自動注入DAO元件
@Autowired
private EmployeeDAO employeeDAO;
@Override
public void sayHello() {
employeeDAO.sayHello();
}
}
5.編寫測試,使用Spring提供的測試
package com.iarchie.spring.auto;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import com.iarchie.spring.auto.service.EmployeeService;
@SpringJUnitConfig //Spring5提供的測試
public class AppTest {
// 注入業務層
@Autowired
private EmployeeService employeeService;
@Test
void test() {
employeeService.sayHello();
}
}
執行看到如下結果,表示基于注解的IOC案例已經測試成功!
資訊: Refreshing org.springframework.context.support.GenericApplicationContext@130f889: startup date [Fri Mar 01 16:09:15 CST 2019]; root of context hierarchy
EmployeeDAO 方法.....