1. 程式人生 > 其它 >spring5之入門2篇(基於註解開發)

spring5之入門2篇(基於註解開發)

spring使用註解開發

記錄:

spring5學習筆記
學習視訊連結地址:https://www.bilibili.com/video/BV1Vf4y127N5p=26&spm_id_from=pageDriver


IOC操作bean(基於註解方式)

1、什麼是註解
(1)註解是程式碼特殊標記
格式:@註解名稱(屬性名稱=屬性值, 屬性名稱=屬性值…)
(2)使用註解,註解作用在類上面,方法上面,屬性上面
(3)使用註解目的:簡化 xml 配置

2、Spring 針對 Bean 管理中建立物件提供註解
(1)@Component
(2)@Service
(3)@Controller
(4)@Repository

* 上面四個註解功能是一樣的,都可以用來建立 bean例項

3、基於註解方式實現物件建立 :
第一步 引入依賴:spring-aopj的ar包依賴
在這裡插入圖片描述

第二步 新增名稱空間並開啟元件掃描

<!--新增名稱空間 -->
 xmlns:context="http://www.springframework.org/schema/context" 
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
<! -- 開啟元件掃描     
	1
如果掃描多個包,多個包使用逗號隔開 2 掃描包上層目錄 -- > <context:component-scan base-package="com.atguigu"></context:component-scan>

第三步 建立類,在類上面新增建立物件註解

// 在註解裡面 value 屬性值可以省略不寫, 
// 預設值是類名稱,首字母小寫 
//Book -- book 
@Component   //相當於<bean id="book" class="com.itheima.hww.Book">
public class Book { @Value("5") //相當於<property name="bid" value="1"></property> private int bid; @Value("九陽神功") //相當於<property name="bname" value="如來神掌"></property> private String bname; } //測試 @Test public void book(){ //1、載入spring配置檔案 ApplicationContext Context = new ClassPathXmlApplicationContext("bean.xml"); //2、獲取配置檔案物件 Book book = Context.getBean("book", Book.class); System.out.println(book); book.toString(); } }

測試結果:
在這裡插入圖片描述

4、開啟元件掃描細節配置

<! -- 示例 1     
	use - default - filters="false" 表示現在不使用預設 filter ,自己配置 filter     
	context: include - filter ,設定掃描哪些內容 -- >
	
<context:component-scan base-package="com.atguigu" use-default filters="false">     
<context:include-filter type="annotation"
		expression="org.springframework.stereotype.Controller"/> 
</context:component-scan> 
 
<! -- 示例 2     
下面配置掃描包所有內容     
context:exclude - filter : 設定哪些內容不進行掃描 -- >

<context:component-scan base-package="com.atguigu"> 
	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

5、基於註解方式實現屬性注入
(1)@Autowired:根據屬性型別進行自動裝配
第一步 把 service 和 dao 物件建立,在 service 和 dao 類新增建立物件註解
第二步 在 service 注入 dao 物件,在 service 類新增 dao 型別屬性,在屬性上面使用註解

@Service 
public class UserService {     
	// 定義 dao 型別屬性     
	// 不需要新增 set 方法     
	// 添加註入屬性註解     
	@Autowired     
	private UserDao userDao; 
	public void add() {        
		System. out .println("service add.......");         
		userDao.add();     
	} 
} 

(2)@Qualifier:根據名稱進行注入
這個@Qualifier 註解的使用,和上面@Autowired 一起使用

// 定義 dao 型別屬性 
// 不需要新增 set 方法 
// 添加註入屬性註解 
@Autowired  // 根據型別進行注入 
@Qualifier(value = "userDaoImpl1") // 根據名稱進行注入 
private UserDao userDao;

(3)@Resource:可以根據型別注入,可以根據名稱注

//@Resource  根據型別進行注入 
@Resource(name = "userDaoImpl1")  // 根據名稱進行注入 
private UserDao userDao;

(4)@Value:注入普通型別屬性

@Value(value = "abc") 
private String name;

6、完全註解開發
(1)建立配置類,替代 xml 配置檔案

@Configuration  // 作為配置類,替代 xml 配置檔案 
@ComponentScan(basePackages = {"com.atguigu"})   //開啟元件掃描
public class SpringConfig {
}

(2)編寫測試類

@Test public void testService2() {     
// 載入配置類(載入配置類的位元組碼檔案)     
	ApplicationContext context= new AnnotationConfigApplicationContext(SpringConfig.class);     
	UserService userService = context.getBean("userService", UserService.class);     		
	System. out .println(userService);     
	userService.add(); 
}