首页 > 学院 > 开发设计 > 正文

spring 第一个helloWord程序

2019-11-06 07:26:51
字体:
来源:转载
供稿:网友

main 主程序:

package sPRing;import org.springframework.context.applicationContext;import org.springframework.context.support.ClassPathxmlApplicationContext;public class Main {	public static void main(String[] args) {		// TODO Auto-generated method stub//		//		//      HelloWorld helloWorld=new HelloWorld();//      helloWorld.setName("wzc");//            //1.创建Spring 的IOC 容器对象		ApplicationContext ctx=new ClassPathXmlApplicationContext("appContext.xml");		//2.从IOC 容器中获取bean实例    //  HelloWorld helloWorld=(HelloWorld) ctx.getBean("helloWorld");          //3. 使用 bean     // helloWorld.hello();	}}helloWord代码:

package spring;public class HelloWorld {    private String name;    public HelloWorld() {		System.out.println("Hello wzc");	}    public void setName(String name) {    	System.out.println("setname"+name);		this.name = name;	}    public void hello() {	 System.out.println("hello"+name);    }}xml 文件代码:

<?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="spring.HelloWorld">        <property name="name" value="Spring"></property>            </bean></beans>


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表