您的位置:首页 > 编程语言 > Java开发

4.spring 依赖注入的三种方式

2017-12-12 22:33 323 查看
Spring依赖注入的方式(3种):

1.属性注入:是通过setter方法注入Bean的属性值或依赖的对象(使用多)
方法:<property name=" " value=" " ref=" ">

<bean id="userInfo" class="com.UserInfo" scope="singleton">
<property name="userName" value="悟空"></property>
<property name="userId" value="1001"></property>
<property name="department" ref="department"></propety>

</bean>

2.构造器注入:是通过构造方法注入Bean的属性值或依赖的对象(使用少):是通过构造方法注入Bean的属性值或依赖的对象

第一步:在类中需要生成构造方法

第二步:在bean.xml中

<!--部门的bean 构造方法注入-->
方法:<constructor-arg>

1.按索引下标匹配参数
<!--按索引下标来匹配参数>
<bean id="department" class="com.Department">
<constructor-arg index="0" value="102"/>
<constructor-arg index="1" value="技术部"/>
</bean>
2.按参数类型匹配参数

<!--按参数类型配置参数-->
<bean id="userInfo" class="com.UserInfo">
<constructor-arg value="1002" type="java.lang.Integer"/>
<constructor-arg value="八戒" type="java.lang.String"/>
<constructor-arg value="department" type="com.Department"/>
</bean>

3.接口(工厂方法注入)(极少使用)

实例1:属性注入

1.结构目录:



代码:

Department.java

package com;

public class Department {
private Integer deId;
private String deName;

public void setDeId(Integer deId) {
this.deId = deId;
}
public void setDeName(String deName) {
this.deName = deName;
}
@Override
public String toString() {
return "Department [deId=" + deId + ", deName=" + deName + "]";
}

}


UserInfo.java
package com;

public class UserInfo {

private Integer userId;
private String userName;
private Department department;

public void setUserId(Integer userId) {
this.userId = userId;
}

public void setUserName(String userName) {
this.userName = u
4000
serName;
}

public void getUserName() {
System.out.println("姓名1:"+userName);
}

public void setDepartment(Department department) {
this.department = department;
}

@Override
public String toString() {
return "UserInfo [userId=" + userId + ", userName=" + userName + ", department=" + department + "]";
}

}


Test.java
package test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.UserInfo;

public class Test {

public static void main(String[] args) {
BeanFactory bf=new ClassPathXmlApplicationContext("beans.xml");
UserInfo user=(UserInfo)bf.getBean("userInfo");

System.out.print(user);//UserInfo [userId=1, userName=悟空, department=Department [deId=101, deName=技术部]]

user.getUserName();//姓名1:悟空

}
}


beans.java

<?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-3.0.xsd">
<!-- 部门的bean -->
<bean id="department" class="com.Department">
<property name="deId" value="101"></property>
<property name="deName" value="技术部"></property>
</bean>

<!-- UserInfo的bean -->
<!--
UserInfo userInfo=new UserInfo();
userInfo.setUserName("悟空");
-->
<bean id="userInfo" class="com.UserInfo">
<property name="userId" value="001"/> <!-- 属性值注入 -->
<property name="userName" value="悟空"/> <!-- 属性值注入 -->
<property name="department" ref="department"></property> <!-- 对象注入 -->
</bean>

</beans>

打印结果:
UserInfo [userId=1, userName=悟空, department=Department [deId=101, deName=技术部]]姓名1:悟空

实例2:构造器注入

1.结构目录



2.代码

Department.java

package com;

public class Department {
private Integer deId;
private String deName;

public void setDeId(Integer deId) {
this.deId = deId;
}
public void setDeName(String deName) {
this.deName = deName;
}

//构造方法,无参和有参
public Department() {};
public Department(Integer deId, String deName) {
super();
this.deId = deId;
this.deName = deName;
}

@Override
public String toString() {
return "Department [deId=" + deId + ", deName=" + deName + "]";
}

}


Test.java
package test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.Department;

public class Test {

public static void main(String[] args) {
BeanFactory bf=new ClassPathXmlApplicationContext("beans.xml");
Department de=(Department)bf.getBean("department");

System.out.print(de);

}
}


beans.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-3.0.xsd">
<!-- 部门的bean -->
<!-- 1.按索引下标匹配参数 -->
<!--
<bean id="department" class="com.Department">
<constructor-arg index="0" value="101"/>
<constructor-arg index="1" value="研发部"/>
</bean>
-->

<!-- 2.按参数类型匹配参数 -->
<bean id="department" class="com.Department">
<constructor-arg value="八戒" type="java.lang.String"></constructor-arg>
<constructor-arg value="110" type="java.lang.Integer"></constructor-arg>
</bean>
</beans>

打印结果:
Department [deId=110, deName=八戒]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: