转自:https://www.cnblogs.com/JimKing/p/8811119.html#s103
1.Spring提供了三种装配bean的方式:
2.自动装配bean:
3.通过Java代码装配bean
4.通过XML装配bean
前言:创建对象的协作关系称为装配,也就是DI(依赖注入)的本质。而在Spring容器中对bean的创建时就需要对它所依赖的对象进行注入装配。
1.Spring提供了三种装配bean的方式
注:三种装配方式可以结合使用,但是推荐首选第3种自动装配,之后选用Java进行装配,最后选用XML进行装配。
2.自动装配bean
自动装配优势:
自动装配限制:
Spring实现自动装配两个步骤:
使用到的注解:
代码实现:
播放器接口:
public interface CDPlayer {// 播放器 void play(); }
唱片接口:
public interface CDDisk {// 唱片 void sing(); }
实现播放器接口的类:
import org.springframework.stereotype.Component; @Component public class MediaPlayer implements CDPlayer { private CDDisk cd; @Autowired public MediaPlayer(CDDisk cd){ this.cd = cd; } @Override public void play() { cd.sing(); } }
实现唱片接口的类:
import org.springframework.stereotype.Component; @Component public class HuaHua implements CDDisk { private String title = "烟火里的尘埃"; private String singer = "华晨宇"; @Override public void sing() { System.out.println(title + "_" + singer); } }
Java配置类:
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan public class CDPlayerConfig { }
测试类:
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; // SpringJUnit4ClassRunner测试用,测试开始的时候自动创建Spring的应用上下文。 @RunWith(SpringJUnit4ClassRunner.class) // 测试用,去指定类加载配置 @ContextConfiguration(classes = CDPlayerConfig.class) public class CDTest { @Autowired private CDPlayer cdPlayer; @Test public void test() { cdPlayer.play(); } }
测试结果:
3.通过Java代码装配bean
优点:
使用到的注解:
代码实现:
播放器接口与唱片接口:
public interface CDPlayer {// 播放器 void play(); } public interface CDDisk {// 唱片 void sing(); }
实现播放器的类:
public class MediaPlayer implements CDPlayer { private CDDisk cd; @Autowired public MediaPlayer(CDDisk cd){ this.cd = cd; } @Override public void play() { cd.sing(); } }
实现唱片的类:
import java.util.List; public class HuaHua implements CDDisk { private String title; private String singer; private Listtracks; // 注入字面量 public HuaHua(String title, String singer, List tracks) { this.title = title; this.singer = singer; this.tracks = tracks; } @Override public void sing() { System.out.println(title + "_" + singer); for (String track : tracks) { System.out.println(track); } } }
总配置类(将指定的配置类组合到一起):
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @Configuration // 将指定配置类组合到一起 @Import({CDPlayerConfig.class, CDDiskConfig.class}) public class SoundSystemConfig { }
播放器的配置类:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class CDPlayerConfig { private CDDisk cd; @Bean public CDPlayer cdPlayerConfig(CDDisk cd) { return new MediaPlayer(cd); } }
唱片配置类:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.ArrayList; @Configuration public class CDDiskConfig { @Bean public CDDisk cdDisk() { return new HuaHua("专辑", "华晨宇", new ArrayList() {{ add("烟火里的尘埃"); add("国王与乞丐"); add("齐天"); add("我的滑板鞋2016"); }}); } }
测试类:
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; // SpringJUnit4ClassRunner测试用,测试开始的时候自动创建Spring的应用上下文。 @RunWith(SpringJUnit4ClassRunner.class) // 测试用,去指定类加载配置 @ContextConfiguration(classes = SoundSystemConfig.class) public class CDTest { @Autowired private CDPlayer cd; @Test public void test() { cd.play(); } }
测试结果:
4.通过XML装配bean
优点:什么都能做。
缺点:配置繁琐。
使用到的标签:
代码实现(将Java配置改为了XML配置):
播放器接口与唱片接口:
public interface CDPlayer {// 播放器 void play(); } public interface CDDisk {// 唱片 void sing(); }
实现播放器的类:
public class MediaPlayer implements CDPlayer { private CDDisk cd; public MediaPlayer(CDDisk cd){ this.cd = cd; } @Override public void play() { cd.sing(); } }
实现唱片的类:
import java.util.List; public class HuaHua implements CDDisk { private String title; private String singer; private Listtracks; // 注入字面量 public HuaHua(String title, String singer, List tracks) { this.title = title; this.singer = singer; this.tracks = tracks; } @Override public void sing() { System.out.println(title + "_" + singer); for (String track : tracks) { System.out.println(track); } } }
测试类:
import org.springframework.context.support.ClassPathXmlApplicationContext; public class CDTest { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("soundsystem.xml"); CDPlayer cd = context.getBean(CDPlayer.class); cd.play(); } }
总XML配置(将播放器的XML配置引入):
播放器的XML配置(将唱片的XML配置引入):
唱片的XML配置:
烟火里的尘埃 国王与乞丐 齐天 我的滑板鞋2016