三种不同方式的构造器注入
2013-04-02 14:51
633 查看
1 java类
2 配置文件init_bean.xml
3测试代码
public class HelloImpl implements IHello { private String message; private int index; public HelloImpl(String message, int index) { this.message = message; this.index = index; } public HelloImpl(){} @Override public void sayHello() { System.out.println(this.index + ":" + message); } }
2 配置文件init_bean.xml
<!--通过构造器参数索引方式依赖注入 --> <bean id="byIndex" class="com.pyy.HelloImpl"> <constructor-arg index="0" value="Hello Spring"/> <constructor-arg index="1" value="1"/> </bean> <!--通过构造器参数类型方式依赖注入 --> <bean id="byType" class="com.pyy.HelloImpl"> <constructor-arg type="java.lang.String" value="Hello Spring"/> <constructor-arg type="int" value="2"/> </bean> <!--通过构造器参数名字方式依赖注入 --> <bean id="byName" class="com.pyy.HelloImpl"> <constructor-arg name="message" value="Hello Spring"/> <constructor-arg name="index" value="3"/> </bean>
3测试代码
@Test public void testConstruct(){ ApplicationContext beans = new ClassPathXmlApplicationContext( "/config/init_bean.xml"); HelloImpl bean1 = beans.getBean("byIndex", HelloImpl.class); bean1.sayHello(); HelloImpl bean2 = beans.getBean("byType", HelloImpl.class); bean2.sayHello(); HelloImpl bean3 = beans.getBean("byName", HelloImpl.class); bean3.sayHello(); }
相关文章推荐
- Spring实现控制反转(IOC)的三种方式(二)——构造器注入
- Spring IOC三种注入方式(接口注入、setter注入、构造器注入)
- Spring IOC三种注入方式(接口注入、setter注入、构造器注入)
- Spring IOC三种注入方式(接口注入、setter注入、构造器注入)
- Spring IOC三种注入方式(接口注入、setter注入、构造器注入)
- Spring IOC三种注入方式(接口注入、setter注入、构造器注入)
- Spring IOC三种注入方式(接口注入、setter注入、构造器注入)(摘抄)
- Spring IOC三种注入方式(接口注入、setter注入、构造器注入)
- Spring静态注入的三种方式
- Spring依赖注入的三种方式
- Spring 框架中 Setter 注入 和 构造器注入 方式的区别 与优劣
- 用三种不同的方式实现TabHost功能(二)
- jdbc mysql数据库链接 及 三种不同的注册驱动的方式
- Spring IOC 三种注入方式
- 简单的小程序演示Unity的三种依赖注入方式
- Spring学习(十八)Bean 的三种依赖注入方式介绍
- PHP传值到不同页面的三种常见方式及php和html之间传值问题
- Hive三种不同的数据导出的方式
- 以下三种下载方式有什么不同?如何用python模拟下载器下载?
- iOS开发基础之用三种不同方式创建Table View应用