详谈Spring对IOC的理解(推荐篇)

2025-05-29 0 50

一、ioc控制反转和di依赖注入

1.控制反转,字面可以理解为:主动权的转移,原来一个应用程序内的对象是类通过new去主动创建并实例化的,对对像创建的主动权在程序代码中。程序不仅要管理业务逻辑也要管理对的象创建和依赖关系。这是很累的,也跟软件工程 "低耦合高内聚" 的概念不十分符合。

详谈Spring对IOC的理解(推荐篇)

有了spring的ioc容器之后,对象的实例化和依赖关系管理都由ioc容器进行统一管理,主体类只要依赖ioc容器就够了,需要啥,容器会给他注入进去,也就是只要声明对象不用再主动去new,ioc容器帮忙把相应的对象注入到声明对象中,使其变成实例化对象。(类似主体类提供一个躯体,ioc容器把灵魂注入进去,使其变成一个生命体,激活他),这样创建对象的主动权就转移交接了,

  详谈Spring对IOC的理解(推荐篇)

二、使用xml配置方式实现ioc

1.在ioc容器中配置了dao实现类和service类的bean,在容器加载的时候就会实例化这些bean到内存中。(bean.xml配置如下)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
<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

">

<!-- bookdao bean -->

<bean id="bookdao" class="com.study.daoimpl.bookdaoimpl"></bean>

<!-- bookservice bean-->

<bean id="bookservice" class="com.study.service.bookservice">

<!-- bookservice中的声明了bookdao对象,通过ref属性将bookdao的bean注入到对象中 -->

<property name="bookdao" ref="bookdao"/>

</bean>

</beans>

2. service类中需要用到dao类的实例(正常情况下需要new一个dao类对象),但是用ioc容器接管后只需要声明dao接口对象即可,然后写一个dao对象的set方法。(要注入的对象必须要有set方法,否则将报错 bean property 'bookdao' is not writable or has an invalid setter method)因为spring注入是根据反射机制实现的,他在反射注入的时候会调用该方法名的set方法,如果set方法写错,或者根本没写,那么注入就会失败。(bookservice类如下)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17
public class bookservice {

private bookdao bookdao;

public bookservice() {

system.out.println("bookservice实例化");

}

public void setbookdao(bookdao bookdao) {

system.out.println("bookservice属性初始化装配成功");

this.bookdao = bookdao;

}

public void storebook(string bookname){

system.out.println("图书上架");

system.out.println(bookdao.addbook(bookname));

}

}

如上代码:bookserivce类需要用到bookdao对象,但是却没有new对象,只有一个set方法,这个set方法就是ioc容器注入的入口(必不可少),

3.此处我们用applicationcontext作为容器,初始化配置文件,然后从容器中根据id名取出容器中已经帮我们实例化好的对象。

?

1

2

3

4

5

6

7

8

9

10

11
public class testdmeo {

bookservice bookservice;

@test

public void teststorebook(){

system.out.println("容器初始化");

applicationcontext app = new classpathxmlapplicationcontext("bean.xml");

bookservice = (bookservice) app.getbean("bookservice");//将对象注入到声明好的bookservice对象中。(bookservice就是配置文件中的id)

bookservice.storebook("spring mvc");

}

}

 详谈Spring对IOC的理解(推荐篇)

4.dao类和实现类如下:

接口类:

?

1

2

3
public interface bookdao {

public string addbook(string bookname);

}

实现类:

?

1

2

3

4

5

6

7

8

9

10
public class bookdaoimpl implements bookdao {

public bookdaoimpl() {

system.out.println("bookdao实例化");

}

public string addbook(string bookname) {

return bookname+"添加成功";

}

}

5.运行测试结果:

详谈Spring对IOC的理解(推荐篇)

6.大体思路如下图:

详谈Spring对IOC的理解(推荐篇)

程序中除了初始化容器用了new对象,其余的基本没有new的存在。

二、注解方式配置ioc

注解配置方式目的和xml配置的目的一样,都是为了实现bean的创建。常用的注解如下:

@component 在类定义之前添加@component注解,他会被spring容器识别,并转为bean。

@repository 对dao实现类进行注解 (特殊的@component)

@service 用于对业务逻辑层进行注解, (特殊的@component)

@controller 用于控制层注解 , (特殊的@component)

装配注解如下:

@autowired 默认按照类型装配注入,想按照名称来装配的话要结合@quapfier(“name”)一起使用,使用@autowired注解可以不用set方法。@autowired 注释进行自动注入时,spring 容器中匹配的候选 bean 数目必须有且仅有一个

@quapfier("name") 中的name是bean的名字,也就是id,和@autowired可以作为限定专配对象的名称

@resource 默认按照名称装配注入,当找不到对应名成的bean的时候就按照类型匹配,如果还是找不到的话就会报错,@autowired是spring提供的,@resource是javaee提供,使用@resource可以减少对spring的依赖

范例:

1.例子同上,只是配置bean的方式从xml文件中转移到了代码中,用注解体现。

详谈Spring对IOC的理解(推荐篇)

2.除了把配置文件中<bean id="" class=""/>变成对应的注解外,另外一个区别在于,bean.xml文件中的修改,需要做如下,配置才能够使注解生效

详谈Spring对IOC的理解(推荐篇)

context的配置有如下方法:

1.仅扫描特定包下的特定类

?

1
<context:component-scan base-package="com.study" resource-pattern="service/b*.class"/>

这是扫描service包下b开头的所有类。

2.使用<context:include-filter …/>和<context:exclude-filter …/>配置那些需要和不需要的扫描的包

filter type examples expression description
annotation org.example.someannotation 符合someannoation的target class(注解类)
assignable org.example.someclass 指定class或interface的全名(指定确切的类或接口)
aspectj org.example..*service+ aspectj语法
regex org\\.example\\.default.* regelar expression  (正则表达式)
custom org.example.mytypefilter spring3新增自定义type,org.springframework.core.type.typefilter
?

1

2

3

4

5

6

7
<!-- 容器扫描包下的注解配置组件 -->

<context:component-scan base-package="com.study" use-default-filters="false">

<context:include-filter type="aspectj" expression="com.study.service.*"/> <!-- 模糊过滤 -->

<context:include-filter type="annotation" expression="org.springframework.stereotype.component"/><!-- 过滤指定的注解 -->

<context:include-filter type="assignable" expression="com.study.service.bookservice"/><!-- 过滤指定的类或接口,路径要完整,如果是接口的话,所有派生类都会被过滤 -->

<context:include-filter type="regex" expression="com.*"/>

</context:component-scan>

<context:exclude-filter ../>要配在<context:include-filter …/>的后面。

以上这篇详谈spring对ioc的理解(推荐篇)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。

原文链接:http://www.cnblogs.com/caijh/p/7688044.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 详谈Spring对IOC的理解(推荐篇) https://www.kuaiidc.com/114509.html

相关文章

发表评论
暂无评论