Spring 装配

栏目: Java · 发布时间: 6年前

内容简介:有以下三种配置在xml中配置在java中配置

有以下三种配置

在xml中配置

java 中配置

bean自动装配

自动装配

这里以转载CD为例子

首先需要建立CD概念

即,定义一个cd接口

只需要实现添加两个注解,

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

扫描的时候,会直接按照该类所在的包,作为基础包,进行扫描

对依赖的实现自动装配

添加注解以后,初始化bean以后,会尽可能的满足bean的依赖

目录如下

Spring 装配

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

运行结果

Spring 装配

在这里,在装配的时候,尽量满足配置规则进行装配

Java代码装配bean

Spring 装配

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

MATLAB数值计算

MATLAB数值计算

莫勒 / 喻文健 / 机械工业出版社 / 2006-6 / 35.00元

《MATLAB数值计算》是关于数值方法、MATLAB软件和工程计算的教材,着重介绍数学软件的熟练使用及其内在的高效率算法。主要内容包括:MATLAB介绍、线性方程组、插值、方程求根、最小二乘法、数值积分、常微分方程、傅里叶分析、随机数、特征值与奇异值、偏微分方程。《MATLAB数值计算》配备大量MATLAB例子源代码及习题,其中涉及密码学、Google网页分级、大气科学和图像处理等前沿问题,可以帮......一起来看看 《MATLAB数值计算》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具