Simple programming of spring framework (hello world!)
First, create a java project project and name it
Second , import the jar package and log file used by spring into the project
Third, create source files helloWorld.java and mainEntry.java
helloWorld.java is as follows
public class helloWorld{
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println(“输出信息为:”+message);
}
}
The mainEntry.java file is as follows
public class mainEntry{
public static void main(String[] args){
ApplicationContext context = new classPathXmlApplicationContext(“bean.xml”);
helloWorld obj = (helloWorld) context.getBean(“helloworld”);
obj.getMessage();
}
}
Note: ClassPathXmlApplicationContext() to create the application context.
GetBean to get the bean is to convert the actual object through the id.
Finally , the configuration file for creating the bean (created in the src directory) bean.xml is as follows:
<beans>
<bean id=”helloworld” class=”com.–.helloWorld”>
// class is helloWorld path
< property name=”message” value=”Hello World!”/>
< /bean>
< /beans>