Learn spring in spring (2) (zz)

Source: Internet
Author: User

Ii. Simple Example of spring Object Management

Bromon originality, please respect copyright

Any objects to be handed over to spring management must be registered in the configuration file. This process is called wiring. The following is a simple hello World demonstration. The classes we will register are as follows:

 

  1. /*
  2. * Creation date: 2005-3-22
  3. */
  4. PackageOrg. bromon. Spring. test;
  5.  
  6. /**
  7. * @ Author bromon
  8. */
  9. Public ClassHellotalker
  10. {
  11. Public StringGreeting ()
  12. {
  13. Return"Hello World ";
  14. }
  15. }

Then we will compile a spring configuration file with Arbitrary File names. Here it is springconfig. xml. Note that this file should be stored in the path contained in classpath:

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <! Doctype beans public "-// spring // DTD bean // en" "http://www.springframework.org/dtd/spring-beans.dtd">
  3. <Beans>
  4. <Bean id = "hellotalker"Class= "Org. bromon. Spring. Test. hellotalker">
  5. </Bean>
  6. </Beans>

By using the bean tag, A hellotalker object is registered. Its name is hellotalker. Then we compile a test class that uses the interfaces provided by the Spring framework to load the configuration file and obtain an object by specifying the Object ID. The Code is as follows:

  1. /*
  2. * Creation date: 2005-3-17
  3. */
  4. PackageOrg. bromon. Spring. Test. JUnit;
  5.  
  6. ImportJava. Io.Fileinputstream;
  7.  
  8. ImportOrg. springframework. Beans. Factory. xml. xmlbeanfactory;
  9. ImportOrg. springframework. Context. applicationcontext;
  10. ImportOrg. springframework. Context. Support. classpathxmlapplicationcontext;
  11.  
  12. ImportOrg. bromon. Spring. test;
  13.  
  14. /**
  15. * @ Author bromon
  16. */
  17. Public ClassTeststudentmanagerExtendsTestcase {
  18.  
  19. Public VoidTesthellotalker ()
  20. {
  21. Try
  22. {
  23. Applicationcontext context =NewClasspathxmlapplicationcontext ("springconfig. xml ");
  24. Hellotalker ht = (hellotalker) Context. getbean ("hellotalker ");
  25. System. Out. println (HT. Greeting ());
  26. }Catch(ExceptionE)
  27. {
  28. E. printstacktrace ();
  29. }
  30. }
  31.  
  32. }

This program is complete, because only one object hellotalker is registered in spring, so there is no dependency between objects, and of course no dependency injection is involved. The following shows a simple dependency injection:

The first step is to modify hellotalker and add a string name attribute:

  1. Public StringName;

Compile the Set Method for this attribute. This method must strictly abide by the naming rules of JavaBean:

  1. Public VoidSetname (StringName)
  2. {
  3.   This. Name = Name;
  4. }

Modify the greeting method:

  1. Public StringGreeting ()
  2. {
  3.   Return"Hello" + name;
  4. }

As you can see, the name attribute is not initially tested, because its value will be dynamically injected into the runtime by spring.

Step 2: Modify the unique bean configuration in springconfig. xml:

  1. <Bean id = "hellotalker"Class= "Org. bromon. Spring. Test. hellotalker">
  2. <Property name = "name">
  3. <Value> bromon </value>
  4. </Property>
  5. </Bean>

Modification completed. We will write a name "bromon" into springconfig. xml and it will be dynamically injected into the name attribute of hellotalker. The greeting method will print it out. Run the JUnit class again and you can see the result.

We only demonstrate how to inject a simple string. In fact, we can inject any value type, instance of any class, list, map, and properties. The configuration file manages the relationship between all objects and objects, while objects are only responsible for executing their own functions. The fewer their responsibilities, the lower their coupling, the easier the system to test, management and maintenance are also easier.

<Bean> A tag has many attributes used to specify how an object is instantiated. It also has many sub-tags used to configure the attributes of an object. For more information, see related DTD and documentation, can quickly grasp. This series of articles is not a spring manual. for basic spring knowledge, see spring in action, which is detailed and accurate enough. The subsequent sections will discuss more details and advanced features of system design and development.

  1. /*
  2. * Creation date: 2005-3-22
  3. */
  4. PackageOrg. bromon. Spring. test;
  5.  
  6. /**
  7. * @ Author bromon
  8. */
  9. Public ClassHellotalker
  10. {
  11. Public StringGreeting ()
  12. {
  13. Return"Hello World ";
  14. }
  15. }

Then we will compile a spring configuration file with Arbitrary File names. Here it is springconfig. xml. Note that this file should be stored in the path contained in classpath:

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <! Doctype beans public "-// spring // DTD bean // en" "http://www.springframework.org/dtd/spring-beans.dtd">
  3. <Beans>
  4. <Bean id = "hellotalker"Class= "Org. bromon. Spring. Test. hellotalker">
  5. </Bean>
  6. </Beans>

By using the bean tag, A hellotalker object is registered. Its name is hellotalker. Then we compile a test class that uses the interfaces provided by the Spring framework to load the configuration file and obtain an object by specifying the Object ID. The Code is as follows:

  1. /*
  2. * Creation date: 2005-3-17
  3. */
  4. PackageOrg. bromon. Spring. Test. JUnit;
  5.  
  6. ImportJava. Io.Fileinputstream;
  7.  
  8. ImportOrg. springframework. Beans. Factory. xml. xmlbeanfactory;
  9. ImportOrg. springframework. Context. applicationcontext;
  10. ImportOrg. springframework. Context. Support. classpathxmlapplicationcontext;
  11.  
  12. ImportOrg. bromon. Spring. test;
  13.  
  14. /**
  15. * @ Author bromon
  16. */
  17. Public ClassTeststudentmanagerExtendsTestcase {
  18.  
  19. Public VoidTesthellotalker ()
  20. {
  21. Try
  22. {
  23. Applicationcontext context =NewClasspathxmlapplicationcontext ("springconfig. xml ");
  24. Hellotalker ht = (hellotalker) Context. getbean ("hellotalker ");
  25. System. Out. println (HT. Greeting ());
  26. }Catch(ExceptionE)
  27. {
  28. E. printstacktrace ();
  29. }
  30. }
  31.  
  32. }

This program is complete, because only one object hellotalker is registered in spring, so there is no dependency between objects, and of course no dependency injection is involved. The following shows a simple dependency injection:

The first step is to modify hellotalker and add a string name attribute:

  1. Public StringName;

Compile the Set Method for this attribute. This method must strictly abide by the naming rules of JavaBean:

  1. Public VoidSetname (StringName)
  2. {
  3.   This. Name = Name;
  4. }

Modify the greeting method:

  1. Public StringGreeting ()
  2. {
  3.   Return"Hello" + name;
  4. }

As you can see, the name attribute is not initially tested, because its value will be dynamically injected into the runtime by spring.

Step 2: Modify the unique bean configuration in springconfig. xml:

  1. <Bean id = "hellotalker"Class= "Org. bromon. Spring. Test. hellotalker">
  2. <Property name = "name">
  3. <Value> bromon </value>
  4. </Property>
  5. </Bean>

Modification completed. We will write a name "bromon" into springconfig. xml and it will be dynamically injected into the name attribute of hellotalker. The greeting method will print it out. Run the JUnit class again and you can see the result.

We only demonstrate how to inject a simple string. In fact, we can inject any value type, instance of any class, list, map, and properties. The configuration file manages the relationship between all objects and objects, while objects are only responsible for executing their own functions. The fewer their responsibilities, the lower their coupling, the easier the system to test, management and maintenance are also easier.

<Bean> A tag has many attributes used to specify how an object is instantiated. It also has many sub-tags used to configure the attributes of an object. For more information, see related DTD and documentation, can quickly grasp. This series of articles is not a spring manual. for basic spring knowledge, see spring in action, which is detailed and accurate enough. The subsequent sections will discuss more details and advanced features of system design and development.

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.