Spring dependency Injection

Source: Internet
Author: User

1. Add the Jar package of spring and the Jar package of spring's additional dependencies.


Specifies the method for creating a class (the corresponding constructor is used by default). A method is called before the class is created. Method created after the class is destroyed

package com.lubby.test;public class Single {private String id;private String something;private static Single single;private Single() {super();}private Single(String id, String something) {super();this.id = id;this.something = something;}public static Single getInstance(String id, String something) {if (single == null) {single = new Single(id, something);}return single;}public void init() {System.out.println("doSomethingBefor........");}public void destroy() {System.out.println("doSomethingAfter........");}@Overridepublic String toString() {return "Single [id=" + id + ", something=" + something + "]";}}



Configuration in spring

 
  
factory-method="getInstance" 
Specifies the method for creating a class (corresponding constructor is used by default ),
init-method="init"
Call a method before creating a class. Method created after the class is destroyed

 destroy-method="destroy"

    
    


Dependency Injection

Course class

package com.lubby.test;public class Course {private String courseName;private String teacherName;public Course() {super();}public Course(String courseName, String teacherName) {super();this.courseName = courseName;this.teacherName = teacherName;}public String getCourseName() {return courseName;}public void setCourseName(String courseName) {this.courseName = courseName;}public String getTeacherName() {return teacherName;}public void setTeacherName(String teacherName) {this.teacherName = teacherName;}@Overridepublic String toString() {return "Course [courseName=" + courseName + ", teacherName=" + teacherName + "]";}}


Student Class

package com.lubby.test;public class Student {private String name;private String school;private int age;private Course course;private String address;
public Student() {super();// TODO Auto-generated constructor stub}public Student(String name, String school, int age, Course course) {super();this.name = name;this.school = school;this.age = age;this.course = course;}public void setAddress(String address) {this.address = address;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSchool() {return school;}public void setSchool(String school) {this.school = school;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Course getCourse() {return course;}public void setCourse(Course course) {this.course = course;}@Overridepublic String toString() {return "Student [name=" + name + ", school=" + school + ", age=" + age + ", course=" + course + ", address="+ address + "]";}}
1. Inject parameters through Constructors

     
      
      
     

3. Inject parameters using the setting Method

     
      
      
      
      
     
Run

Package com. lubby. test; import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext; public class Test {public static void main (String [] args) {// load the configuration file and obtain the context ApplicationContext ctx = new ClassPathXmlApplicationContext ("com/lubby/test. xml "); // Single single Single = (Single) ctx. getBean ("single"); // System. out. println (single); // single. destroy (); // create and obtain bean Course course = (Course) ctx by bean id in the configuration file. getBean ("course"); System. out. println (course); Student student = (Student) ctx. getBean ("stu"); System. out. println (student );}}









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.