Spring依賴注入

咱們常常見到的控制翻轉(Inversion of Control-IOC) 和依賴注入(dependency injection-DI),在Spring 環境下等同的概念,控制翻轉是經過依賴注入實現的。所謂依賴注入指的是容器負責建立對象和維護對象間的依賴關係,而不是經過對象自己負責本身的建立和解決本身的依賴。java

Spring IOC容器(ApplicationContext)負責建立Bean ,並經過容器將功能類Bean注入到你須要的Bean中。Spring框架提供使用Xml、註解、java配置、groovy配置實現Bean 的建立和注入。spring

不管是 Xml 配置、註解配置仍是 java 配置,都被稱爲元數據,所謂元數據即描述數據的數據。元數據自己不具有任何可執行能力,只能經過外界代碼來對這些元數據行解析後進行一些有意義的操做。Spring容器解析這些配置的元數據進行Bean初始化、配置和管理依賴。編程

聲明Bean的註解:app

@Component 沒有明確的角色
@Service 在業務邏輯層(service層)使用
@Repository 在數據訪問層(dao層)使用
@Controller 在表現層(MVC-->SpringMVC)使用
注入Bean的註解,通常狀況下通用框架

@Autowired Spring提供的註解
@Inject JSR-330提供的註釋
@Resource JSR-250提供的註釋
@Autowired 、@Inject 、@Resource可註解在set方法上或者屬性上,建議註解在屬性上,有點是代碼更少、層次更清晰。
通常注入規則:學習

從邏輯圖中咱們能夠看出,Spring容器包含了Model層、Service層、Dao層 。 他們每個都是Spring容器管理的一個實體類。能夠經過Spring容器將一個實體類須要的另外實體類傳給它。 而不採用在實體類中經過new的方式來獲取實體類,由於new的方式不方便咱們的使用。咱們經過注入的方式,傳給他須要的實體類,也就是一個實體類須要什麼對象,只要被須要的對象在Spring容器管理下,我就給它什麼對象,再也不須要經過new的形式獲得他,這樣在實體類中咱們就能夠直接使用。測試

依賴注入:①註解注入this

[java] view plain copy設計

(model層)
package com.dependencyInjection_Di;
code

import org.springframework.stereotype.Component;

@Component //聲明Model是Spring管理的一個bean
public class Model {

public String ModelName;

}

(service層)

package com.dependencyInjection_Di;

import com.dependencyInjection_Di.ModelMapper;

import com.dependencyInjection_Di.Model;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

@Service(value = "modelService") //聲明ModelService是Spring管理的一個bean

public class ModelService {

@Autowired



ModelMapper modelMapper;



@Autowired



Model model;



public void SayHello(){



    System.out.println("進入Service層");



    model.ModelName="測試完畢";



    modelMapper.SayHello(model);



}

}

(dao層)

package com.dependencyInjection_Di;

import org.springframework.stereotype.Repository;

@Repository

public class ModelMapper {

public void SayHello(Model model){



    System.out.print("數據持久層insert "+"\n"+model.ModelName);



}

}

聲明配置類

package com.dependencyInjection_Di;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

@Configuration //聲明當前類是一個配置類

@ComponentScan("com.dependencyInjection_Di") //使用ComponentScan自動掃描包下全部的@Service、@Component、@Repository、@Controller

public class Diconfig {

}

測試類

package com.dependencyInjection_Di;

import com.dependencyInjection_Di.Diconfig;

import com.dependencyInjection_Di.ModelService;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class RunningTest {

/**



 * 用到jar包



 <dependency>



     <groupId>org.springframework</groupId>



     <artifactId>spring-context</artifactId>



     <version>4.3.12.RELEASE</version>



 </dependency>



 <dependency>



     <groupId>org.springframework</groupId>



     <artifactId>spring-core</artifactId>



     <version>4.3.13.RELEASE</version>



 </dependency>



 * Spring框架自己四大原則



 * ⑴使用POJO進行輕量級和最小侵入式開發



 * ⑵經過依賴注入和基於接口編程實現鬆耦合



 * ⑶經過AOP和默認習慣進行聲明式編程



 * ⑷使用AOP和模板(template)減小模式化代碼



 * Spring全部功能的設計和實現都是基於此四大原則



 * @Configuration 聲明類是配置類



 * @ComponentScan 自動掃描目錄下全部帶有註解的Bean



 * @Service、@Controller、@Component、@Repository  是等效的,能夠根據層面選擇進行標識使用



 */



public static void main(String arg[]){



    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Diconfig.class);



    ModelService modelService = (ModelService) context.getBean("modelService");



    modelService.SayHello();



}

}

輸出信息

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。蠻多信息

信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@63ee4826: startup date [Thu Jan 18 18:14:29 CST 2018]; root of context hierarchy

進入Service層

數據持久層insert

測試完畢

Process finished with exit code 0

依賴注入:②java配置注入

(model層)

package com.JavaDependencyInjection_Di;

public class Model

{

public String modelName;

}

(service層)

package com.JavaDependencyInjection_Di;

import com.JavaDependencyInjection_Di.ModelMapper;

import com.JavaDependencyInjection_Di.Model;

public class ModelService {

Model model;



ModelMapper modelMapper;



public void setModel(Model model){



    this.model=model;



}







public void setModelMapper(ModelMapper modelMapper) {



    this.modelMapper = modelMapper;



}







public void SayHello(){



    System.out.println("service層");



    model.modelName="Hello";



    modelMapper.SayHello(model);



}

}

(dao層)

package com.JavaDependencyInjection_Di;

public class ModelMapper {

public void SayHello(Model model){



    System.out.print("Dao層 \n");



    System.out.print(model.modelName);



    System.out.print("\n 測試完畢!");



}

}

(配置類)

package com.JavaDependencyInjection_Di;

import com.JavaDependencyInjection_Di.ModelMapper;

import com.JavaDependencyInjection_Di.Model;

import com.JavaDependencyInjection_Di.ModelService;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

@Configuration //①

public class JavaConfig {

@Bean      //②



public Model model(){



    return new Model();



}



@Bean



public ModelMapper modelMapper(){



     return new ModelMapper();



}







@Bean







public ModelService modelService(){



    ModelService modelService = new ModelService();



    modelService.setModelMapper(modelMapper());  //③



    modelService.setModel(model());



    return modelService;



}



@Bean



public ModelService modelService(Model model,ModelMapper modelMapper){



    ModelService modelService = new ModelService();



    modelService.setModelMapper(modelMapper); //④



    modelService.setModel(model);



    return modelService;



}



/**



 * ①使用@Configuration 聲明當前類是一個配置類,這覺得着這個類裏可能含有 0 個或者多個 @Bean註解,



 *      此處沒有使用包掃描,是由於全部的 Bean 都在此類中定義了



 * ②使用@Bean聲明當前方法 model() 返回值是一個Bean,Bean名稱是方法名



 * ③注入 ModelMapper 的 Bean 時候直接調用 modelMapper()



 * ④另外一種注入方式,直接將 ModelMapper  做爲參數給 modelService(),這也是Spring容器提供的極好的功能,



 *      在 Spring 容器中,只要容器中存在某個 Bean,就能夠在另外一個 Bean 的聲明方法參數中寫入



 */

}

(測試類)

package com.JavaDependencyInjection_Di;

import com.JavaDependencyInjection_Di.JavaConfig;

import com.JavaDependencyInjection_Di.ModelService;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class RunningTest {

public static void main(String arg[]){



    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class);



    ModelService modelService = context.getBean(ModelService.class);



    modelService.SayHello();



}

}

(運行結果)

一月 18, 2018 6:31:05 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh

信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7956b7c1: startup date [Thu Jan 18 18:31:05 CST 2018]; root of context hierarchy

service層

Dao層

Hello

測試完畢!

Process finished with exit code 0

此乃學習筆記,記錄下來但願以備本身忘記,很差地方,望多加指點。

相關文章
相關標籤/搜索