打开 myeclipse 创建一个java工程:helloword,并添加spring支持类库,右键工程名称,选择"myeclipse"->"add spring capabilites",如下图:
一个简单的 spring bean.
package com.h3.core; /** * spring bean * */ public class helloworld { private string name; public void setname(string name) { this.name = name; } public void printhello() { system.out.println("spring 3 : hello ! " + name); } }
file : applicationcontext.xml
<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="hellobean" class="com.h3.core.helloworld"> <property name="name" value="h3" /> </bean> </beans>
查看目录结构如下:
package com.h3.core; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; public class app { public static void main(string[] args) { applicationcontext context = new classpathxmlapplicationcontext( "applicationcontext.xml"); helloworld obj = (helloworld) context.getbean("hellobean"); obj.printhello(); } }
spring 3 : hello ! h3