《Spring in action 4》(二)装配Bean 您所在的位置:网站首页 tostring用法 《Spring in action 4》(二)装配Bean

《Spring in action 4》(二)装配Bean

#《Spring in action 4》(二)装配Bean| 来源: 网络整理| 查看: 265

装配Bean

莫道君行早 更有早行人

文章目录​​装配Bean​​​​Spring装配Bean的三种方式​​​​XML显示配置​​​​Java显示配置​​​​自动发现和装配​​​​属性注入​​​​构造注入​​​​Set注入​​​​set 和 list 的区别​​​​p 和 c-arg 区别​​​​导入和混合配置​​​​XML配置文件相互引用​​​​Java配置类相互引用​​​​Java配置类中引用Xml配置文件​​​​Xml配置文件中引用Java配置类​​Spring装配Bean的三种方式在XML中进行显示配置在Java中进行显示配置隐式的Bean发现和自动装配组件扫描:Spring会自动发现应用上下文所创建的Bean自动装配:Spring自动满足Bean之间的依赖

尽可能地的使用自动配置的机制。显示的配置越少越好,当你必须要显示配置Bean的时候(比如:有些源码不是由你来维护的,而当你需要为这些代码配置的时候),推荐使用类型安全(因为在Java配置中可以利用编译器检查)并且比XML更加强大的JavaConfig。最后,只有当你想要使用便利的XML命名空间,并且在JavaConfig中没有同样的实现时,才应该使用XML。

下面利用一个案例

手机和手机电池是一个依赖关系,在正常情况下,如果手机离开了手机电池,那么其实也不会起到什么作用,故以此为一个案例展开Spring Bean的装配。

手机接口:

/** * 案例分析: * 手机是依赖于手机电池存在的。 */public interface MobilePhone {}

电池接口:

/*电池接口*/public interface Battery {}

手机实现类:

public class HuaweiMobilePhone implements MobilePhone { private Battery battery; public HuaweiMobilePhone(){}

public HuaweiMobilePhone(Battery battery){ this.battery = battery; } @Override public String toString() { return "HuaweiMobilePhone{" + "battery=" + battery + '}'; }}

电池实现类:

public class HuaweiBattery implements Battery{ private String brand; private String name; public HuaweiBattery(){}

public HuaweiBattery(String brand, String name){ this.brand = brand; this.name = name; }

@Override public String toString() { return "HuaweiBattery{" + "brand='" + brand + '\'' + ", name='" + name + '\'' + '}'; }}

XML显示配置

下面展示了基本的Bean注入方式,我们在上一篇其实已经接触到了。

使用Xml方式,测试类:

@Testpublic void testDIXml(){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("phone.xml"); HuaweiMobilePhone bean = context.getBean(HuaweiMobilePhone.class); System.out.println(bean);}Java显示配置

PhoneConfig 配置类:

public class PhoneConfig { @Bean public HuaweiBattery huaweiBattery(){ return new HuaweiBattery(); } @Bean public HuaweiMobilePhone huaweiMobilePhone(){ return new HuaweiMobilePhone(huaweiBattery()); }}

使用注解,测试,如下:

@Testpublic void testAutoWire(){ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(PhoneConfig.class); HuaweiMobilePhone phone = context.getBean(HuaweiMobilePhone.class); System.out.println(phone);}自动发现和装配

​ 使用Bean自动发现和装配的方式,则需要在Bean类上用​​@Component​​​注解进行标识。通过​​@CompentScan(basePackages="")​​​进行组件扫描。使用​​@AutoWired​​注解进行自动装配。

手机实现类:

@Componentpublic class HuaweiBattery implements Battery{ private String brand; private String name; public HuaweiBattery(){}

public HuaweiBattery(String brand, String name){ this.brand = brand; this.name = name; } @Override public String toString() { return "HuaweiBattery{" + "brand='" + brand + '\'' + ", name='" + name + '\'' + '}'; }}

手机电池实现类:

@Componentpublic class HuaweiMobilePhone implements MobilePhone { @Autowired private Battery battery; public HuaweiMobilePhone(){} public HuaweiMobilePhone(Battery battery){ this.battery = battery; }

@Override public String toString() { return "HuaweiMobilePhone{" + "battery=" + battery + '}'; }}

Java配置类:

@ComponentScan(basePackages = "com.ooyhao.spring")public class AutoWirePhoneConfig {}

测试类:

@Testpublic void testAutoWire(){ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AutoWirePhoneConfig.class); HuaweiMobilePhone phone = context.getBean(HuaweiMobilePhone.class); System.out.println(phone);}

总结:

创建应用对象之间的协作关系行为通常称为装配,这也是依赖注入(DI)的本质。 ​​@ComponentScan​​​默认会扫描与配置类相同的包。组件扫描默认是不启动的。​​@ComponentScan​​除了basePackages属性,还有basePackageClasses属性。 ​​@Bean​​ 注解会告诉Spring这个方法将会返回一个对象。默认情况下,bean的ID与带有​​@Bean​​注解的方法名一样。 ​​@component​​ 默认是使用类名首字母小写作为Bean的id。当然也可以用其属性自定义指定。属性注入构造注入

XML配置:

测试实例不变,测试结果:

HuaweiMobilePhone{battery=HuaweiBattery{brand='Huawei', name='华为手机'}}

为了了解更多的类型注入,下面新增耳机接口和实现类,并将手机的实现类进行修改,如下:

耳机接口类:

/*耳机接口类*/public interface EarPhone {}

耳机实现类:

public class HuaweiEarPhone implements EarPhone {

private String color; private Double price;

public HuaweiEarPhone() {}

public HuaweiEarPhone(String color, Double price) { this.color = color; this.price = price; }

@Override public String toString() { return "HuaweiEarPhone{" + "color='" + color + '\'' + ", price=" + price + '}'; }}

手机类:增加手机颜色和耳机

public class HuaweiMobilePhone implements MobilePhone {

private Battery battery; //手机颜色 private List colors; private Set earPhones;

public HuaweiMobilePhone(){}

public HuaweiMobilePhone(Battery battery){ this.battery = battery; }

public HuaweiMobilePhone(List colors) { this.colors = colors; }

public HuaweiMobilePhone(Battery battery, List colors){ this.battery = battery; this.colors = colors; }

public HuaweiMobilePhone(Battery battery,List colors, Set earPhones) { this.battery = battery; this.colors = colors; this.earPhones = earPhones; }

@Override public String toString() { return "HuaweiMobilePhone{" + "battery=" + battery + ", colors=" + colors + ", earPhones=" + earPhones + '}'; }}

修改XML配置文件:

蓝色 红色 黑色

测试结果如下:

HuaweiMobilePhone{battery=HuaweiBattery{brand='Huawei', name='华为手机'}, colors=[蓝色, 红色, 黑色], earPhones=[HuaweiEarPhone{color='白色', price=59.9}, HuaweiEarPhone{color='黑色', price=60.5}, HuaweiEarPhone{color='粉色', price=66.6}, HuaweiEarPhone{color='蓝色', price=88.8}, HuaweiEarPhone{color='绿色', price=90.8}]}Set注入

下面案例展示如何使用Set方法进行属性注入,所以需要将 手机实现类、电池实现类和手机耳机实现类均添加上set方法。(此处不展示了)

Xml配置文件:

蓝色 红色 黑色

测试结果:

HuaweiMobilePhone{battery=HuaweiBattery{brand='Huawei', name='华为手机'}, colors=[蓝色, 红色, 黑色], earPhones=[HuaweiEarPhone{color='白色', price=59.9}, HuaweiEarPhone{color='蓝色', price=88.88}]}

注意:

​ 可以使用​​​​​ 或者 ​​​​ 标签可以将null注入。

set 和 list 的区别

​ ​​​​​ 和 ​​​​​ 元素的区别不大,其中最重要的不同在于当Spring创建要装配的集合时,所创建的时java.util.Set 还是 java.util.List。 如果时Set的话,所有重复值都会被忽略掉,存放顺序也不会得以保证。不过无论在哪种情况下,​​​​​ 和 ​​



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有