spring 第一个程序 hello
来源:互联网 发布:uml类图转化为java代码 编辑:程序博客网 时间:2024/05/18 01:47
public class HelloWord {
private String userName;
public void setUserName(String userName) {
this.userName = userName;
}
public void hello(){
System.out.println("hello:"+userName);
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
/**
* 创建对象及为对象赋值交给spring完成
*/
// HelloWord helloWord = new HelloWord();
// helloWord.setUserName("hello");
// helloWord.hello();
//1.创建spring IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//2从容器中获得Bean
HelloWord helloWord = (HelloWord) ctx.getBean("helloWord");
//3.调用方法
helloWord.hello();
}
}
配置文件为:
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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 -->
<bean id="helloWord" class="com.spring.HelloWord">
<property name="userName" value="springsss"></property>
</bean>
</beans>
输出结果为:
hello:springsss
- spring 第一个程序 hello
- spring开发第一个hello world程序
- 第一个hello程序
- 【有配图】Spring Boot 第一个Hello World 程序
- Spring MVC框架下的第一个Hello World程序
- 第一个“Hello World”程序
- 第一个程序:"Hello World"
- 第一个程序:Hello World
- 第一个程序:Hello World
- Hello World 第一个程序
- 第一个程序“Hello World !”
- 第一个Spring程序
- Spring第一个程序
- 第一个spring程序
- 第一个spring程序
- 第一个Spring程序
- Spring第一个程序
- Spring 第一个程序
- 使用git时避免每次提交输入密码
- 不同浏览器Ext.Window父窗体获取子窗体信息值
- Perl寻找去除数组中重复元素
- 题目:两数之和
- Linux查看设置系统时区
- spring 第一个程序 hello
- hdoj 4875 逃生
- 如何在 Java 中正确使用 wait, notify 和 notifyAll – 以生产者消费者模型为例
- 1011 最大连续子序列问题
- java开发博客
- C++运算符重载
- 安卓两个Activity切换时Activity的生命周期
- epoll的两种触发模式ET和LT
- ARM内核全解析,从ARM7,ARM9到Cortex-A7,A8....Cortex-A53,A57,A72