本文主要是介绍Spring四大注入方式,set注入、构造函数注入、p名称空间注入、spel注入,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
简单解释一下注入:就是给对象赋值(直接给出代码,解释全部在代码里面 )
前提是你已经配置好了spring的环境,如果还不会配置可以去看我的入门spring配置
https://blog.csdn.net/tomwildboar/article/details/80913681
1、所用到的实体类
1-1 car类
package spring.bean;public class Car {private String color;private String name;public String getColor() {return color;}public void setColor(String color) {this.color = color;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Car [color=" + color + ", name=" + name + "]";}}
1-2 User类
package spring.bean;public class User {private String name;private int age;private Car car;public User() {super();}public User(String name, Car car) {this.name = name;this.car = car;}public Car getCar() {return car;}public void setCar(Car car) {this.car = car;}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;}@Overridepublic String toString() {return "User [name=" + name + ", age=" + age + ", car=" + car + "]";}
}
2、applicationContext.xml (核心)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "><!--beans 第三行是如果要使用p名称空间才需要加上 --><!-- 如果看不到bena中的基本配置,可以去看我的spring入门配置,里面有详解 --><!-- set方式注入 --><bean name="user" class="spring.bean.User"><!-- 值类型注入 : 为user对象中名为name的属性注入tom作为值 --><property name="name" value="tom"></property><property name="age" value="19"></property><!-- 引用类型注入 : 为car属性注入下面的car对象 --><property name="car" ref="car"></property></bean><!-- 将car对象配置到容器中 --><bean name="car" class="spring.bean.Car"><property name="name" value="大众"></property><property name="color" value="黄色"></property></bean><!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<!-- 构造函数注入 (必须提供相对应的构造方法) --><bean name="user2" class="spring.bean.User"><!-- name属性:构造函数的参数名index属性:构造函数的参数索引type属性:构造函数的参数类型index:主要是解决这样的情况public User(String name, Car car) {this.name = name;this.car = car;}public User(Car car, String name) {this.name = name;this.car = car;}type:主要是解决这样的情况public User(String name, Car car) {this.name = name;this.car = car;}public User(Integer name, Car car) {this.car = car;}--><constructor-arg name="name" value="jerry" index="" type=""></constructor-arg><constructor-arg name="car" ref="car" index=""></constructor-arg></bean><!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --><!-- p名称空间注入值类型:p : 属性名 = "值"对象类型: p : 属性名-ref="bean名称" --><bean name="user3" class="spring.bean.User" p:name="jack" p:age="18" p:car-ref="car"></bean><!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --><!-- spel注入: spring Expression Language spring表达式语言--><bean name="user4" class="spring.bean.User" ><property name="name" value="#{user.name}"></property><property name="age" value="#{user3.age}"></property><property name="car" ref="car"></property></bean></beans>
3、Demo测试
package spring.injection;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import spring.bean.User;public class Demo {@Testpublic void fun1(){ApplicationContext ac = new ClassPathXmlApplicationContext("spring/injection/applicationContext.xml");User u = (User) ac.getBean("user"); System.out.println(u);}//构造函数注入@Testpublic void fun2(){ApplicationContext ac = new ClassPathXmlApplicationContext("spring/injection/applicationContext.xml");User u = (User) ac.getBean("user2"); System.out.println(u);}//p名称空间注入 @Testpublic void fun3(){ApplicationContext ac = new ClassPathXmlApplicationContext("spring/injection/applicationContext.xml"); User u = (User) ac.getBean("user3"); System.out.println(u); }//spel注入@Testpublic void fun4(){ApplicationContext ac = new ClassPathXmlApplicationContext("spring/injection/applicationContext.xml"); User u = (User) ac.getBean("user4");System.out.println(u);}}
代码亲测有效
这篇关于Spring四大注入方式,set注入、构造函数注入、p名称空间注入、spel注入的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!