Download spring:http://repo.spring.io/libs-release-local/org/springframework/spring download Zip Tarball: and unzip.
Ii. developing Spring applications in Eclipse Yes MyEclipse
1. The new Java Project Project is named Myspring 2. Add Spring support for this project. Add user Libraries spring4.0.6 and common-logging add methods such as:
Commons-logging-1.1.3.jar
3. Define the Bean (POJO) Src\hsl\service\personservice.java code in a Spring management container as follows:
[Java]View PlainCopy
- Package hsl.service;
- Public class Personservice {
- private String name;
- //The setter method for the Name property
- public void SetName (String name) {
- this.name = name;
- }
- //Test the info method of the person class
- public void info () {
- System.out.println ("This person is named:" + name);
- }
- }
Note: Spring can manage arbitrary POJO and does not require the Java class to be a standard JavaBean.
4. Write the main program, the program initializes the Spring container Src\hsl\springtest.java code as follows:
[Java]View PlainCopy
- Package HSL;
- Import Hsl.service.PersonService;
- Import Org.springframework.context.ApplicationContext;
- Import Org.springframework.context.support.ClassPathXmlApplicationContext;
- Public class Springtest {
- public static void Main (string[] args) {
- //Create a spring ApplicationContext
- ApplicationContext CTX = new Classpathxmlapplicationcontext ("Bean.xml");
- System.out.println (CTX); //Output Spring container
- //Get the person instance through the Spring container and set the property value for the person instance (this is called inversion of Control, IoC)
- Personservice p = Ctx.getbean ("Personservice", Personservice. Class);
- P.info ();
- }
- }
5. Deploy the Persionservice class in the Spring configuration file with the Src\bean.xml code as follows:
[HTML]View PlainCopy
- <? XML version= "1.0" encoding="UTF-8"?>
- <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="Http://www.springframework.org/schema/beans"
- xsi:schemalocation= "Http://www.springframework.org/schema/beans
- Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">
- <!--Deploy the Personservice class as a bean in a spring container--
- <Bean id= "personservice" class="Hsl.service.PersonService">
- <property name="name" value="Wawa"/>
- </Bean>
- </Beans>
6. Run the main program with the following results:
Eclipse builds the spring environment