spring 疑问汇总

1. 关于constructor-arg:

【xml】
<bean name="conService" class="io.lin.common.spring.conarg.ConstructService">
    <constructor-arg name="name" value="aaaa"></constructor-arg>
    <constructor-arg name="num" value="12"></constructor-arg>
    <constructor-arg name="bool" value="true"></constructor-arg>
</bean>
<bean name="prototypeConService" class="io.lin.common.spring.conarg.ConstructService" scope="prototype">
    <constructor-arg name="name" value="aaaa"></constructor-arg>
    <constructor-arg name="bool" value="true"></constructor-arg>
    <constructor-arg name="num" value="12"></constructor-arg>
</bean>
<bean name="lazyConService" class="io.lin.common.spring.conarg.ConstructService" lazy-init="true">
    <constructor-arg name="name" value="aaaa"></constructor-arg>
    <constructor-arg name="num" value="12"></constructor-arg>
    <constructor-arg name="bool" value="true"></constructor-arg>
</bean>


【java】

//test default
ConstructService conService = (ConstructService) context.getBean("conService",new String("xxxx"),new Boolean(true),new Integer(1));
String result = conService.toString();
Assert.assertEquals("aaaa true 12", result);



//test prototype
ConstructService prototypeConService1= (ConstructService) context.getBean("prototypeConService");
String result1 = prototypeConService1.toString();
Assert.assertEquals("aaaa true 12", result1);

ConstructService prototypeConService2 = (ConstructService) context.getBean("prototypeConService",new String("xxxxxxxxx"),new Boolean(true),new Integer(1));
String result2 = prototypeConService2.toString();
Assert.assertEquals("xxxxxxxxx true 1", result2);


//test singleton & lazy-init
ConstructService lazyConService = (ConstructService) context.getBean("lazyConService",new String("rrrr"),new Boolean(true),new Integer(1));
Assert.assertEquals("rrrr true 1", lazyConService.toString());

ConstructService lazyConService2 = (ConstructService) context.getBean("lazyConService");
Assert.assertEquals("rrrr true 1", lazyConService2.toString());
 



2.context:component-scan
2.1)bean已经写在xml里面,但base-package路径又重复包含该bean。xml重复定义的bean在scan的时候跳过。
2.2)base-package路径及其子路径都会被scan

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值