1. 程式人生 > >spring 框架中的依賴注入(IOC--設值注入)--使用註解--的具體例項的簡單實現

spring 框架中的依賴注入(IOC--設值注入)--使用註解--的具體例項的簡單實現

體現了具體專案工程裡面的分層,dao,daoImpl,service,serviceImpl,action。讓你真正的理解這為啥分層。
順便清清楚楚的理解@Component、@Service、@Repository 和 @Controller之間的關係。

順便還可以學習下,什麼是依賴注入--DI,什麼是IOC--控制反轉。


Spring 2.5引入了更多典型化註解(stereotype annotations): @Component、@Service和 @Controller。 
@Component是所有受Spring管理元件的通用形式;
而@Repository、@Service和 @Controller則是@Component的細化, 用來表示更具體的用例(例如,分別對應了持久化層、服務層和表現層)。
也就是說,
你能用@Component來註解你的元件類, 但如果用@Repository、@Service 或@Controller來註解它們,
你的類也許能更好地被工具處理,或與切面進行關聯。
(切面暫且不管,後話啦)


分幾個檔案,如下:

先是各個元件,dao,daoImpl,service,serviceImpl,action。

1.PersonDao

package lxk.test.spring.mvc.annotation;

interface PersonDao {
    void savePerson();

    void updatePerson();
}

2.PersonDaoImpl

package lxk.test.spring.mvc.annotation;

import org.springframework.stereotype.Repository;

/**
 * 經掃描注入容器之後,相當於在容器中聲明瞭如下這麼個bean
 * <bean id="personDaoImpl" class="...PersonDaoImpl">
 */
@Repository("personDao")
public class PersonDaoImpl implements PersonDao {

    public void savePerson() {
        System.out.println("save person");
    }

    public void updatePerson() {
        System.out.println("update person");
    }

}
3.PersonService
package lxk.test.spring.mvc.annotation;

interface PersonService {
    void savePerson();

    void updatePerson();
}
4.PersonServiceImpl
package lxk.test.spring.mvc.annotation;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class PersonServiceImpl implements PersonService {

    //此屬性,沒有對應的getter和setter,而是有spring容器負責給這個屬性設值。
    //一般情況下,要用這個屬性,得自己設定,現在容器幫我們幹了。
    // 這就是傳說中的----控制反轉(IOC)----哦,依賴注入的縮寫是 DI
    @Resource(name = "personDao")
    private PersonDao personDao;
    //上面的@Resource後面帶括號,就要靠id(即bean的id=personDao)去容器中找到對應的bean。
    //在容器中找到PersonDaoImpl(PersonDao介面類未帶註解,所以容器不管他),因為他帶有註解(容器會管他的)的一個bean

    public void savePerson() {
        this.personDao.savePerson();
    }

    public void updatePerson() {
        this.personDao.updatePerson();
    }

}
5.PersonAction
package lxk.test.spring.mvc.annotation;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

@Controller
public class PersonAction {
    //此屬性,沒有對應的getter和setter,而是有spring容器負責給這個屬性設值。
    //一般情況下,要用這個屬性,得自己設定,現在容器幫我們幹了。
    // 這就是傳說中的----控制反轉----
    @Resource
    private PersonService personService;
    //上面的@Resource後面沒帶括號,就要靠型別去容器中找到對應的bean。
    //在容器中找到型別是PersonService(此型別有2個:service介面和其實現類)的帶有註解(service實現類)的一個bean

    public void savePerson() {
        this.personService.savePerson();
    }

    public void updatePerson() {
        this.personService.updatePerson();
    }
}
6.main
package lxk.test.spring.mvc.annotation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class MainTest {
    public static void main(String[] args) {
        //ApplicationContext context = new ClassPathXmlApplicationContext("file:E:/fusion/intellij_work/TrunkNew/src/main/java/lxk/test/spring/mvc/annotation/applicationContext.xml");
        //ApplicationContext context = new FileSystemXmlApplicationContext("file:E:/fusion/intellij_work/TrunkNew/src/main/java/lxk/test/spring/mvc/annotation/applicationContext.xml");
        ApplicationContext context = new FileSystemXmlApplicationContext("src/main/java/lxk/test/spring/mvc/annotation/applicationContext.xml");
        PersonAction personAction = (PersonAction) context.getBean("personAction");
        personAction.updatePerson();
    }
}
7.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-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:component-scan base-package="lxk.test.spring.mvc.annotation"/>
</beans>

測試執行結果圖:



具體檔案的位置擺放:


注意:發現上面的註解有的帶括號啦,有的沒帶,主要是為了學習用,可以很好的理解註解的作用,實際開發中,每個註解上面最好都得帶上括號中的內容。以防發生意外的錯誤,等等。