Spring:IOC—控制反转(3)
来源:互联网 发布:怎么网络推广 编辑:程序博客网 时间:2024/04/30 12:14
IOC创建对象的三种方法
第二种:通过有参的构造方法创建对象
Hello.java
package com.et.bean;public class Hello { private String name; //带参数的构造方法 public Hello(String name){ super(); this.name=name; } public void show(){ System.out.println("hello,"+name); }}
Test.java基本不变
package com.et.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.et.bean.Hello;public class Test { public static void main(String[] args) { ApplicationContext act=new ClassPathXmlApplicationContext("beans.xml"); Hello hello=(Hello)act.getBean("hello"); hello.show(); }}
有参数的构造方法创建对象又可以分为三种:
bean.xml
1.根据构造方法参数下标
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="hello" class="com.et.bean.Hello"> <!-- 根据构造方法参数下标:参数下标从0开始 --> <constructor-arg index="0" value="elliott"/> </bean></beans>
2.根据参数名称来设置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="hello" class="com.et.bean.Hello"> <!-- 根据参数名称 --> <constructor-arg name="name" value="elliott"/> </bean></beans>
3.根据参数类型来设置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="hello" class="com.et.bean.Hello"> <!-- 根据参数类型 --> <constructor-arg type="java.lang.String" value="elliott"></constructor-arg> </bean></beans>
输出结果都为:
hello,elliott
阅读全文
0 0
- Spring:IOC—控制反转(3)
- Spring:IOC—控制反转(1)
- Spring:IOC—控制反转(2)
- Spring:IOC—控制反转(4)
- Spring IoC(控制反转)
- Spring IoC(控制反转)
- Spring IOC 反转控制
- Spring-----> IOC(控制反转)
- Spring - 控制反转IOC
- Spring IoC[控制反转]
- Spring控制反转IOC
- Spring IOC控制反转
- Spring IOC 控制反转
- spring ioc 控制反转
- Spring(3):IOC--“控制反转”的剖析
- Spring 框架学习—控制反转(IOC)
- Spring 框架学习—控制反转(IOC)
- Spring 框架学习—控制反转(IOC)
- Java编程之反射中的注解详解
- Caused by: org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token found char
- Use powershell config network
- SQLyog 激活 注册码
- ORCLE 查询一段时间(时间点不定,时差确定)内符合一定条件的数据
- Spring:IOC—控制反转(3)
- MFC自定义控件
- 解决:ERROR: Exception when publishing, exception message [Exec exit status not zero. Status [-1]]
- 卷积神经网络CNN知识点
- 解决solr4.10.3在solrhome下添加多个solrcore遇到的问题总结
- XML学习---利用Java对XML文档的增、删、改、查
- Android.mk介绍(一)
- select onChange事件
- 你值得了解的 10 个有趣的 Linux 命令行小技巧