最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring IOC相關注解運用(上篇)

 更新時間:2023年05月05日 11:39:08   作者:會洗碗的CV工程師  
這篇文章主要介紹了Spring?IOC相關注解的運用,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

前言

注解配置和xml配置對于Spring的IOC要實現(xiàn)的功能都是一樣的,只是配置的形式不一樣。
準備工作:

  • 創(chuàng)建一個新的Spring項目。
  • 編寫pojo,dao,service類。
  • 編寫空的配置文件,如果想讓該文件支持注解,需要在bean.xml添加新的約束:
<?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">
 
    <context:component-scan base-package="com.example"/>
    <context:property-placeholder location="db.properties"/>
 
</beans>

一、@Component

@Component可以代替bean標簽

  • 作用:用于創(chuàng)建對象,放入Spring容器,相當于 <bean id="" class="">
  • 位置:類上方
  • 注意:@Component 注解配置bean的默認id是首字母小寫的類名。也可以手動設置bean的id值。
// 此時bean的id為studentDaoImpl
@Component
public class StudentDaoImpl implements StudentDao{
  public Student findById(int id) {
    // 模擬根據id查詢學生
    return new Student(1,"程序員","北京");
 }
// 此時bean的id為studentDao
@Component("studentDao")
public class StudentDaoImpl implements StudentDao{
  public Student findById(int id) {
    // 模擬根據id查詢學生
    return new Student(1,"程序員","北京");
 }
}

二、@Repository、@Service、@Controller

作用:這三個注解和@Component的作用一樣,使用它們是為了區(qū)分該類屬于什么層。
位置:

  • @Repository用于Dao層
  • @Service用于Service層
  • @Controller用于Controller層
@Repository
public class StudentDaoImpl implements StudentDao{}
@Service
public class StudentService {}

三、@Scope

作用:指定bean的創(chuàng)建策略
位置:類上方
取值:singleton prototype request session globalsession 

@Service
@Scope("singleton")
public class StudentService {}

測試一下: 

    @Test
    public void t1(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        StudentDao studentDao = (StudentDao) ac.getBean("studentDao");
        System.out.println(studentDao);
        StudentService service1 = (StudentService) ac.getBean("studentService");
        System.out.println(service1.hashCode());
        StudentService service2 = ac.getBean("studentService",StudentService.class);
        System.out.println(service2.hashCode());
    }

OK,確實可以

四、@Autowired

作用:從容器中查找符合屬性類型的對象自動注入屬性中。用于代替 <bean> 中的依賴注入配置。
位置:屬性上方、setter方法上方、構造方法上方。
注意:@Autowired 寫在屬性上方進行依賴注入時,可以省略setter方法。

@Component
public class StudentService {
  @Autowired
  private StudentDao studentDao;
  public Student findStudentById(int id){
    return studentDao.findById(id);
 }
}
 
// 測試方法
@Test
public void t2(){
  ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
  StudentService studentService = (StudentService) ac.getBean("studentService");
  System.out.println(studentService.findStudentById(1));
}

測試結果:

OK,也是可以的 

五、@Qualifier

作用:在按照類型注入對象的基礎上,再按照bean的id注入。
位置:屬性上方
注意:@Qualifier必須和@Autowired一起使用。

如下

@Component
public class StudentService {
  @Autowired
  @Qualifier("studentDaoImpl2")
  private StudentDao studentDao;
  public Student findStudentById(int id){
    return studentDao.findById(id);
 }
}

六、@Value

作用:注入String類型和基本數據類型的屬性值。
位置:屬性上方

以下說明一下用法:

1. 直接設置固定的屬性值

    @Value("1")
    private int count;
 
    @Value("hello")
    private String str;

2. 獲取配置文件中的屬性值

編寫配置文件db.properties

jdbc.username=root
jdbc.password=123456

spring核心配置文件(bean.xml)掃描配置文件

<context:property-placeholder location="db.properties"/>

注入配置文件中的屬性值

    @Value("${jdbc.username}")
    private String username;
 
    @Value("${jdbc.password}")
    private String password;

3. 測試結果

測試方法

    // 測試注解Value
    @Test
    public void t3(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        StudentService service = ac.getBean("studentService",StudentService.class);
        System.out.println(service);
    }

運行結果

OK,應該和上面設置的值一樣,說明可以使用,本篇就介紹到這幾個注解了,下篇會介紹完接下來的注解。 

以上就是Spring IOC相關注解運用(上篇)的詳細內容,更多關于Spring IOC注解的資料請關注腳本之家其它相關文章!

相關文章

  • 如何取消IDEA中的項目和Git倉庫的關聯(lián)

    如何取消IDEA中的項目和Git倉庫的關聯(lián)

    這篇文章介紹了一種取消已經開啟代碼控制的項目與Git倉庫關聯(lián)的簡單方法,首先,在IntelliJ IDEA中打開項目,刪除.idea目錄下的vcs.xml文件,這樣IDE界面上的Git圖標就會消失,接下來,打開項目所在位置,通過文件管理器啟用“顯示隱藏的項目”選項
    2024-10-10
  • Kotlin 的注解類詳解及實例

    Kotlin 的注解類詳解及實例

    這篇文章主要介紹了Kotlin 的注解類詳解及實例的相關資料,需要的朋友可以參考下
    2017-06-06
  • 詳解Java如何跨平臺獲取MAC地址

    詳解Java如何跨平臺獲取MAC地址

    有時我們因為軟件授權或者其它需要獲取主機唯一標識而需要獲取用戶主機的MAC地址,而本文則將介紹如何通過Java來實現(xiàn)跨平臺獲取MAC地址的兩種方法,需要的朋友可以參考下
    2021-06-06
  • SpringBoot主鍵ID傳到前端后精度丟失的問題解決

    SpringBoot主鍵ID傳到前端后精度丟失的問題解決

    這篇文章主要通過示例為大家詳細介紹一些SpringBoot如何解決雪花算法主鍵ID傳到前端后精度丟失問題,文中的示例代碼講解詳細,需要的可以參考一下
    2022-05-05
  • Spring?Boot?Actuator管理日志的實現(xiàn)

    Spring?Boot?Actuator管理日志的實現(xiàn)

    本文主要介紹了Spring?Boot?Actuator管理日志的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • SpringBoot事件發(fā)布與監(jiān)聽超詳細講解

    SpringBoot事件發(fā)布與監(jiān)聽超詳細講解

    今天去官網查看spring boot資料時,在特性中看見了系統(tǒng)的事件及監(jiān)聽章節(jié),所以下面這篇文章主要給大家介紹了關于SpringBoot事件發(fā)布和監(jiān)聽的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-11-11
  • ReentrantLock獲取鎖釋放鎖的流程示例分析

    ReentrantLock獲取鎖釋放鎖的流程示例分析

    這篇文章主要為大家介紹了ReentrantLock獲取鎖釋放鎖的流程示例分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • IDEA設置Tab選項卡快速的操作

    IDEA設置Tab選項卡快速的操作

    這篇文章主要介紹了IDEA設置Tab選項卡快速的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • 使用spring?security?BCryptPasswordEncoder接入系統(tǒng)

    使用spring?security?BCryptPasswordEncoder接入系統(tǒng)

    這篇文章主要介紹了使用spring?security?BCryptPasswordEncoder接入系統(tǒng)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • 解決mybatis-plus新增數據自增ID變無序問題

    解決mybatis-plus新增數據自增ID變無序問題

    這篇文章主要介紹了解決mybatis-plus新增數據自增ID變無序問題,具有很好的參考價值,希望對大家有所幫助。
    2023-07-07

最新評論

华安县| 内乡县| 灌阳县| 大关县| 山丹县| 文水县| 东丽区| 永寿县| 大悟县| 阜宁县| 乐东| 永州市| 云和县| 吉木乃县| 应用必备| 什邡市| 忻州市| 资中县| 江阴市| 丹寨县| 汾阳市| 渝中区| 农安县| 开江县| 喀喇沁旗| 泸溪县| 米泉市| 惠安县| 元阳县| 绥芬河市| 潼关县| 永和县| 明光市| 惠东县| 丰原市| 洪江市| 修文县| 汉阴县| 南汇区| 桦川县| 虞城县|