Spring learning summary (1)

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>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325860899&siteId=291194637