【Spring in action】自动化装配Bean、通过Java代码及xml装配Bean

本文详细介绍了Spring框架中Bean的自动化装配,包括简单对象的自动装配、XML配置启用自动扫描、Java配置的自动装配以及通过Java代码和XML装配复杂对象的方法。通过实例展示了如何使用JavaConfig实现注入,以及XML配置中装配注入、属性值注入和集合对象的处理。
摘要由CSDN通过智能技术生成

一、简单对象自动装配Bean

隐式的Bean 发现和自动装配:

CompactDisc

package testJava;

public interface CompactDisc {
	void play();
}

SgtPeppers

package testJava;

import org.springframework.stereotype.Component;

@Component
public class SgtPeppers implements CompactDisc{
	private String title = "七里香";
	private String artist = "周杰伦";
	@Override
	public void play() {
		System.out.println("Playing "+title+" by "+artist);
	}
}
Java中配置

CDPlayerConfig

package testJava;

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

@Configuration
@ComponentScan
public class CDPlayerConfig {

}
单元测试类:
package testJava;

import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=CDPlayerConfig.class)
public class TestJava {
	//简单对象(区别于有依赖的对象)
	@Autowired
	private CompactDisc cd;
	
	@Test
	public void test() {
		//基于注解,自动装配1
		//assertNotNull(cd);
		cd.play();
	}
}
执行结果:

Playing 七里香 by 周杰伦

导包一览如下:


二、简单对象通过xml启用自动扫描如下:

去掉上面代码中的CDPlayerConfig.java类。添加如下applicationContext.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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	<context:component-scan base-package="testXml"></context:component-scan>

</beans>
修改TestJava:
package testXml;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestJava {
	@Autowired
	private CompactDisc cd;
	
	@Test
	public void test() {
		//基于注解,自动装配2
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("testXml/applicationContext.xml");
		cd = ctx.getBean(CompactDisc.class);
		cd.play();
		ctx.close();
	}
}
包结构如下:


执行结果:

Playing 七里香 by 周杰伦


三、复杂对象Java配置自动装配Bean

MediaPlayer

package testComplexJava;

public interface MediaPlayer {
	void play();
}

CDPlayer

package testComplexJava;

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

@Component
public class CDPlayer implements MediaPlayer{
	private CompactDisc cd;
	@Autowired
	public CDPlayer(CompactDisc cd) {
		this.cd = cd;
	}

	@Override
	public void play() {
		cd.play();
	}
}

TestJava

package testComplexJava;

import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfi
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值