SPRING-HELLOWORLD 改写为构造注入
2012-03-19 11:40
453 查看
1.helloworld类增加构造方法
2.改写配置文件config.xml
3.测试程序testhelloworld.java
1.增加构造方法,MSG作为参数
2.config.xml
3.测试程序
输出:
nihao
2.改写配置文件config.xml
3.测试程序testhelloworld.java
1.增加构造方法,MSG作为参数
package com.gc.action; public class HelloWorld { public String msg=null;//该变量用来存储字符串 //增加了一个构造方法 public HelloWorld(String msg) { this.msg=msg; } //设定变量msg的set方法 public void setMsg(String msg) { this.msg=msg; } //获取变量msg的get方法 public String getMsg() { return this.msg; } }
2.config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!--定义一个Bean--> <bean id="HelloWorld" class="com.gc.action.HelloWorld"> <!--通过构造函数进行注入--> <constructor-arg index="0"> <value>nihao</value> </constructor-arg> </bean> </beans>
3.测试程序
package com.gc.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import com.gc.action.HelloWorld; public class TestHelloWorld { public static void main(String[] args) { //通过ApplicationContext来获取Spring文件的配置 ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml"); //通过Bean的id来获取Bean HelloWorld HelloWorld=(HelloWorld)actx.getBean("HelloWorld"); //打印输出 System.out.println(HelloWorld.getMsg()); } }
输出:
nihao
相关文章推荐
- Spring 将HelloWorld实例改为构造方式注入
- Spring基于xml的构造方法注入&amp;amp;setter注入
- Spring - 基于构造方法的依赖注入
- Spring(8):构造方法注入与示例
- spring注入方式 (Set注入 构造方法注入)
- spring基于构造方法的依赖注入
- Spring通过构造方法注入的四种方式
- 【Spring学习笔记-2.1】Spring的设值注入和构造注入
- Spring构造注入
- Spring基础:快速入门spring(3):构造注入
- spring中的二种注入方式设值注入和构造注入简单例子
- Spring:设值注入、构造注入及命名空间注入
- Spring 构造注入 传参数
- Spring属性注入、构造方法注入、工厂注入以及注入参数(转)
- spring开发_spring构造注入Bean
- 了解Spring 构造注入和自动注入
- Spring的注入方式:设值注入和构造注入
- spring xml 配置 属性注入 构造方法注入
- [Java] Spring3.0 setter 注入 / 留印象构造方法注入 7、8、9.avi
- spring 利用构造方法进行以来性注入