最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Spring——IOC(依赖注入)的三种方式

互联网 admin 1浏览 0评论

Spring——IOC(依赖注入)的三种方式

依赖注入的三种方式(注入的底层是通过反射实现的)

第一种:set方式的依赖注入

 

applicationContext.xml

id:bean的唯一标识符

class:是实体的包类名

property:name是实体类的属性名;value指的是属性值

<bean id="teacher" class="com.dt.entity.Teacher"><property name="name" value="zs"></property><property name="age" value="23"></property> 
</bean><bean id="course" class="com.dt.entity.Course"><property name="courseName" value="java"></property><property name="courseHour" value="100"></property><property name="teacher" ref="teacher"></property>
</bean>

Teacher.java

package com.dt.entity;public class Teacher {private String name;private int age;public Teacher() {}public Teacher(String name, int age) {super();this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}

Course.java

 

package com.dt.entity;public class Course {private String courseName;private int courseHour;private Teacher teacher;//授课老师;依赖于Teacher类public Course() {}public Course(String courseName, int courseHour, Teacher teacher) {super();this.courseName = courseName;this.courseHour = courseHour;this.teacher = teacher;}public String getCourseName() {return courseName;}public void setCourseName(String courseName) {this.courseName = courseName;}public int getCourseHour() {return courseHour;}public void setCourseHour(int courseHour) {this.courseHour = courseHour;}public Teacher getTeacher() {return teacher;}public void setTeacher(Teacher teacher) {this.teacher = teacher;}//打印课程信息public void showInfo(){System.out.println(this.courseName+"," + this.courseHour+","+this.teacher.getName());}}

Tset.java

package com.dt.test;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.dt.entity.Course;
import com.dt.entity.Student;public class test {public static void main(String[] args) {testDI();}public static void testDI(){ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");Course course= (Course)context.getBean("course");course.showInfo();}}

第二种,构造器(构造方法注入)

通过构造方法赋值:
             constructor-arg和构造方法的参数顺序一致,若是不一致,可以通过name或index(索引)或type(类型)指定参数

<bean id="teacher" class="com.dt.entity.Teacher" ><constructor-arg value="李四"></constructor-arg><constructor-arg value="24"></constructor-arg>
</bean><bean id="course" class="com.dt.entity.Course"><constructor-arg value="c"></constructor-arg><constructor-arg value="100"></constructor-arg><constructor-arg ref="teacher"></constructor-arg>
</bean>

其他测试类和实体类不变

第三种,p命令空间赋值

        1、引入p命名空间:xmlns:p=""

        2、简单类型为p:属性名="属性值"

             引用类型:p:属性名-ref="引用的id"

<bean id="teacher" class="com.dt.entity.Teacher" p:age="25" p:name="王五">
<bean id="course" class="com.dt.entity.Course" p:courseHour="300" p:courseName="hadoop" p:teacher-ref="teacher">

其他类不变

 

个人学习心得,希望能帮助到你们,一起学习吧=.=!!!也可以去b站看颜群老师的视频讲解,挺好的

Spring——IOC(依赖注入)的三种方式

依赖注入的三种方式(注入的底层是通过反射实现的)

第一种:set方式的依赖注入

 

applicationContext.xml

id:bean的唯一标识符

class:是实体的包类名

property:name是实体类的属性名;value指的是属性值

<bean id="teacher" class="com.dt.entity.Teacher"><property name="name" value="zs"></property><property name="age" value="23"></property> 
</bean><bean id="course" class="com.dt.entity.Course"><property name="courseName" value="java"></property><property name="courseHour" value="100"></property><property name="teacher" ref="teacher"></property>
</bean>

Teacher.java

package com.dt.entity;public class Teacher {private String name;private int age;public Teacher() {}public Teacher(String name, int age) {super();this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}

Course.java

 

package com.dt.entity;public class Course {private String courseName;private int courseHour;private Teacher teacher;//授课老师;依赖于Teacher类public Course() {}public Course(String courseName, int courseHour, Teacher teacher) {super();this.courseName = courseName;this.courseHour = courseHour;this.teacher = teacher;}public String getCourseName() {return courseName;}public void setCourseName(String courseName) {this.courseName = courseName;}public int getCourseHour() {return courseHour;}public void setCourseHour(int courseHour) {this.courseHour = courseHour;}public Teacher getTeacher() {return teacher;}public void setTeacher(Teacher teacher) {this.teacher = teacher;}//打印课程信息public void showInfo(){System.out.println(this.courseName+"," + this.courseHour+","+this.teacher.getName());}}

Tset.java

package com.dt.test;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.dt.entity.Course;
import com.dt.entity.Student;public class test {public static void main(String[] args) {testDI();}public static void testDI(){ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");Course course= (Course)context.getBean("course");course.showInfo();}}

第二种,构造器(构造方法注入)

通过构造方法赋值:
             constructor-arg和构造方法的参数顺序一致,若是不一致,可以通过name或index(索引)或type(类型)指定参数

<bean id="teacher" class="com.dt.entity.Teacher" ><constructor-arg value="李四"></constructor-arg><constructor-arg value="24"></constructor-arg>
</bean><bean id="course" class="com.dt.entity.Course"><constructor-arg value="c"></constructor-arg><constructor-arg value="100"></constructor-arg><constructor-arg ref="teacher"></constructor-arg>
</bean>

其他测试类和实体类不变

第三种,p命令空间赋值

        1、引入p命名空间:xmlns:p=""

        2、简单类型为p:属性名="属性值"

             引用类型:p:属性名-ref="引用的id"

<bean id="teacher" class="com.dt.entity.Teacher" p:age="25" p:name="王五">
<bean id="course" class="com.dt.entity.Course" p:courseHour="300" p:courseName="hadoop" p:teacher-ref="teacher">

其他类不变

 

个人学习心得,希望能帮助到你们,一起学习吧=.=!!!也可以去b站看颜群老师的视频讲解,挺好的

发布评论

评论列表 (0)

  1. 暂无评论