专业的编程技术博客社区

网站首页 > 博客文章 正文

Spring依赖注入和自动注入(spring 依赖注入)

baijin 2024-08-15 16:54:17 博客文章 6 ℃ 0 评论

1、 Spring依赖注入

1、 Set 方法注入 ,调用setXxx()方法为属性(依赖)赋值

2、 构造器注入,调用构造器时,赋值

1.1 Set注入

<!-- 依赖注入 -->
<bean name="uu" class="com.nm.spring.pojo.User">
<property name="username" value="jiajiage"/>
<property name="tel" value="110"/>
<property name="gender">
<value>男</value>
</property>
<property name="myCar" ref="xxCar"/>
<!-- <property name="myCar"> 和上行代码作用一致,写法不同
<ref bean="xxCar"/>
</property> -->
</bean>
<bean name="xxCar" class="com.nm.spring.pojo.Car" >
<property name="brand" value="奥迪"/>
<property name="name" value="A6L"/>
<property name="price" value="250000"/>
</bean>
注意: name: 需要赋值的属性名
value : 直接赋值 8大基本数据类型(含包装类) 和String
ref: 如果属性是自定义的引用数据类型使用
内部bean 配置:
<bean name="uu" class="com.nm.spring.pojo.User">
<property name="username" value="jiajiage" />
<property name="tel" value="110" />
<property name="gender">
<value>男</value>
</property>
<property name="myCar">
<bean class="com.nm.spring.pojo.Car">
<property name="brand" value="奥迪" />
<property name="name" value="A8L" />
<property name="price" value="350000" />
</bean>
</property>
</bean>

1.2构造器注入

1)构造器注入的基本使用

<!-- 构造器注入 -->
<bean name="ccCar" class="com.nm.spring.pojo.Car">
<constructor-arg name="brand" value="BMW"/>
<constructor-arg name="name">
<value>X7</value>
</constructor-arg>
<constructor-arg name="price" value="998.99" />
</bean>
说明:通过name 属性 区分参数
<bean name="ddCar" class="com.nm.spring.pojo.Car">
<constructor-arg index="1" value="X6" />
<constructor-arg index="0" value="BMW"/>
<constructor-arg index="2" value="122.99" />
</bean>
说明:通过 index 属性 区分参数

2)如果参数为引用类型,则引用其他bean赋值(ref属性)

<bean name="uu2" class="com.nm.spring.pojo.User">
<constructor-arg name="username" value="helloGod"/>
<constructor-arg name="tel" value="119"/>
<constructor-arg name="gender" value="女"/>
 <constructor-arg name="myCar" ref="ccCar"/>
</bean>
<bean name="ccCar" class="com.nm.spring.pojo.Car">
<constructor-arg name="brand" value="BMW"/>
<constructor-arg name="name">
<value>X7</value>
</constructor-arg>
<constructor-arg name="price" value="998.99" />
</bean>

1.3 集合注入

<!-- 集合注入 -->
<bean name="zsf" class="com.nm.spring.pojo.Person">
<property name="mylist">
<list>
<value>苹果</value>
<value>香蕉</value>
<value>梨子</value>
<!-- 引用其他bean -->
<ref bean="ccCar"/>
</list>
</property>
<property name="myset">
<set>
<value>宝马</value>
<value>别克</value>
<value>大众</value>
<!-- 使用内部bean -->
<bean class="com.nm.spring.pojo.Car">
<constructor-arg index="1" value="X66" />
<constructor-arg index="0" value="BMW2"/>
<constructor-arg index="2" value="122.99" />
</bean>
</set>
</property>
<property name="mymap">
<map>
<entry key="k1" value="spring"/>
<entry key="k2" value="java"/>
</map>
</proper>

2、 Spring 自动注入

2.1手动注入依赖

自动注入,就是把注入过程交个IOC容器,但是要指明注入的方式

2.2 byType 注入

byType: 指的是根据依赖的类型从IOC从中选择一个同类型的 bean注入

Exception encountered during context initialization - cancelling refresh attempt: nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.nm.spring.pojo.Car' available: expected single matching bean but found 2: cc,dd 期望1但是找到2个,容器无法确定

<bean name="cc" class="com.nm.spring.pojo.Car" autowire-candidate="false">

autowire-candidate="false" : 表示当前bean为 非候选bean,不参与注入。

autowire-candidate="true": 表示当前bean 为候选bean 参入注入

2.3 byName:注入

byName注入指的是,根据依赖的名字从IOC容器中选择 同名bena 注入。

2.4 constructor 注入

Constructor注入 指的是根据构造器的参数类型 从ioc容器中选择类型一致的注入,和byType一致,如果有多个bean同时注入人注入要求的bean则注入失败

13.5 No: 不自动注入

12.6 Default: 默认值 ,取的是NO

3、 Spring 注解配置

<!-- 开启注解配置: 开启包扫描,告诉IOC容器到哪个地方去找带有注解的类型然后注入到容器中 -->

<context:component-scan base-package="com.nm.spring.pojo"/>

使用该标签需要引入名字空间:

1

第 1 页

@Component("名字") : 组件注解,没有业务含义的注解使用,在类级别 名字可省略,如果省略,则为类名首字母小写后的类名作为名字相当于 在spring 配置文件中 使用<bean>标签注册一个对象到IOC容器

@Value("值"):为属性赋值(基本类型及其包装类型和String)

@Service("名字"):服务层组件注解,通常业务逻辑层使用,如UserServiceIml

@Repository("名字"):持久层注解,通常用于数据访问层,如UserDaoImpl

@Controller("名字"):控制器层注解,通常用于SpringMVC中的控制器类

@Autowired(required=true|false):自动注入,根据默认类型byType注入

required:表示是否为必须,如果为true, 又找不到注入的bean 则报错 默认为 true,该注解可以再字段set方法构造器上使用

@Qualifier("名字"):和@Autowired配合使用,可以根据名字注入byName

@Resource(name="bean名字"): JSR250提供的注解,根据名字注入

@Configuration:配置类,该注解表示这个类是一个配置类,其作用等价于Spring XML配置文件

@Bean("名字"):等价于 xml 中书写 <bean>注册bean


本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表