码迷,mamicode.com
首页 > 编程语言 > 详细

Spring4

时间:2014-09-01 02:50:03      阅读:315      评论:0      收藏:0      [点我收藏+]

标签:private   public   return   import   

  1. Hello World

    bubuko.com,布布扣

HelloWorld.java

package com.test.spring01;

public class HelloWorld {
	private String name;
	
	public HelloWorld() {
		System.out.println("调用构造函数...");
	}

	public String getName() {
		return name;
	}

	public void setName2(String name) {//对应name="name2" 
		this.name = name;
	}
	
	public void sayHello() {
		System.out.println("Hello," + name);
	}
	
}

Main.java

package com.test.spring01;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
		System.out.println("创建对象");
		HelloWorld helloWorld = (HelloWorld)applicationContext.getBean("a1");
		helloWorld.sayHello();
	}
}

applicationContext.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-3.0.xsd">
                                 
    <bean id="a1" class="com.test.spring01.HelloWorld">
    	<property name="name2" value="spring"/>
    </bean>
</beans>

运行结果

调用构造函数...

创建对象

Hello,spring



本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1547187

Spring4

标签:private   public   return   import   

原文地址:http://shamrock.blog.51cto.com/2079212/1547187

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!