建立一个Hello World级别的Spring项目
2016-03-31 16:44
399 查看
package com.sevenhu.domain; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by hu on 2016/3/31. */ public class HelloWorld { private String userName; //Spring项目中,配置在容器中的类,其属性都必须有setter方法 public void setUserName(String userName) { this.userName=userName; } public void hello(){ System.out.println("Hello: "+userName); } public static void main(String[] args){ //1.创建Spring的IOC容器 ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml"); //2.从容器中获取bean HelloWorld helloWorld= (HelloWorld) applicationContext.getBean("helloWorld"); System.out.println(helloWorld); //3.调用方法 helloWorld.hello(); } }
Spring的配置文件的代码如下:
<?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="helloWorld" class="com.sevenhu.domain.HelloWorld"> <property name="userName" value="Spring"></property> </bean> </beans>
相关文章推荐
- 【Java】 JTextField文本框实时监控
- 常用DOS命令
- Spring Bean 创建过程
- Java解决火狐浏览器使用uploadify上传报错302以及报IO error错问题
- Java中的wait()方法与notify(),notifyAll()方法
- Web必知必会Cookie与Session
- Java多线程模拟实现消费者生产者问题
- Java的动态代理
- [Java]数据存储(栈,堆,常量池)
- 【eclipse】No enclosing instance of type A is accessible. Must qualify the allocation with an enclosing instance of type A
- SpringWeb ContextLoaderListener 初始化过程
- 认识java中的堆和栈
- SpringMVC DispatcherServlet 初始化过程
- java后台通过url获取返回数据
- java#2servlet
- Java 四种线程池的用法分析
- Struts2 传递参数map对象
- 华为上机题-二师弟的星际加法
- JavaEE_3_30_servlet详细介绍
- Swagger与SpringMVC项目整合