Notes when using the autowire feature in spring

Source: Internet
Author: User

In spring, autowire = "byname" or "bytype" can be used to provide automatic injection. However, you must note that it does not support automatic injection of basic data types (such as string, Int, and long. For example
Java class:
  1. Public class testbean {
  2. Private string name;
  3. Private long age;
  4. Private string fullname;
  5. Private testbean2 testbean2;
  6. Public void settestbean2 (testbean2 testbean2 ){
  7. This. testbean2 = testbean2;
  8. }
  9. Public testbean2 gettestbean2 (){
  10. Return testbean2;
  11. }
  12. Public void setfullname (string fullname ){
  13. This. fullname = fullname;
  14. }
  15. Public String getfullname (){
  16. Return fullname;
  17. }
  18. Public void setage (long age ){
  19. This. Age = age;
  20. }
  21. Public long getage (){
  22. Return age;
  23. }
  24. Public void setname (string name ){
  25. This. Name = Name;
  26. }
  27. Public String getname (){
  28. Return name;
  29. }
  30. }

Java Class 2:

  1. Public class testbean2 {
  2. Public String getstring (){
  3. Return "testb ";
  4. }
  5. }

Configuration file:

  1. <Beans default-autowire = "byname">
  2. <! -- Login -->
  3. <Bean id = "testbean" class = "testbean">
  4. <Property name = "fullname">
  5. <Value> fullname </value>
  6. </Property>
  7. </Bean>
  8. <Bean id = "name" class = "Java. Lang. String">
  9. <Constructor-Arg>
  10. <Value> hell </value>
  11. </Constructor-Arg>
  12. </Bean>
  13. <Bean id = "Age" class = "Java. Lang. Long">
  14. <Constructor-Arg>
  15. <Value> 20 </value>
  16. </Constructor-Arg>
  17. </Bean>
  18. <Bean id = "testbean2" class = "testbean2"/>
  19. </Beans>

Test class:

  1. Import org. springframework. Context. Support. classpathxmlapplicationcontext;
  2. Public class test {
  3. Public static void main (string [] ARGs ){
  4. Classpathxmlapplicationcontext T = new classpathxmlapplicationcontext (
  5. "Test. xml ");
  6. Testbean bean = (testbean) T. getbean ("testbean ");
  7. System. Out. println ("name:" + bean. getname () + "Age:" + bean. getage ()
  8. + "Fullname:" + bean. getfullname () + "testbean2"
  9. + Bean. gettestbean2 (). getstring ());
  10. }
  11. }

The output result is:
Name: NULL age: NULL fullname: fullname testbean2: testb

To exclude the code of the basic type from spring source code (the source code is from the org. springframework. Beans. Factory. Support. abstractautowirecapablebeanfactory class), go:

  1. Protected string [] unsatisfiednonsimpleproperties (abstractbeandefinition mbd, beanwrapper BW ){
  2. Set result = new treeset ();
  3. Propertyvalues PVS = mbd. getpropertyvalues ();
  4. Propertydescriptor [] PPS = BW. getpropertydescriptors ();
  5. For (INT I = 0; I <PPS. length; I ++ ){
  6. If (PPS [I]. getwritemethod ()! = NULL &&! Isexcludedfromdependencycheck (PAM [I]) &
  7. ! PVS. Contains (PPS [I]. getname ())&&! Beanutils. issimpleproperty (PPS [I]. getpropertytype ())){
  8. Result. Add (PPS [I]. getname ());
  9. }
  10. }
  11. Return stringutils. tostringarray (result );
  12. }

Pay attention to the purple section above. The unsatisfiednonsimpleproperties method is called during Automatic injection. Therefore, basic types are not injected.

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.