spring註解基於Annotation的依賴注入配置筆記

spring整個的處理操作過程之中,實際上沒有必要過於擔心applicationContext.xml配置複雜,因爲實際開發之中,這些配置不會非常的複雜,而且Spring框架的設計的好處是可以幫助用戶更簡化的進行代碼的編寫,在之前發現,Spring對於所有類的操作管理實際上都是基於配置文件完成的,那麼大家思考一下,假設說現在做一個業務層,再做一個數據層,那麼很明顯業務層需要去調用數據層,如果都將所有的配置寫在了applicationContext.xml文件裏面,一個大型的項目會有幾百個DAO實現類,也會有幾百個業務實現類,那麼這個文件就別看了。所以在Spring裏面針對於這些Bean定義以及關係的引用提供了一組Annotation,並且提供了自動注入操作配置。爲了能夠更加顯示出本配置的優點,所以建議將項目迴歸到最初狀態。

配置註解

在整個的Spring裏面提供有如下的幾種註解(除了名字不一樣之外,都一樣):
· 【數據層】倉庫配置類:@Repository
                (org.springframework.stereotype.Repository)
· 【業務層】業務配置類:@Service
                (org.springframework.stereotype.Service)
· 【工具組件】工具類配置:@Component
                (org.springframework.stereotype.Component)
· 【控制層】控制層配置:@Controller
                (org.springframework.stereotype.Controller)

簡單實例

1、定義一個接口


public interface PersonDao {

    public void savePerson( );

}

2、定義一個接口實現類

import org.springframework.stereotype.Repository;


@Repository("personDao")
public class PersonDaoImpl implements PersonDao {

    @Override
    public void savePerson() {
        // TODO Auto-generated method stub
        System.out.println("saveperson");
    }



}

3、定義一個切面類


import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component("transaction")
@Aspect
public class Transaction {
    @Pointcut("execution(* com.zhiyou.dao.impl.*. *(..))")
    private void aa(){}  //方法簽名

    @Before("aa()")
    public void beginTransaction(){
        System.out.println("begin transaction");
    }

    @AfterReturning("aa()")
    public void commit(){
        System.out.println("commit");
    }

}

4、填寫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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
 <context:annotation-config/>
 <context:component-scan base-package="com.zhiyou"></context:component-scan>
 <aop:aspectj-autoproxy></aop:aspectj-autoproxy>



</beans>  

5、寫一個測試類

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zhiyou.dao.impl.PersonDao;

public class PersonTest {
    private static final String Person = null;

    @Test
    public void test1()
    {

        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");

        PersonDao personDao=(PersonDao)context.getBean("personDao");
        personDao.savePerson();
        System.out.println("sdf");
    }


}

6、運行結果
2017-08-16 21:46:29,047 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@233c0b17: startup date [Wed Aug 16 21:46:29 CST 2017]; root of context hierarchy
2017-08-16 21:46:29,237 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [applicationContext.xml]
2017-08-16 21:46:29,795 INFO [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] - JSR-330 ‘javax.inject.Inject’ annotation found and supported for autowiring
begin transaction
saveperson
commit
sdf

發佈了28 篇原創文章 · 獲贊 6 · 訪問量 9630
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章