目錄
- 一、使用注解的步驟
- 二、@Component
- 三、@Value
- 四、@Autowired
- 五、@Qualifier
- 六、@Resource
- 七、XML和注解對比
通過spring的注解完成對java物件的創建,屬性的賦值,代替xml檔案
ioc能夠實作業務和物件之間的解耦合,例如service和dao物件之間的解耦合
常用注解:
- @Component、創建物件
- @Respotory、創建dao物件,用來訪問資料庫
- @Service、創建Service物件,處理業務邏輯,可以有事務的功能
- @Controller、創建控制器物件,接收請求,顯示處理結果
- @Value、簡單型別的屬性賦值
- @Autowired、spring框架中參考型別賦值的注解,支持byName、byType,默認byType
- @Resource、jdk中的注解,使用自動注入給參考資料型別賦值,支持byName、byType,默認byName
一、使用注解的步驟
- 加入maven的依賴 spring-context ,在你加入spring-context的同時, 間接加入spring-aop的依賴,使用注解必須使用spring-aop依賴
- 在類中加入spring的注解(多個不同功能的注解)
- 在spring的組態檔中,加入一個組件掃描器的標簽,說明注解在你的專案中的位置
<context:component-scan base-package="包名"/>
- 使用注解創建物件, 創建容器ApplicationContext
首先還是創建一個新的專案,在pom.xml檔案中加入依賴
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
二、@Component
首先創建學生類
package com.md.b1;
import org.springframework.stereotype.Component;
/**
* @author MD
* @create 2020-08-08 15:51
* @Component: 創建物件的, 等同于<bean>的功能
* 屬性:value 就是物件的名稱,也就是bean的id值,
* value的值是唯一的,創建的物件在整個spring容器中就一個
* 位置:在類的上面
*
* @Component(value = https://www.cnblogs.com/mengd/p/"myStudent")等同于
*
*/
//@Component(value = "myStudent")
// 省略value,常用
@Component("myStudent")
// 不指定物件的名稱,由spring默認提供:為類名首字母小寫(student)
//@Component
public class Student {
private String name;
private Integer age;
public Student() {
System.out.println("我是Student的無參構造方法");
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
然后在spring的組態檔中applicationContext.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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--宣告組件掃描器,組件就是java物件
base-package:指定注解在你專案中的包名
component-scan作業方式:spring會掃描遍歷base-package指定的包,
找包中以及子包中的所有的類,并找到類中的注解,
按照注解的功能創建物件,或者給屬性賦值
-->
<context:component-scan base-package="com.md.b1"/>
</beans>
測驗:
package com.md;
import com.md.b1.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author MD
* @create 2020-08-08 16:05
*/
public class MyTest01 {
@Test
public void test01(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// 從容器中獲取物件
Student student = (Student) ac.getBean("myStudent");
System.out.println(student);
}
}
注意一:指定多個包的三種方式
<!--第一種方式:使用多次組件掃描器,指定不同的包-->
<context:component-scan base-package="com.md.b1"/>
<context:component-scan base-package="com.md.b2"/>
<!--第二種方式:使用分隔符(;或,)分隔多個包名-->
<context:component-scan base-package="com.md.b1;com.md.b2" />
<!--第三種方式:指定父包-->
<context:component-scan base-package="com.md" />
重點:
注意二:spring中和@Component功能一致,創建物件的注解還有
- @Repository(用在持久層類的上面) : 放在dao的實作類上面,表示創建dao物件,dao物件是能訪問資料庫的
- @Service(用在業務層類的上面):放在service的實作類上面,創建service物件,service物件是做業務處理,可以有事務等功能的
- @Controller(用在控制器的上面):放在控制器(處理器)類的上面,創建控制器物件的,控制器物件,能夠接受用戶提交的引數,顯示請求的處理結果,
以上三個注解的使用語法和@Component一樣的, 都能創建物件,但是這三個注解還有額外的功能
@Repository,@Service,@Controller是給專案的物件分層的
某個類使用上面的三個注解都不合適的時候使用@Component
問題:創建物件的注解有幾個,為什么有多個?
三、@Value
簡單型別屬性注入,需要在屬性上使用注解@Value,該注解的 value 屬性用于指定要注入的值
使用該注解完成屬性注入時,類中無需 setter,當然,若屬性有 setter,則也可將其加到 setter 上
如下:
package com.md.b2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @author MD
* @create 2020-08-08 15:51
*/
@Component("myStudent")
public class Student {
/**
* @Value: 簡單型別的屬性賦值
* 屬性: value 是String型別的,表示簡單型別的屬性值
* 位置: 1.在屬性定義的上面,無需set方法,推薦使用,
* 2.在set方法的上面
*/
// @Value(value = https://www.cnblogs.com/mengd/p/"比比東")
@Value("比比東")
private String name;
// @Value(value = "18")
@Value("22")
private Integer age;
public Student() {
System.out.println("我是Student的無參構造方法");
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
然后在spring的組態檔中applicationContext.xml加入組件掃描器
<context:component-scan base-package="com.md.b2"/>
測驗
@Test
public void test01(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// 從容器中獲取物件
Student student = (Student) ac.getBean("myStudent");
System.out.println(student);
//我是Student的無參構造方法
//Student{name='比比東', age=22}
}
四、@Autowired
byType自動注入
@Autowired:spring框架提供的注解,實作參考型別的賦值
需要在參考屬性上使用注解@Autowired,該注解默認使用按型別自動裝配Bean的方式(byType)
package com.md.b3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @author MD
* @create 2020-08-08 16:39
*/
@Component("student")
public class Student {
@Value("比比東")
private String name;
@Value("20")
private Integer age;
/**
* 參考資料型別
* @Autowired:spring框架提供的注解,實作參考型別的賦值
* spring中通過注解給參考資料型別賦值的原理是使用自動注入,支持byName,byType
* 默認使用的byType默認注入
*
* 使用:在屬性定義的上面,無需set方法,推薦
* 在set方法的上面
*/
// 使用注解在School類里定義物件名稱,以及屬性值 或 使用bean的方式創建物件以及通過set方法給屬性賦值
@Autowired
private School school;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", school=" + school +
'}';
}
}
School類
package com.md.b3;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @author MD
* @create 2020-08-08 16:40
*/
@Component("mySchool")
public class School {
@Value("交大")
private String name;
@Value("上海")
private String address;
@Override
public String toString() {
return "School{" +
"name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
public void setName(String name) {
this.name = name;
}
public void setAddress(String address) {
this.address = address;
}
}
然后在spring的組態檔中applicationContext.xml加入組件掃描器
<context:component-scan base-package="com.md.b3"/>
測驗和上面一樣,就不寫了,除了使用注解還可以使用xml的方式
這樣就是可以的,
<bean id="school" class="com.md.b3.School">
<property name="name" value=https://www.cnblogs.com/mengd/p/"上海交通大學"/>
五、@Qualifier
byName自動注入
由于@Autowired 默認使用的是byType,為了使用byName方式,
需要在參考屬性上聯合使用注解@Autowired 與@Qualifier
@Qualifier 的 value 屬性用于指定要匹配的 Bean 的 id 值,
使用byName方式
1. 屬性的上面加入@Autowired注解
2. 屬性的上面加@Qualifier(value=https://www.cnblogs.com/mengd/p/"bean的id")注解,表示使用指定名稱的bean完成賦值
也就是使用這個物件來完成賦值
School類
@Component("school")
public class School {
@Value("廈門大學")
private String name;
@Value("廈門")
private String address;
@Override
public String toString() {
return "School{" +
"name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
}
Student類
@Component("student")
public class Student {
@Value("比比東")
private String name;
@Value("20")
private Integer age;
// 在School類里通過注解直接物件名為school
@Autowired
@Qualifier("school")
private School school;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", school=" + school +
'}';
}
}
屬性:required ,是一個boolean型別的,默認true
- required=true:表示參考型別賦值失敗,程式報錯,并終止執行,推薦使用
- required=false:參考型別如果賦值失敗, 程式正常執行,參考型別是null
例如:還是在上面的程式上
使用的是byName的方式,而此時失誤bean的id寫錯了
- 若使用默認值,則結果或報錯
- 若按照下面寫的執行,則不會報錯,但資料為空
@Component("student")
public class Student {
@Value("比比東")
private String name;
@Value("20")
private Integer age;
// 在School類里通過注解直接物件名為school
@Autowired(required = false)
@Qualifier("schoo")
private School school;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", school=" + school +
'}';
}
}
六、@Resource
spring提供了對jdk中@Resource注解的支持,
@Resource注解既可以按名稱匹配Bean,也可以按型別匹配 Bean, 默認是按名稱注入(byName)
使用該注解,要求 JDK 必須是 6 及以上版本,@Resource 可在屬性上,也可在 set 方法上
還是School類中
@Component("school")
public class School {
@Value("浙江大學")
private String name;
@Value("浙江")
private String address;
@Override
public String toString() {
return "School{" +
"name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
}
此時Student類
@Component("student")
public class Student {
@Value("比比東")
private String name;
@Value("20")
private Integer age;
/**
* 參考型別
* @Resource: 來自jdk中的注解,spring框架提供了對這個注解的功能支持,可以使用它給參考型別賦值
* 使用的也是自動注入原理,支持byName, byType .默認是byName
* 位置: 1.在屬性定義的上面,無需set方法,推薦使用,
* 2.在set方法的上面
*/
//默認是byName: 先使用byName自動注入,如果byName賦值失敗,再使用byType
//@Resource
// 若只使用byName方式,需要增加一個屬性 name,name的值就是bean的id(物件名稱)
@Resource(name = "school")
private School school;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", school=" + school +
'}';
}
}
在spring的組態檔中applicationContext.xml加入組件掃描器
<context:component-scan base-package="com.md.b6"/>
測驗:
@Test
public void test01(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// 從容器中獲取物件
Student student = (Student) ac.getBean("student");
System.out.println(student);
// Student{name='比比東', age=20, school=School{name='浙江大學', address='浙江'}}
}
七、XML和注解對比
注解優點是:方便、直觀、高效(代碼少,沒有組態檔的書寫那么復雜)
缺點:以硬編碼的方式寫入到 Java 代碼中,修改是需要重新編譯代碼的
適合于不是經常修改的
XML 方式優點是:配置和代碼是分離的、在 xml 中做修改,無需編譯代碼,只需重啟服務器即可將新的配置加載
缺點:撰寫麻煩,效率低,大型專案過于復雜
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/86673.html
標籤:Java
上一篇:Spring——IOC(控制反轉)與DI(依賴注入)
下一篇:JVM垃圾回收(GC)
