文章目录
Spring中依赖注入的方法
基于构造方法的注入
基于构造方法的注入:可以带参,也可以不带参,以下展示的是带参的
首先新建一个StudentConstructor类,里面带有一个带参的构造方法
package com.zhbit.pojo;
public class StudentConstructor {
private String name;
private String sex;
private String studentNo;
private int age;
public StudentConstructor(String name, String sex, String studentNo, int age) {
this.name = name;
this.sex = sex;
this.studentNo = studentNo;
this.age = age;
}
@Override
public String toString() {
return "StudentConstructor{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", studentNo='" + studentNo + '\'' +
", age=" + age +
'}';
}
}
spring的构造器注入方法主要分为三种:1.根据索引赋值,索引都是以0开始 2.根据参数的名字传值 3.根据所属类型传值
下面对这三种方法进行简单分析。
根据索引赋值
配置constructor-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.xsd">
<!--采用构造器的index-value方式注入 -->
<bean id="studentConstructor" class="com.zhbit.pojo.StudentConstructor">
<constructor-arg index="0" value="小明"/>
<constructor-arg index="1" value="男"/>
<constructor-arg index="2" value="S001"/>
<constructor-arg index="3" value="23"/>
</bean>
</beans>
编写测试类MyConstructorBeanTest.class
import com.zhbit.pojo.StudentConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyConstructorBeanTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("constructor-beans.xml");
StudentConstructor StudentConstructor = (StudentConstructor) context.getBean("studentConstructor");
System.out.println(StudentConstructor);
}
}
测试结果:
根据所属类型传值
配置constructor-beans.xml
<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">
<!--<!–采用构造器的index-value方式注入 –>
<bean id="studentConstructor" class="com.zhbit.pojo.StudentConstructor">
<constructor-arg index="0" value="小明"/>
<constructor-arg index="1" value="男"/>
<constructor-arg index="2" value="S001"/>
<constructor-arg index="3" value="23"/>
</bean>-->
<!-- 使用构造器的name-value方式注入 该办法通过反射实现注入-->
<bean id="studentConstructor" class="com.zhbit.pojo.StudentConstructor">
<constructor-arg name="name" value="小红"/>
<constructor-arg name="age" value="18"/>
<constructor-arg name="sex" value="女"/>
<constructor-arg name="studentNo" value="160202103483"/>
</bean>
</beans>
编写测试类MyConstructorBeanTest.class
import com.zhbit.pojo.StudentConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyConstructorBeanTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("constructor-beans.xml");
StudentConstructor StudentConstructor = (StudentConstructor) context.getBean("studentConstructor");
System.out.println(StudentConstructor);
}
}
测试结果
根据所属类型传值(不推荐)
不推荐的原因:一个类可以有好几个相同基本类型的变量,这样会混淆,这里就不做详细说明了。
基于setter注入
编写StudentSetter.class
package com.zhbit.pojo;
public class StudentSetter {
private String name;
private String sex;
private String studentNo;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "StudentSetter{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", studentNo='" + studentNo + '\'' +
", age=" + age +
'}';
}
}
这里面的每一个属性的set方法必须有,没有的话会在xml文件里面报错
接下来就是setter-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.xsd">
<!--通过setter方法注入属性-->
<bean id="studentSetter" class="com.zhbit.pojo.StudentSetter" >
<property name="name" value="小橙"/>
<property name="age" value="18"/>
<property name="sex" value="男"/>
<property name="studentNo" value="123"/>
</bean>
</beans>
编写测试类
import com.zhbit.pojo.StudentSetter;
import com.zhbit.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MySetterTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("setter-beans.xml");
StudentSetter studentSetter = (StudentSetter) context.getBean("studentSetter");
System.out.println(studentSetter);
}
}
测试结果:
基于接口的注入(不常用,这里不说明了)
拓展方式注入
P命名空间注入(基于setter方法注入)
编写UserP.class类
package com.zhbit.pojo;
public class UserP {
private String name;
private int 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;
}
@Override
public String toString() {
return "UserP{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
编写userPBeans.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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p命名空间注入,可以直接注入属性的值:property-->
<bean id="userP" class="com.zhbit.pojo.UserP" p:name="小明" p:age="18">
</bean>
</beans>
其中
xmlns:p="http://www.springframework.org/schema/p"
是用来导入P-namespace命令
编写测试类MyUserPTest.class
import com.zhbit.pojo.UserP;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyUserPTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("userPBeans.xml");
UserP userP = context.getBean("userP",UserP.class);
System.out.println(userP.toString());
}
}
测试结果:
C命名空间注入(基于控制器注入)
编写UserC.class
package com.zhbit.pojo;
public class UserC {
private String name;
private int age;
public UserC() {
}
public UserC(String name, int age) {
this.name = name;
this.age = age;
}
}
编写userCBeans.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"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--c命名空间注入,通过构造器注入:constructor-arg -->
<bean id="userC" class="com.zhbit.pojo.UserC" c:age="18" c:name="拼命三郎"/>
</beans>
其中
xmlns:c="http://www.springframework.org/schema/c"
为C命名空间的导入
编写测试类MyUserCTest.class
import com.zhbit.pojo.UserC;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyUserCTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("userCBeans.xml");
UserC userC = context.getBean("userC", UserC.class);
System.out.println(userC.toString());
}
}
测试结果:
注意点:p命名和c命名空间不能直接使用,需要导入xml约束,可以到spring官网查找
来源:CSDN
作者:HONHoChi
链接:https://blog.csdn.net/HONHoChi/article/details/103697942