Spring详细内容En(2)

电玩女神 2023-09-24 18:39 143阅读 0赞

1.Constructor Injection with Dependent Object

  • If there is HAS-A relationship between the classes, we create the instance of dependent object (contained object) first then pass it as an argument of the main class constructor.
  • Here, our scenario is Employee HAS-A Address. The Address class object will be termed as the dependent object.

Address.java

This class contains three properties, one constructor and toString() method to return the values of these object.

  1. public class Address {
  2. private String city;
  3. private String state;
  4. private String country;
  5. public Address(String city, String state, String country) {
  6. this.city = city;
  7. this.state = state;
  8. this.country = country;
  9. }
  10. public String toString(){
  11. return city+" "+state+" "+country;
  12. }
  13. }

Employee.java

It contains three properties id, name and address(dependent object) ,two constructors and show() method to show the records of the current object including the depedent object.

  1. public class Employee {
  2. private int id;
  3. private String name;
  4. private Address address;
  5. public Employee(int id, String name, Address address) {
  6. this.id = id;
  7. this.name = name;
  8. this.address = address;
  9. }
  10. void show(){
  11. System.out.println(id+" "+name);
  12. System.out.println(address.toString());
  13. }
  14. }

applicationContext.xml

Theref attribute is used to define the reference of another object, such way we are passing the dependent object as an constructor argument.

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  8. <bean id="address" class="com.amy.Address">
  9. <constructor-arg value="Suzhou" type="String"></constructor-arg>
  10. <constructor-arg value="Jangsu" type="String"></constructor-arg>
  11. <constructor-arg value="China" type="String"></constructor-arg>
  12. </bean>
  13. <bean id="employee" class="com.amy.Employee">
  14. <constructor-arg value="1" type="int"></constructor-arg>
  15. <constructor-arg value="Amy" type="String"></constructor-arg>
  16. <constructor-arg>
  17. <ref bean = "address"/>
  18. </constructor-arg>
  19. </bean>
  20. </beans>

Test.java

This class gets the bean from the applicationContext.xml file and calls the show method.

  1. public class Test1 {
  2. public static void main(String[] args) {
  3. Resource resource = new ClassPathResource("applicationContext.xml");
  4. BeanFactory factory = new XmlBeanFactory(resource);
  5. Employee employee = (Employee) factory.getBean("employee");
  6. employee.show();
  7. }
  8. }
  9. 1 Amy
  10. Suzhou Jangsu China

2.Constructor Injection with Collection Example

  • We can inject collection values by constructorin spring framework.
  • There can be used three elements inside the constructor-arg element.

    • list
    • set
    • map

Question.java

  1. public class Question {
  2. private int id;
  3. private String name;
  4. private List<String> answers;
  5. public Question(int id, String name, List<String> answers) {
  6. this.id = id;
  7. this.name = name;
  8. this.answers = answers;
  9. }
  10. public void displayInfo(){
  11. System.out.println(id+" "+name);
  12. Iterator<String> itr = answers.iterator();
  13. while(itr.hasNext()){
  14. System.out.println(itr.next());
  15. }
  16. }
  17. }

applicationContext.xml

The list element of constructor-arg is used here to define the list.

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  8. <bean id="question" class="com.amy.Question">
  9. <constructor-arg value="1" type="int"></constructor-arg>
  10. <constructor-arg value="Amy" type="String"></constructor-arg>
  11. <constructor-arg>
  12. <list>
  13. <value>Java is a programming language</value>
  14. <value>Java is a Platform</value>
  15. </list>
  16. </constructor-arg>
  17. </bean>
  18. </beans>

Test.java

  1. public class Test {
  2. public static void main(String[] args) {
  3. Resource resource = new ClassPathResource("applicationContext.xml");
  4. BeanFactory factory = new XmlBeanFactory(resource);
  5. Question question= (Question) factory.getBean("question");
  6. question.displayInfo();
  7. }
  8. }
  9. 1 Amy
  10. Java is a programming language
  11. Java is a Platform

3.Constructor Injection with Non-String Collection (having Dependent Object) Example

  • If we have dependent object in the collection, we can inject these information by using the ref element inside the list, set or map.
  • In this example, we are taking the example of Forum where One question can have multiple answers.

Question.java

This class contains three properties, two constructors and displayInfo() method that prints the information. Here, we are using List to contain the multiple answers.

  1. public class Question {
  2. private int id;
  3. private String name;
  4. private List<Answer> answers;
  5. public Question() {
  6. }
  7. public Question(int id, String name, List<Answer> answers) {
  8. this.id = id;
  9. this.name = name;
  10. this.answers = answers;
  11. }
  12. public void displayInfo(){
  13. System.out.println(id+" "+name);
  14. Iterator<Answer> itr = answers.iterator();
  15. while(itr.hasNext()){
  16. System.out.println(itr.next());
  17. }
  18. }
  19. }

Answer.java

This class has three properties id, name and by with constructor and toString() method.

  1. public class Answer {
  2. private int id;
  3. private String name;
  4. private String by;
  5. public Answer(){
  6. }
  7. public Answer(int id, String name, String by) {
  8. super();
  9. this.id = id;
  10. this.name = name;
  11. this.by = by;
  12. }
  13. public String toString(){
  14. return id+" "+name+" "+by;
  15. }
  16. }

applicationContext.xml

The refelement is used to define the reference of another bean. Here, we are using bean attribute of ref element to specify the reference of another bean.

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  8. <bean id="ans1" class="com.amy.Answer">
  9. <constructor-arg value="1" type="int"></constructor-arg>
  10. <constructor-arg value="Ste" type="String"></constructor-arg>
  11. <constructor-arg value="Java is a programming language" type="String"></constructor-arg>
  12. </bean>
  13. <bean id="ans2" class="com.amy.Answer">
  14. <constructor-arg value="2" type="int"></constructor-arg>
  15. <constructor-arg value="David" type="String"></constructor-arg>
  16. <constructor-arg value="Java is a Platform" type="String"></constructor-arg>
  17. </bean>
  18. <bean id="question" class="com.amy.Question">
  19. <constructor-arg value="1" type="int"></constructor-arg>
  20. <constructor-arg value="Amy" type="String"></constructor-arg>
  21. <constructor-arg>
  22. <list>
  23. <ref bean="ans1"/>
  24. <ref bean="ans2"/>
  25. </list>
  26. </constructor-arg>
  27. </bean>
  28. </beans>

Test.java

  1. public class Test {
  2. public static void main(String[] args) {
  3. Resource resource = new ClassPathResource("applicationContext.xml");
  4. BeanFactory factory = new XmlBeanFactory(resource);
  5. Question question= (Question) factory.getBean("question");
  6. question.displayInfo();
  7. }
  8. }
  9. 1 Amy
  10. 1 Ste Java is a programming language
  11. 2 David Java is a Platform

4.Constructor Injection with Non-String Map (having dependent Object) Example

  • In this example, we are using map as the answer that have Answer and User.
  • Here, we are using key and value pair both as an object. Answerhas its own information such as answerId, answer and postedDate, Userhas its own information such asuserId, username, emailId.

    public class Answer {

    1. private int id;
    2. private String name;
    3. private String by;
    4. public Answer(){
    5. }
    6. public Answer(int id, String name, String by) {
    7. super();
    8. this.id = id;
    9. this.name = name;
    10. this.by = by;
    11. }
    12. public String toString(){
    13. return id+" "+name+" "+by;
    14. }

    }

    public class User {

    1. private int id;
    2. private String name,email;
    3. public User(){
    4. }
    5. public User(int id, String name, String email) {
    6. this.id = id;
    7. this.name = name;
    8. this.email = email;
    9. }
    10. @Override
    11. public String toString() {
    12. return "Id:"+id+" Name:"+name+" Email Id:"+email;
    13. }

    }

    public class Question {

    1. private int id;
    2. private String name;
    3. private Map<Answer,User> answers;
    4. public Question() {
    5. }
    6. public Question(int id, String name, Map<Answer,User> answers) {
    7. this.id = id;
    8. this.name = name;
    9. this.answers = answers;
    10. }
    11. public void displayInfo(){
    12. System.out.println("question id:"+id);
    13. System.out.println("question name:"+name);
    14. System.out.println("Answers....");
    15. Set<Map.Entry<Answer,User>> set= answers.entrySet();
    16. Iterator<Map.Entry<Answer,User>> itr = set.iterator();
    17. while(itr.hasNext()){
    18. Map.Entry<Answer,User> entry = itr.next();
    19. Answer ans = entry.getKey();
    20. User user = entry.getValue();
    21. System.out.println("Answer Information:");
    22. System.out.println(ans);
    23. System.out.println("Posted By:");
    24. System.out.println(user);
    25. }
    26. }

    }

    <?xml version=”1.0” encoding=”UTF-8”?>











    Ste@gmail.com" >









    LiLi@gmail.com" >













    public class Test {

    1. public static void main(String[] args) {
    2. Resource resource = new ClassPathResource("applicationContext.xml");
    3. BeanFactory factory = new XmlBeanFactory(resource);
    4. Question question= (Question) factory.getBean("question");
    5. question.displayInfo();
    6. }

    }

5.Inheriting Bean in Spring

  • By using the parent attribute of bean, we can specify the inheritance relation between the beans. In such case, parent bean values will be inherited to the current bean.

    public class Employee {

    1. private int id;
    2. private String name;
    3. private Address address;
    4. public Employee() {
    5. }
    6. public Employee(int id, String name) {
    7. this.id = id;
    8. this.name = name;
    9. }
    10. public Employee(int id, String name, Address address) {
    11. this.id = id;
    12. this.name = name;
    13. this.address = address;
    14. }
    15. void show(){
    16. System.out.println(id+" "+name);
    17. System.out.println(address.toString());
    18. }

    }

    public class Address {

    1. private String city;
    2. private String state;
    3. private String country;
    4. public Address(String city, String state, String country) {
    5. this.city = city;
    6. this.state = state;
    7. this.country = country;
    8. }
    9. public String toString(){
    10. return city+" "+state+" "+country;
    11. }

    }

    <?xml version=”1.0” encoding=”UTF-8”?>

  1. <bean id="e1" class="com.amy.Employee">
  2. <constructor-arg value="1" ></constructor-arg>
  3. <constructor-arg value="Ste"></constructor-arg>
  4. </bean>
  5. <bean id="address1" class="com.amy.Address">
  6. <constructor-arg value="Ghaziabad"></constructor-arg>
  7. <constructor-arg value="UP"></constructor-arg>
  8. <constructor-arg value="USA"></constructor-arg>
  9. </bean>
  10. <bean id="employee" class="com.amy.Employee" parent="e1">
  11. <constructor-arg ref="address1"></constructor-arg>
  12. </bean>
  13. </beans>
  14. package com.amy;
  15. import org.springframework.beans.factory.BeanFactory;
  16. import org.springframework.beans.factory.xml.XmlBeanFactory;
  17. import org.springframework.core.io.ClassPathResource;
  18. import org.springframework.core.io.Resource;
  19. /**
  20. * @author: Amy
  21. * @create: 2023-02-14 16:13
  22. **/
  23. public class Test1 {
  24. public static void main(String[] args) {
  25. Resource resource = new ClassPathResource("applicationContext.xml");
  26. BeanFactory factory = new XmlBeanFactory(resource);
  27. Employee employee = (Employee) factory.getBean("employee");
  28. employee.show();
  29. }
  30. }
  31. 1 Ste
  32. Ghaziabad UP USA

6.Dependency Injection by setter method

  • We can inject the dependency by setter method also.
  • The <property> subelement of <bean> is used for setter injection.

    • primitive and String-based values
    • Dependent object (contained object)
    • Collection values etc.

Injecting primitive and string-based values by setter method

  1. public class Employee {
  2. private int id;
  3. private String name;
  4. private String city;
  5. public int getId() {
  6. return id;
  7. }
  8. public void setId(int id) {
  9. this.id = id;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public String getCity() {
  18. return city;
  19. }
  20. public void setCity(String city) {
  21. this.city = city;
  22. }
  23. void display(){
  24. System.out.println(id + " "+ name+" "+city);
  25. }
  26. }
  27. <?xml version="1.0" encoding="UTF-8"?>
  28. <beans
  29. xmlns="http://www.springframework.org/schema/beans"
  30. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  31. xmlns:p="http://www.springframework.org/schema/p"
  32. xsi:schemaLocation="http://www.springframework.org/schema/beans
  33. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  34. <bean id="employee" class="com.amy.Employee" >
  35. <property name="id">
  36. <value>1</value>
  37. </property>
  38. <property name="name">
  39. <value>Amy</value>
  40. </property>
  41. <property name="city">
  42. <value>Shanghai</value>
  43. </property>
  44. </bean>
  45. </beans>
  46. package com.amy;
  47. import org.springframework.beans.factory.BeanFactory;
  48. import org.springframework.beans.factory.xml.XmlBeanFactory;
  49. import org.springframework.core.io.ClassPathResource;
  50. import org.springframework.core.io.Resource;
  51. /**
  52. * @author: Amy
  53. * @create: 2023-02-14 16:13
  54. **/
  55. public class Test1 {
  56. public static void main(String[] args) {
  57. Resource resource = new ClassPathResource("applicationContext.xml");
  58. BeanFactory factory = new XmlBeanFactory(resource);
  59. Employee employee = (Employee) factory.getBean("employee");
  60. employee.display();
  61. }
  62. }
  63. 1 Amy Shanghai

Setter Injection with Dependent ObjectExample

  1. public class Employee {
  2. private int id;
  3. private String name;
  4. private Address address;
  5. public int getId() {
  6. return id;
  7. }
  8. public void setId(int id) {
  9. this.id = id;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public Address getAddress() {
  18. return address;
  19. }
  20. public void setAddress(Address address) {
  21. this.address = address;
  22. }
  23. void display(){
  24. System.out.println(id+" "+name);
  25. System.out.println(address);
  26. }
  27. }
  28. public class Address {
  29. private String city;
  30. private String state;
  31. private String country;
  32. public String getCity() {
  33. return city;
  34. }
  35. public void setCity(String city) {
  36. this.city = city;
  37. }
  38. public String getState() {
  39. return state;
  40. }
  41. public void setState(String state) {
  42. this.state = state;
  43. }
  44. public String getCountry() {
  45. return country;
  46. }
  47. public void setCountry(String country) {
  48. this.country = country;
  49. }
  50. public String toString(){
  51. return city+" "+state+" "+country;
  52. }
  53. }
  54. <?xml version="1.0" encoding="UTF-8"?>
  55. <beans
  56. xmlns="http://www.springframework.org/schema/beans"
  57. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  58. xmlns:p="http://www.springframework.org/schema/p"
  59. xsi:schemaLocation="http://www.springframework.org/schema/beans
  60. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  61. <bean id="address" class="com.amy.Address">
  62. <property name="city" value="Ghaziabad"></property>
  63. <property name="state" value="UP"></property>
  64. <property name="country" value="India"></property>
  65. </bean>
  66. <bean id="employee" class="com.amy.Employee" >
  67. <property name="id">
  68. <value>1</value>
  69. </property>
  70. <property name="name">
  71. <value>Amy</value>
  72. </property>
  73. <property name="address" ref="address">
  74. </property>
  75. </bean>
  76. </beans>
  77. package com.amy;
  78. import org.springframework.beans.factory.BeanFactory;
  79. import org.springframework.beans.factory.xml.XmlBeanFactory;
  80. import org.springframework.core.io.ClassPathResource;
  81. import org.springframework.core.io.Resource;
  82. /**
  83. * @author: Amy
  84. * @create: 2023-02-14 16:13
  85. **/
  86. public class Test1 {
  87. public static void main(String[] args) {
  88. Resource resource = new ClassPathResource("applicationContext.xml");
  89. BeanFactory factory = new XmlBeanFactory(resource);
  90. Employee employee = (Employee) factory.getBean("employee");
  91. employee.display();
  92. }
  93. }
  94. 1 Amy
  95. Ghaziabad UP India

Setter Injection with Collection Example

  • We can inject collection values by setter methodin spring framework. There can be used three elementsinside the property element.
  • It can be:

    • list
    • set
    • map
  • Each collection can have string based andnon-stringbased values.

    package com.amy;

    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;

    /**

    • @author: Amy
    • @create: 2023-02-15 18:39
      **/
      public class Question {

      private int id;
      private String name;
      private List answers;

      public int getId() {

      1. return id;

      }

      public void setId(int id) {

      1. this.id = id;

      }

      public String getName() {

      1. return name;

      }

      public void setName(String name) {

      1. this.name = name;

      }

      public List getAnswers() {

      1. return answers;

      }

      public void setAnswers(List answers) {

      1. this.answers = answers;

      }
      public void displayInfo(){

      1. System.out.println(id+" "+name);
      2. System.out.println("answers are:");
      3. Iterator<String> itr=answers.iterator();
      4. while(itr.hasNext()){
      5. System.out.println(itr.next());
      6. }

      }
      }

    <?xml version=”1.0” encoding=”UTF-8”?>





    1


    “What is Java?”



    Java is a programming language
    Java is a platform
    Java is an Island



    package com.amy;

    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.Resource;

  1. /**
  2. * @author: Amy
  3. * @create: 2023-02-14 14:39
  4. **/
  5. public class Test {
  6. public static void main(String[] args) {
  7. Resource resource = new ClassPathResource("applicationContext.xml");
  8. BeanFactory factory = new XmlBeanFactory(resource);
  9. Question question= (Question) factory.getBean("question");
  10. question.displayInfo();
  11. }
  12. }
  13. 1 "What is Java?"
  14. answers are:
  15. Java is a programming language
  16. Java is a platform
  17. Java is an Island

Setter Injection with Non-String Collection (having Dependent Object) Example

  • If we have dependent object in the collection, we can inject these information by using the ref element inside the list, set or map.
  • Here, we will use list, set or map element inside the property element.

    public class Answer {

    1. private int id;
    2. private String name;
    3. private String by;
    4. public int getId() {
    5. return id;
    6. }
    7. public void setId(int id) {
    8. this.id = id;
    9. }
    10. public String getName() {
    11. return name;
    12. }
    13. public void setName(String name) {
    14. this.name = name;
    15. }
    16. public String getBy() {
    17. return by;
    18. }
    19. public void setBy(String by) {
    20. this.by = by;
    21. }
    22. public String toString(){
    23. return id+" "+name+" "+by;
    24. }

    }

    public class Question {

    1. private int id;
    2. private String name;
    3. private List<Answer> answers;;
    4. public int getId() {
    5. return id;
    6. }
    7. public void setId(int id) {
    8. this.id = id;
    9. }
    10. public String getName() {
    11. return name;
    12. }
    13. public void setName(String name) {
    14. this.name = name;
    15. }
    16. public List<Answer> getAnswers() {
    17. return answers;
    18. }
    19. public void setAnswers(List<Answer> answers) {
    20. this.answers = answers;
    21. }
    22. public void displayInfo(){
    23. System.out.println(id+" "+name);
    24. Iterator<Answer> itr = answers.iterator();
    25. while(itr.hasNext()){
    26. System.out.println(itr.next());
    27. }
    28. }

    }

    <?xml version=”1.0” encoding=”UTF-8”?>
















    1


    “What is Java?”








    public class Test {

    1. public static void main(String[] args) {
    2. Resource resource = new ClassPathResource("applicationContext.xml");
    3. BeanFactory factory = new XmlBeanFactory(resource);
    4. Question question= (Question) factory.getBean("question");
    5. question.displayInfo();
    6. }

    }

    1 “What is Java?”
    answers are:
    1 Java is a programming language Ravi Malik
    2 Java is a platform Sachin

Setter Injection with Map Example

  • In this example, we are using map as the answer for a question that have answer as the key and username as the value.
  • Here, we are usingkey and value pair both as a string.

Like previous examples, it is the example of forum where one question can have multiple answers.

  1. public class Question {
  2. private int id;
  3. private String name;
  4. private Map<String,String> answers;
  5. public int getId() {
  6. return id;
  7. }
  8. public void setId(int id) {
  9. this.id = id;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public Map<String, String> getAnswers() {
  18. return answers;
  19. }
  20. public void setAnswers(Map<String, String> answers) {
  21. this.answers = answers;
  22. }
  23. public void displayInfo(){
  24. System.out.println("question id:"+id);
  25. System.out.println("question name:"+name);
  26. System.out.println("Answers....");
  27. Set<Map.Entry<String, String>> set=answers.entrySet();
  28. Iterator<Map.Entry<String, String>> itr=set.iterator();
  29. while(itr.hasNext()){
  30. Map.Entry<String,String> entry=itr.next();
  31. System.out.println("Answer:"+entry.getKey()+" Posted By:"+entry.getValue());
  32. }
  33. }
  34. }
  35. <?xml version="1.0" encoding="UTF-8"?>
  36. <beans
  37. xmlns="http://www.springframework.org/schema/beans"
  38. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39. xmlns:p="http://www.springframework.org/schema/p"
  40. xsi:schemaLocation="http://www.springframework.org/schema/beans
  41. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  42. <bean id="question" class="com.amy.Question" >
  43. <property name="id">
  44. <value>1</value>
  45. </property>
  46. <property name="name">
  47. <value>"What is Java?"</value>
  48. </property>
  49. <property name="answers">
  50. <map>
  51. <entry key =" Java is a programming language" value="Amy" ></entry>
  52. <entry key ="Java is a Platform " value="LiLi" ></entry>
  53. </map>
  54. </property>
  55. </bean>
  56. </beans>
  57. public class Test {
  58. public static void main(String[] args) {
  59. Resource resource = new ClassPathResource("applicationContext.xml");
  60. BeanFactory factory = new XmlBeanFactory(resource);
  61. Question question= (Question) factory.getBean("question");
  62. question.displayInfo();
  63. }
  64. }
  65. question id:1
  66. question name:"What is Java?"
  67. Answers....
  68. Answer: Java is a programming language Posted By:Amy
  69. Answer:Java is a Platform Posted By:LiLi

Setter Injection with Non-String Map (having dependent Object)

  • In this example, we are using map as theanswer that have Answer and User.
  • Here, we areusing key and value pair both as an object.
  • Answer has its own information such as answerId, answer and postedDate, User has its own information such as userId, username, emailId.

    public class Question {

    1. private int id;
    2. private String name;
    3. private Map<Answer,User> answers;;
    4. public int getId() {
    5. return id;
    6. }
    7. public void setId(int id) {
    8. this.id = id;
    9. }
    10. public String getName() {
    11. return name;
    12. }
    13. public void setName(String name) {
    14. this.name = name;
    15. }
    16. public Map<Answer, User> getAnswers() {
    17. return answers;
    18. }
    19. public void setAnswers(Map<Answer, User> answers) {
    20. this.answers = answers;
    21. }
    22. public void displayInfo(){
    23. System.out.println("question id:"+id);
    24. System.out.println("question name:"+name);
    25. System.out.println("Answers....");
    26. Set<Map.Entry<Answer, User>> set=answers.entrySet();
    27. Iterator<Map.Entry<Answer, User>> itr=set.iterator();
    28. while(itr.hasNext()){
    29. Map.Entry<Answer, User> entry=itr.next();
    30. Answer ans=entry.getKey();
    31. User user=entry.getValue();
    32. System.out.println("Answer Information:");
    33. System.out.println(ans);
    34. System.out.println("Posted By:");
    35. System.out.println(user);
    36. }
    37. }

    }

    public class Answer {

    1. private int id;
    2. private String answer;
    3. public String getPostedDate() {
    4. return postedDate;
    5. }
    6. public void setPostedDate(String postedDate) {
    7. this.postedDate = postedDate;
    8. }
    9. private String postedDate;
    10. public int getId() {
    11. return id;
    12. }
    13. public void setId(int id) {
    14. this.id = id;
    15. }
    16. public String getAnswer() {
    17. return answer;
    18. }
    19. public void setAnswer(String answer) {
    20. this.answer = answer;
    21. }
  1. public String toString(){
  2. return id+" "+ answer +" "+postedDate;
  3. }
  4. }
  5. public class User {
  6. private int id;
  7. private String name,email;
  8. public int getId() {
  9. return id;
  10. }
  11. public void setId(int id) {
  12. this.id = id;
  13. }
  14. public String getName() {
  15. return name;
  16. }
  17. public void setName(String name) {
  18. this.name = name;
  19. }
  20. public String getEmail() {
  21. return email;
  22. }
  23. public void setEmail(String email) {
  24. this.email = email;
  25. }
  26. @Override
  27. public String toString() {
  28. return "Id:"+id+" Name:"+name+" Email Id:"+email;
  29. }
  30. }
  31. <?xml version="1.0" encoding="UTF-8"?>
  32. <beans
  33. xmlns="http://www.springframework.org/schema/beans"
  34. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  35. xmlns:p="http://www.springframework.org/schema/p"
  36. xsi:schemaLocation="http://www.springframework.org/schema/beans
  37. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  38. <bean id="answer1" class="com.amy.Answer">
  39. <property name="id" value="1"></property>
  40. <property name="answer" value="Java is a Programming Language"></property>
  41. <property name="postedDate" value="12/12/2001"></property>
  42. </bean>
  43. <bean id="answer2" class="com.amy.Answer">
  44. <property name="id" value="2"></property>
  45. <property name="answer" value="Java is a Platform"></property>
  46. <property name="postedDate" value="12/12/2003"></property>
  47. </bean>
  48. <bean id="user1" class="com.amy.User">
  49. <property name="id" value="1"></property>
  50. <property name="name" value="Arun Kumar"></property>
  51. <property name="email" value="arun@gmail.com"></property>
  52. </bean>
  53. <bean id="user2" class="com.amy.User">
  54. <property name="id" value="2"></property>
  55. <property name="name" value="Varun Kumar"></property>
  56. <property name="email" value="Varun@gmail.com"></property>
  57. </bean>
  58. <bean id="question" class="com.amy.Question">
  59. <property name="id" value="1"></property>
  60. <property name="name" value="What is Java?"></property>
  61. <property name="answers">
  62. <map>
  63. <entry key-ref="answer1" value-ref="user1"></entry>
  64. <entry key-ref="answer2" value-ref="user2"></entry>
  65. </map>
  66. </property>
  67. </bean>
  68. </beans>
  69. public class Test {
  70. public static void main(String[] args) {
  71. Resource resource = new ClassPathResource("applicationContext.xml");
  72. BeanFactory factory = new XmlBeanFactory(resource);
  73. Question question= (Question) factory.getBean("question");
  74. question.displayInfo();
  75. }
  76. }
  77. question id:1
  78. question name:What is Java?
  79. Answers....
  80. Answer Information:
  81. 1 Java is a Programming Language 12/12/2001
  82. Posted By:
  83. Id:1 Name:Arun Kumar Email Id:arun@gmail.com
  84. Answer Information:
  85. 2 Java is a Platform 12/12/2003
  86. Posted By:
  87. Id:2 Name:Varun Kumar Email Id:Varun@gmail.com

7.Difference between constructor and setter injection

  1. Partial dependency:

    • it can be injected using setter injection but it is not possible by constructor.
    • Suppose there are 3 properties in a class, having 3 arg constructor and setters methods.
    • In such case, if you want to pass information for only one property, it is possible bysetter method only.
  2. Overriding:

    • Setter injection overrides the constructor injection.
    • If we use both constructor and setter injection, IOC container will use the setter injection.
  3. Changes:

    • We can easily change the value by setter injection.
    • It doesn’t create a new bean instance always like constructor.
    • So setter injection is flexible than constructor injection.

发表评论

表情:
评论列表 (有 0 条评论,143人围观)

还没有评论,来说两句吧...

相关阅读