Spring 裝配

有如下三種配置
在xml中配置 在java中配置 bean自動裝配java

自動裝配

這裏以轉載CD爲例子spring

首先須要創建CD概念bash

即,定義一個cd接口app

只須要實現添加兩個註解,ide

package com.ming;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * 定義cd接口
 * @author ming
 */
@Configuration
@ComponentScan
public interface CompactDisc {
    /**
     * 規定方法爲play
     */
    void play();
}

複製代碼
package com.ming;

import org.springframework.stereotype.Component;

/**
 *
 * @author ming
 */

// 啓用組件掃描
@Component
class SgtPeppers implements CompactDisc {
    private String title = "標題";
    private String artist = "內容";
    /**
     * 規定方法爲play
     */
    @Override
    public void play() {
        System.out.println(this.artist + this.title);
    }
}

複製代碼
package com.ming;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import static org.junit.Assert.*;

public class CompactDiscTest {
    private ApplicationContext applicationContext = null;

    @Before
    public void setUp() throws Exception {
        applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void play() {
        CompactDisc compactDisc = applicationContext.getBean(CompactDisc.class);
        assertNotNull(compactDisc);
    }
}
複製代碼

掃描的時候,會直接按照該類所在的包,做爲基礎包,進行掃描ui

對依賴的實現自動裝配

添加註解之後,初始化bean之後,會盡量的知足bean的依賴this

目錄以下spa

2019-05-21-01-50-05----

package com.ming;

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

/**
 *
 * @author ming
 */
@Component
public class CDPlay implements MediaPlayer {
    CompactDisc compactDisc = null;

    @Autowired
    public CDPlay(CompactDisc compactDisc){
        this.compactDisc = compactDisc;
    }

    /**
     * 規定方法爲play
     */
    @Override
    public void play() {
        compactDisc.play();
    }
}

複製代碼
package com.ming;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * 定義cd接口
 * @author ming
 */
@Configuration
@ComponentScan
public interface CompactDisc {
    /**
     * 規定方法爲play
     */
    void play();
}

複製代碼
package com.ming;

import org.springframework.context.annotation.ComponentScan;

/**
 * @author ming
 */
@ComponentScan
public interface MediaPlayer {
    /**
     * 規定方法爲play
     */
    public void play();
}

複製代碼
package com.ming;

import org.springframework.stereotype.Component;

/**
 *
 * @author ming
 */

// 啓用組件掃描
@Component
class SgtPeppers implements CompactDisc {
    private String title = "標題";
    private String artist = "內容";
    /**
     * 規定方法爲play
     */
    @Override
    public void play() {
        System.out.println(this.artist + this.title);
    }
}

複製代碼
<?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">
	<context:component-scan base-package="com.ming"/>
</beans>
複製代碼
package com.ming;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import static org.junit.Assert.*;

public class CompactDiscTest {
    private ApplicationContext applicationContext = null;

    @Before
    public void setUp() throws Exception {
        applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    }

    @After
    public void tearDown() throws Exception {

    }

    @Test
    public void play() {
        MediaPlayer mediaPlayer = (MediaPlayer) applicationContext.getBean(MediaPlayer.class);
        mediaPlayer.play();
        assertNotNull(mediaPlayer);
    }
}
複製代碼

運行結果code

2019-05-21-01-51-54----

在這裏,在裝配的時候,儘可能知足配置規則進行裝配component

Java代碼裝配bean

2019-05-21-02-18-11----

package com.ming;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 *
 * @author ming
 */
@Configuration  // 註解代表這是一個配置類
public class CDPlayerConfig {

    @Bean
    public CompactDisc sgtPeppers(){
        // 此處進行裝配
        return new SgtPeppers();
    }

    /**
     * 注入裝配
     * @return
     */
    @Bean
    public CDPlay cdPlay(){
        return new CDPlay(this.sgtPeppers());
    }
}

複製代碼
相關文章
相關標籤/搜索