1. 程式人生 > 其它 >8. Spring 註解開發(原始註解)

8. Spring 註解開發(原始註解)

Spring原始註解

Spring是輕程式碼而重配置的框架,配置比較繁重,影響開發效率,所以註解開發是一種趨勢,註解代替xml配置 檔案可以簡化配置,提高開發效率。

Spring原始註解主要是替代的配置:



註解

說明

@Component

使用在類上用於例項化Bean

@Controller

使用在web層類上用於例項化Bean

@Service

使用在service層類上用於例項化Bean

@Repository

使用在dao層類上用於例項化Bean

@Autowired

使用在欄位上用於根據型別依賴注入

@Qualifier

結合@Autowired一起使用用於根據名稱進行依賴注入

@Resource

相當於@Autowired+@Qualifier,按照名稱進行注入

@Value

注入普通屬性

@Scope

標註Bean的作用範圍

@PostConstruct

使用在方法上標註該方法是Bean的初始化方法

@PreDestroy

使用在方法上標註該方法是Bean的銷燬方法



用註解開發:

先來個普通的DaoImpl 注入到 ServiceImpl 的原始程式碼,然後我們吧Spring的配合檔案拆分,分別對應註解來寫:

注意: 使用註解進行開發時,需要在Spring配置檔案中配置元件掃描作用是指定哪個包及其子包下的Bean 需要進行掃描以便識別使用註解配置的類、欄位和方法。【簡單點說就是掃描指定包下的所有檔案 掃描下看有沒有註解 有的話就自動解析配入 】:

在Spring配置檔案中寫:

<!--註解的元件掃描-->
<context:component-scan base-package="com.包名"></context:componentscan>

這三句話 很重要:【多種註解配合注入Bena / 引數

使用@Compont或@Repository標識DaoImpl

需要Spring進行例項化。

使用@Compont或@Service標識ServiceImpl需要Spring進行例項化

使用@Autowired或者@Autowired+@Qulifier或者@Resource進行Dao[ Impl 中的 Dao (引數) ]的注入

程式碼如下:【把Dao的show方法 注入到 Service的方法中 呼叫 Service的Dao 然後帶動 呼叫Dao的show方法】

TestDao.java 和 TestService.java  :

package com.bihu.TestDao;

public interface Dao{
    public void show();
}
package com.bihu.TestService;

public interface Service {
    public void show();
}

TestDaoImpl:

package com.bihu.TestDaoImpl;

import com.bihu.TestDao.Dao;
import org.springframework.stereotype.Repository;

//<bean id="DaoImpl" class="com.bihu.TestDaoImpl.DaoImpl"></bean>
@Repository("DaoImpl")  //在DaoImpl中寫入Bean例項化註解
public class DaoImpl implements Dao {
    @Override
    public void show() {
        System.out.println("我是DaoImpl 中的 show 方法");
    }
}

TestServiceImpl:

package com.bihu.TestServiceImpl;

import com.bihu.TestDao.Dao;
import com.bihu.TestService.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import javax.annotation.Resource;

//<bean id="ServiceImpl" class="com.bihu.TestServiceImpl.ServiceImpl">
@org.springframework.stereotype.Service("ServiceImpl")   //由於名字衝突 其實 是@Service()
public class ServiceImpl implements Service {
    private Dao dao;
///////////////////////////////////////////////////////////////////////////
// Spring配置:   <property name="dao" ref="DaoImpl"></property>

    /*  // 註解: 也可以這樣:
    @Autowired  //使用在欄位上用於根據型別自動依賴注入
@Qualifier("DaoImpl")   //結合@Autowired一起使用用於根據名稱進行依賴注入
*/
    @Resource(name = "DaoImpl") //相當於@Autowired+@Qualifier,按照名稱進行注入
    public void setDao(Dao dao) {
        this.dao = dao;
    }
////////////////////////////////////////////////////////////////////////////////////


    @Override
    public void show() {
        this.dao.show();
    }

}

Spring 配置檔案:

<?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掃描com.bihu下全部的註解 然後解析 -->
        <context:component-scan base-package="com.bihu"></context:component-scan>

</beans>

Demo.java : 【測試檔案】

import com.bihu.TestServiceImpl.ServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Demo {
    @Test
    public void test(){
        //配置檔名叫 app
        ApplicationContext app = new ClassPathXmlApplicationContext("app.xml");
        ServiceImpl service = (ServiceImpl) app.getBean("ServiceImpl");
        service.show();
    }
}

輸出:

資訊: Loading XML bean definitions from class path resource [app.xml]
我是DaoImpl 中的 show 方法

所以呢 這就是用 最原始的註解去進行 例項化Bean 和 注入引數

還有就是儘量可以用一個的註解就用一個 ,貪圖方便是我們程式設計師✖!!! 是我最愛的!