Spring 依赖注入(基础)
在谈论注入编程的方式时要知道通常情况下,有三种注入方式
接口注入
通常需要依赖指定的接口,这样程序具有侵入性,Spring 放弃了这种注入的方式
setter 注入
Spring 控制反转 在我的这篇博客中举的例子就是
setter 注入
的方式。我们需要注入的属性要实现与之匹配的setter
例如:
private int age;
public void setAge(int age) {
this.age = age;
}
这样便在 bean.xml
可以直接注入了。
构造器注入
简单的说就是构造方法是有参的,且采用 Spring 的方式实现
首先有个 Student
类
public class Student {
private String name;
public Student(String name) {
this.name = name;
}
public void play() {
System.out.println(name + "在玩电脑");
}
public void work() {
System.out.println(name + "在学习");
}
}
在 bean,xml
中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<beans>
<bean id="stu" class="com.draper.Student">
<constructor-arg index="0">
<value>小俊</value>
</constructor-arg>
</bean>
</beans>
</beans>
这次的 bean 中的标签不是 <property>
了,而是 <constructor-arg>
,赋值则是直接在 <value>
的标签中写入参数就完成了,简单吧。而 index
则是保证有多个参数时,确保参数的位置不会混淆,而只有一个参数的时候可以省略
测试代码如下
public class TestApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Student stu = (Student) context.getBean("stu");
stu.play();
stu.work();
}
}
下面演示有多个参数的时候如何写
修改 Student
类
public class Student {
private String name;
private int age;
public Student(String name,int age) {
this.name = name;
this.age = age;
}
public void play() {
System.out.println(name + "在玩电脑");
}
public void work() {
System.out.println(name + "在学习");
}
}
修改 bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<beans>
<bean id="stu" class="com.draper.Student">
<constructor-arg index="0">
<value>小俊</value>
</constructor-arg>
<constructor-arg index="1">
<value>14</value>
</constructor-arg>
</bean>
</beans>
</beans>
测试代码
package com.draper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Draper_HXY on 2017/3/27 17:12.
* Email: Draper_HXY@163.com.
*/
public class TestApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Student stu = (Student) context.getBean("stu");
stu.play();
stu.work();
}
}
![](https://img.haomeiwen.com/i3445275/cd46a440a27ea93c.png)
细心地同学可能发现了,我的测试代码没有任何修改,只修改了少量的 Student
类 和 bean.xml
就可以为对象增添属性,侧面体现出 Spring
的 IoC
大大减少了代码的耦合,使程序拓展更方便。但这对于不喜欢写 xml 的同学来说是个难题 -_-#
有些同学发现我们在学习注入的时候,我们注入的怎么尽是 String, int 啊,如何注入自己的类的实例呢?别着急,接下来说的就是这个。
在 Bean 中应用其他 Bean
先在 Student
类中增加个 getName()
的方法,代码就不贴了。
新建一个 Home
类
public class Home {
private Student stu;
public void setStu(Student stu) {
this.stu = stu;
}
public void whoAtHome() {
if (stu != null) {
System.out.println(stu.getName() + "在家");
} else {
System.out.println("家里没人");
}
}
}
主要用来检测是否可以获取到对象
接下来修改 bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<beans>
<bean id="stu" class="com.draper.Student">
<constructor-arg index="0">
<value>小俊</value>
</constructor-arg>
<constructor-arg index="1">
<value>14</value>
</constructor-arg>
</bean>
<bean id="home" class="com.draper.Home">
<property name="stu">
<ref bean="stu"/>//主要在这里注入
</property>
</bean>
</beans>
</beans>
我们可以看到并不是直接在 <property>
的 value
赋值
而是在内部用一个 <ref>
的标签传入另外一个 bean
我们来验证代码吧
public class TestApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Home home = (Home) context.getBean("home");
home.whoAtHome();
}
}
![](https://img.haomeiwen.com/i3445275/84c293df1c8ad8a8.png)
可以看到依赖注入的基础部分就是这些了,会了这些可以暂时练习一些小例子。
如果想要跟着我一起学习,请关注我哦。