首页 技术 正文
技术 2022年11月14日
0 收藏 507 点赞 3,168 浏览 3222 个字

目录


两个测试类

普通方式手动注入

普通方式注入的缺点

自动注入的介绍

配置自动注入的方式

配置全局自动注入

局部单独配置

利用注解实现自动注入


两个测试类

package cn.ganlixin.pojo;public class Major {
private String name;
private String category;// 此处省略了无参构造方法、有参构造方法、getter和setter、toString方法
}

  

package cn.ganlixin.pojo;public class Student {
private int id;
private String name;
private Major major; // 需要引用一个major对象// 此处省略了无参构造方法、有参构造方法、getter和setter、toString方法
}

  

普通方式注入

  上面的Student依赖于一个Major类型的对象,在创建Student这个类的对象(bean)的时候,对major属性进行注入时,需要引用一个Major类创建的bean,也就是下面这样:

<?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"> <bean id="major" class="cn.ganlixin.pojo.Major"></bean> <bean id="student" class="cn.ganlixin.pojo.Student">
<!-- 引用上面的major bean -->
<property name="major" ref="major"></property>
</bean></beans>

  

普通方式注入的缺点

  从上面的配置上来看,其实配置一个<property>好像并不困难,但是当项目稍微大一点,类中属性稍微复杂一点,那么配置利用<property>进行注入将是特别不便捷的,需要写很多的<property>,并且需要记住创建的bean的id,否则可能会引用错误。

  另外,在修改配置的时候也不方便,当一个bean的id发生改变,引用他的bean中的<property>的ref属性也要进行修改,所以,真的不方便。

自动注入的介绍

  依旧以上面这段配置为例:

<bean id="major" class="cn.ganlixin.pojo.Major"></bean><bean id="student" class="cn.ganlixin.pojo.Student">
<!-- 引用上面的major bean -->
<property name="major" ref="major"></property>
</bean>

  其实,我们发现,student这个bean中的<property>中的name和ref,值(字符串)都是一样的,如果能省略这一句,然后让student的major属性自动去找id为major的<bean>,就方便很多了。

<bean id="major" class="cn.ganlixin.pojo.Major"></bean><bean id="student" class="cn.ganlixin.pojo.Student" autowire="byName"></bean>
<!-- major属性自动去Spring容器中寻找id为major的bean,自动引用 -->

  其实这就是自动注入,只不过,我们还需要进行其他配置。

配置自动注入的方式

  配置自动注入的方式有两种,一种是全局配置,另一种是局部单独配置。

  全局配置:只配置一次,之后配置文件中的所有bean,都按照全局配置进行注入,全局配置是在<beans>标签中配置default-autowire=”Xxx”;

  局部单独配置:对于每一个bean,单独设置注入方式,单独配置是在单独的<bean>标签中配置autowire=”xxx”。

  对于全局配置和局部单独配置,都有5个值可以选择:

  1、no:当autowire设置为no的时候,Spring就不会进行自动注入,相当于不给引用属性赋值。

  2、byName:在Spring容器中查找id与属性名相同的bean,并进行注入。

  3、byType:在Spring容器中查找类型与属性名的类型相同的bean,并进行注入。

  4、constructor:仍旧是使用byName方式,只不过注入的时候,使用构造方式进行注入。

  5、default:全局配置的default相当于no,局部的default表示使用全局配置设置

配置全局自动注入

<?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"
default-autowire="byName"
>
<!-- 注意,<beans>标签中配置了全局自动注入方式 --><bean id="major" class="cn.ganlixin.pojo.Major"></bean>
<bean id="student" class="cn.ganlixin.pojo.Student"></bean>
<!-- 默认使用全局配置,全局设置为byName,所以会按照byName方式自动注入, -->
</beans>

  

局部单独配置

  使用局部单独配置就是在要创建的<bean>中设置autowire属性:

<?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"
default-autowire="no"
>
<!-- 注意,<beans>标签中配置了全局自动注入方式 --><bean id="major" class="cn.ganlixin.pojo.Major"></bean>
<bean id="student" class="cn.ganlixin.pojo.Student" autowire="byName"></bean>
<!-- 局部进行配置后,会覆盖全局配置,局部设置为byName,所以会按照byName方式自动注入, -->
</beans>

  

使用注解实现自动注入

  使用注解自动注入,可以参考:Spring 使用注解完成IoC与DI

  主要使用的是@autowired和@Resource两个注解

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297