Spring常用的三种注入方式+{集合注入(set注入一种)}

您所在的位置:网站首页 autowired没有set也能注入 Spring常用的三种注入方式+{集合注入(set注入一种)}

Spring常用的三种注入方式+{集合注入(set注入一种)}

2024-06-24 01:21| 来源: 网络整理| 查看: 265

Spring常用的三种注入方式\集合注入 三种注入方式1、构造方法注入2、set方法注入3、注解4、集合注入

三种注入方式

Spring通过DI(依赖注入)实现IOC(控制反转),常用的注入方式主要有三种:构造方法注入,setter注入,基于注解的注入。 注意:构造方法注入和set注入都是通过java的反射技术得以实现的。 项目整体结构 在这里插入图片描述 链接: 代码下载.

1、构造方法注入

这种注入方式是通过构造方法来实现,类中必须提供构造方法,属性的set方法不需要。

package com.zhang.service; public class Car { private String brand; private float price; @Override public String toString() { return "Car:"+this.brand+"\t"+this.price; } public Car() { } public Car(String brand, float price) { this.brand = brand; this.price = price; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } }

spring-beans.xml配置文件 可以用形参名字注入,也可以根据构造方法形参的索引位置注入。

test2测试类

@Test public void test2(){ ApplicationContext ac = new ClassPathXmlApplicationContext("spring-beans.xml"); Car car = (Car)ac.getBean("car"); System.out.println(car.toString());; }

测试结果 请添加图片描述

2、set方法注入

这种注入方式通过set方法完成注入,所以在类中必须要给属性设定set方法。

package com.zhang.service; import org.springframework.beans.factory.annotation.Autowired; public class Person { String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }

配置文件

测试类

@Test public void test3(){ ApplicationContext ac = new ClassPathXmlApplicationContext("spring-beans.xml"); Person person = (Person)ac.getBean("person"); System.out.println("Person:"+person.getName()); }

测试结果 请添加图片描述

3、注解

通过@Autowired注解方式,可以实现自动装配,只要在对应的属性上面添加该注解进来,但是**@Autowired注解是按照byType类型来注入**。 在介绍注解注入的方式前,先简单了解bean的一个属性autowire,autowire主要有三个属性值:constructor,byName,byType。

constructor:通过构造方法进行自动注入,spring会匹配与构造方法参数类型一致的bean进行注入,如果有一个多参数的构造方法,一个只有一个参数的构造方法,在容器中查找到多个匹配多参数构造方法的bean,那么spring会优先将bean注入到多参数的构造方法中。

byName:被注入bean的id名必须与set方法后半截匹配,并且id名称的第一个单词首字母必须小写,这一点与手动set注入有点不同。

byType:查找所有的set方法,将符合符合参数类型的bean注入。

(1)@Autowired默认按照byType方式进行bean匹配,@Resource默认按照byName方式进行bean匹配

(2)@Autowired是Spring的注解,@Resource是J2EE的注解,这个看一下导入注解的时候这两个注解的包名就一清二楚了

Spring属于第三方的,J2EE是Java自己的东西,因此,建议使用@Resource注解,以减少代码和Spring之间的耦合。 在Person类上面加入Car汽车类,并在上面添加@Autowired注解。

@Autowired的原理 其实在启动spring IoC时,容器自动装载了一个AutowiredAnnotationBeanPostProcessor后置处理器,当容器扫描到@Autowied、@Resource(是CommonAnnotationBeanPostProcessor后置处理器处理的)或@Inject时,就会在IoC容器自动查找需要的bean,并装配给该对象的属性。

如果需要指定具体的名称,利用Qualifier

@Autowired @Qualifier("userJdbcImps") private UserRepository userRepository; package com.zhang.service; import org.springframework.beans.factory.annotation.Autowired; public class Person { String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Autowired private Car car; public String getCarInfo(){ return "Car:"+car.getBrand()+car.getPrice(); } }

配置文件

测试类

@Test public void test4(){ ApplicationContext ac = new ClassPathXmlApplicationContext("spring-beans.xml"); Person person = (Person)ac.getBean("person"); System.out.println("Person:"+person.getName()); System.out.println(person.getCarInfo()); }

测试结果 请添加图片描述

4、集合注入

Java中的集合主要有:List,Set,Map,此外还有个Properties

package com.zhang.service; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class Collection { private Map map; private Set Set; private List list; private Properties pro; public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } public Set getSet() { return Set; } public void setSet(Set set) { Set = set; } public List getList() { return list; } public void setList(List list) { this.list = list; } public Properties getPro() { return pro; } public void setPro(Properties pro) { this.pro = pro; } }

配置类

list1 list2 list3 set1 set2 set3 one two three one two three

每一个集合类都有它固定的注入格式。每个property的name属性必须要跟DI类中申明的属性名要一致。 测试类

@Test public void test5(){ ApplicationContext ac = new ClassPathXmlApplicationContext("spring-beans.xml"); Collection collection = (Collection)ac.getBean("collection"); // 打印这些集合 System.out.println(collection.getList()); System.out.println(collection.getSet()); System.out.println(collection.getMap()); System.out.println(collection.getPro()); }

测试结果 请添加图片描述 参考文章 https://blog.csdn.net/jiangjunlanzhoulan/article/details/81259744 https://baijiahao.baidu.com/s?id=1697723453816285295&wfr=spider&for=pc



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3