1. 程式人生 > 其它 >Spring5學習筆記之IOC操作基於註解方式實現

Spring5學習筆記之IOC操作基於註解方式實現

技術標籤:Spring5的整理總結spring

目錄

註解回憶

什麼是註解?

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

基於註解方式實現物件建立

Spring針對Bean管理中建立物件提供註解

  • @Component:Spring提供的普通註解,使用它可以用來建立物件
  • @Service:一般使用在業務邏輯層或者Service層上
  • @Controller:一般使用在Web層上
  • @Repository:一般是用在DAO層即持久層上

說明:上面四個註解功能是一樣的,都可以用來建立bean例項,即使混著使用也沒事,但是建議不同的業務層使用不同的註解,這樣開發更規範。

實現物件建立

第一步 引入依賴
在這裡插入圖片描述

第二步 開啟元件掃描

    <!--開啟元件掃描
    1 如果掃描多個包,多個包使用逗號隔開
    2 掃描包上層目錄-->
    <context:component-scan base-package="com.athome"
/>

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

//在註解裡面value屬性值可以省略不寫,
// 預設值是類名稱,首字母小寫
@Component(value = "user")
public class User {
    public void login(){
        System.out.println("使用者登入了");
    }
}

開啟元件掃描細節配置

    <!--示例1
    use-default-filters="false" 表示現在不使用預設filter,自己配置filter
    context:include-filter ,設定掃描哪些內容-->
<context:component-scan base-package="com.athome" 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.athome" use-default-filters="true">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

基於註解方式實現屬性注入

@Autowired

@Autowired:根據屬性型別進行自動裝配:

  • 把service和dao物件建立,在service和dao類新增建立物件註解
  • 在service注入dao物件,在service類新增dao型別屬性,在屬性上面使用註解
@Repository
public class Dao {
    public void add() {
        System.out.println("dao呼叫了新增方法");
    }
}
@org.springframework.stereotype.Service
public class Service {
    @Autowired
    private Dao dao;

    public void update() {
        dao.add();
        System.out.println("service呼叫更新方法");
    }
}

@Qualifier

@Qualifier:根據名稱進行注入,注意此註解需要和上面@Autowired一起使用。

@Repository
public class Dao {
    public void add() {
        System.out.println("dao呼叫了新增方法");
    }
}
@org.springframework.stereotype.Service
public class Service {
    @Autowired
    @Qualifier(value = "dao")
    private Dao dao;

    public void update() {
        dao.add();
        System.out.println("service呼叫更新方法");
    }
}

@Resource

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

@org.springframework.stereotype.Service
public class Service {
    @Resource//根據型別注入
    private Dao dao;

    public void update() {
        dao.add();
        System.out.println("service呼叫更新方法");
    }
}
@org.springframework.stereotype.Service
public class Service {
    @Resource(name = "dao")//根據名稱注入
    private Dao dao;

    public void update() {
        dao.add();
        System.out.println("service呼叫更新方法");
    }
}

@Value

@Value:注入普通型別屬性

@org.springframework.stereotype.Service
public class Service {
    @Resource(name = "dao")//根據名稱注入
    private Dao dao;
    @Value("微若塵埃粲若星辰")
    private String name;
    @Value("20")
    private int age;

    public void update() {
        dao.add();
        System.out.println("service呼叫更新方法");
    }

    @Override
    public String toString() {
        return "Service{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

完全註解開發

(1)建立配置類,替代xml配置檔案

@Configuration //作為配置類,替代xml配置檔案
@ComponentScan(basePackages = {"com.athome"})
public class SpringConfig {
}

(2)編寫測試類

    @Test
    public void test02(){
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        Service service = context.getBean("service", Service.class);
        service.update();
        System.out.println(service);
    }

結語

只要能收穫甜蜜,荊棘叢中也有蜜蜂忙碌的身影,未來的你一定會感謝現在努力的自己。