Spring的依赖注入
1.加入spring的Jar包,还包含spring额外依赖的Jar包
指定创建类的方法(默认使用相应的构造函数创建),在创建类之前调用某个方法。在销毁类之后创建的方法
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........"); } @Override public String toString() { return "Single [id=" + id + ", something=" + something + "]"; } }
<bean id="single" class="com.lubby.test.Single" factory-method="getInstance" init-method="init" destroy-method="destroy"><pre name="code" class="html"><pre name="code" class="html">factory-method="getInstance"指定创建类的方法(默认使用相应的构造函数创建),
init-method="init"在创建类之前调用某个方法。在销毁类之后创建的方法
<pre name="code" class="html"> destroy-method="destroy"<constructor-arg value="0100102"></constructor-arg><constructor-arg value="I want to play games...."></constructor-arg></bean>
依赖注入
Course类
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; } @Override public String toString() { return "Course [courseName=" + courseName + ", teacherName=" + teacherName + "]"; } }
Student类
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; } @Override public String toString() { return "Student [name=" + name + ", school=" + school + ", age=" + age + ", course=" + course + ", address=" + address + "]"; } }1.通过构造函数注入參数
<bean id="course" class="com.lubby.test.Course"> <constructor-arg value="lilI Wang" name="teacherName"></constructor-arg> <constructor-arg value="Enligsh" name="courseName"></constructor-arg> <span style="font-family: Arial, Helvetica, sans-serif;"></bean></span>3.通过setting方法注入參数
<bean id="stu" class="com.lubby.test.Student"> <property name="name" value="Lubby"></property> <property name="school" value="USTC"></property> <property name="course" ref="course"></property> <property name="address" value="上海市闸北区广中西路555号"></property> </bean>运行
package com.lubby.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { //载入配置文件,获取上下文 ApplicationContext ctx = new ClassPathXmlApplicationContext("com/lubby/test/test.xml"); // Single single = (Single) ctx.getBean("single"); // System.out.println(single); // single.destroy(); // 通过配置文件里bean 的id去创建和取得bean Course course = (Course) ctx.getBean("course"); System.out.println(course); Student student = (Student) ctx.getBean("stu"); System.out.println(student); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。