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());
    }
}

复制代码
相关文章
相关标签/搜索