Spring's first program hello
Public class HelloWord {
Private String userName;
Public void setUserName (String userName ){
This. userName = userName;
}
Public void hello (){
System. out. println (hello: + userName );
}
}
Import org. springframework. context. ApplicationContext;
Import org. springframework. context. support. ClassPathXmlApplicationContext;
Public class Test {
Public static void main (String [] args ){
/**
* Create an object and assign values to the object to spring.
*/
// HelloWord helloWord = new HelloWord ();
// HelloWord. setUserName (hello );
// HelloWord. hello ();
// 1. Create a spring IOC container
ApplicationContext ctx = new ClassPathXmlApplicationContext (applicationContext. xml );
// 2 obtain the Bean from the container
HelloWord helloWord = (HelloWord) ctx. getBean (helloWord );
// 3. Call the Method
HelloWord. hello ();
}
}
The configuration file is:
ApplicationContext. xml
Xmlns: p = http://www.springframework.org/schema/p
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>
Output result:
Hello: springsss