Eclipse builds the spring environment

Source: Internet
Author: User

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 
  1. Package hsl.service;
  2. Public class Personservice {
  3. private String name;
  4. //The setter method for the Name property
  5. public void SetName (String name) {
  6. this.name = name;
  7. }
  8. //Test the info method of the person class
  9. public void info () {
  10. System.out.println ("This person is named:" + name);
  11. }
  12. }

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 
  1. Package HSL;
  2. Import Hsl.service.PersonService;
  3. Import Org.springframework.context.ApplicationContext;
  4. Import Org.springframework.context.support.ClassPathXmlApplicationContext;
  5. Public class Springtest {
  6. public static void Main (string[] args) {
  7. //Create a spring ApplicationContext
  8. ApplicationContext CTX = new Classpathxmlapplicationcontext ("Bean.xml");
  9. System.out.println (CTX); //Output Spring container
  10. //Get the person instance through the Spring container and set the property value for the person instance (this is called inversion of Control, IoC)
  11. Personservice p = Ctx.getbean ("Personservice", Personservice.   Class);
  12. P.info ();
  13. }
  14. }
5. Deploy the Persionservice class in the Spring configuration file with the Src\bean.xml code as follows: [HTML]View PlainCopy 
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="Http://www.springframework.org/schema/beans"
  4. xsi:schemalocation= "Http://www.springframework.org/schema/beans
  5. Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">
  6. <!--Deploy the Personservice class as a bean in a spring container--
  7. <Bean id= "personservice" class="Hsl.service.PersonService">
  8. <property name="name" value="Wawa"/>
  9. </Bean>
  10. </Beans>
6. Run the main program with the following results:

Eclipse builds the spring environment

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.