spring 使用注解注入


要使用注解,首先要給bean.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
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!--告知spring在創建容器時要掃描的包,
            配置所需要的標簽不是在beans的約束中,而是一個名稱為context名稱空間和約束中-->
    <context:component-scan base-package="com.itheima">
    </context:component-scan>

</beans>
package com.itheima.service.impl;

import com.itheima.dao.IAccountDao;
import com.itheima.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * 曾經xml的配置
 * <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"
 *      scope="" init-method="" destroy-method="">
 *          <property name="" value/ref=""></property>
 *      </bean>
 *
 * 注解的功能分類:
 * 用於創建對象的注解
 *      和xml配置文件里的<bean>標簽類似
 *      @Component:
 *          作用:用於把當前類對象存入spring容器中
 *          屬性:
 *              value:用於指定bean的id,當不寫時,默認值是當前類名且首字母改小寫
 *      @Controller:一般用在表現層
 *      @Service:一般用在業務層
 *      @Repository:一般用在持久層
 *      以上三個注解的作用和屬性與Component是一模一樣的
 *
 * 用於注入數據的
 *      和xml配置文件里的<bean>里的<property>標簽作用類似
 *      @Autowired:
 *          作用:自動按照類型注入,只要容器中有唯一的bean對象類型和要注入的變量類型匹配,就可以注入成功
 *                  如果ioc容器中沒有任何bean的類型和要注入的變量類型匹配,則報錯
 *                  如果有多個匹配:首先按類型圈定可匹配的bean對象,再使用變量名稱作為bean的id,在已經圈定出來的對象里繼續查找
 *          出現位置:
 *              可以是變量上,也可以是方法上
 *          細節:
 *              在使用注解注入時,set方法就不是必須的
 *      @Qualifier:
 *          作用:在按照類中注入的基礎上,再按照名稱注入。他在給類成員注入時不能單獨使用,但在給方法參數注入時可以
 *          屬性:
 *              value:用於指定注入bean的id
 *      @Resource:(好像用不了)
 *          作用:直接按照bean的id注入。可以獨立使用
 *          屬性:
 *              name:用於指定bean的id
 *      三個注入都只能注入其他bean類的數據,而基本類型和String類型無法使用上述注解注入
 *      集合類型的注入只能通過xml來實現
 *       @Value
 *          作用:用於注入基本類型和String類型的數據
 *          屬性:
 *              value:用於指定數據的值,同時可以使用spring中的SpEL(spring中的el表達式)
 *                    SpEL的寫法:${表達式}
 *
 *  用於改變作用范圍的
 *      和<bean scopr="">類似
 *      Scope
 *          作用:用於指定bean的作用范圍
 *          屬性:
 *              value:指定范圍的取值。常用取值:singleton prototype
 *
 * 和生命周期相關
 *      和<bean init-method="" destroy-method="">類似
 *      PreDesTory
 *          作用:用於指定銷毀方法
 *      PostConstruct
 *          作用:用於指定初始化方法
 *
 *
 *
 * */
@Component(value="accountService")
@Scope("prototype")
public class AccountServiceImpl implements IAccountService {

    @Autowired
    @Qualifier("accountDao") //必須要和Autowired配合
    private IAccountDao accountDao ;

    public AccountServiceImpl(){
        System.out.println("service對象創建了");
    }

    @PostConstruct
    public void init(){
        System.out.println("初始化方法");
    }
    @PreDestroy
    public void destroy(){
        System.out.println("銷毀方法");
    }

    public void  saveAccount(){
        accountDao.saveAccount();
    }
}

 

注意:使用PostConstruct,以及PreDestroy,要在pom配置里導入依賴

<dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>jsr250-api</artifactId>
            <version>1.0</version>
        </dependency>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM