星空网 > 软件开发 > Java

Spring学习之Ioc控制反转(1)

开始之前:

1. 本博文为原创,转载请注明出处

2. 作者非计算机科班出身,如有错误,请多指正

---------------------------------------------------------------------------开始啦啦啦啦啦-------------------------------------------------------------------------------

从开始接触spring起,听到最多的就是Ioc(控制反转)和AOP(面向切面编程)啦。Spring的官方文档给出了这样一个框架图(><看起来好深奥~~)。不过本篇要介绍的所谓的控制反转,就是通过core里面的bean以及context jar包实现的哦。其他的jar包会在之后的章节中讲到。

Spring学习之Ioc控制反转(1)

控制反转,也可以称之为依赖注入(dependency injection),指的是当依赖被创建,或从工厂里获得以后,其他类可以通过构造方法,工厂方法,或set方法得到此依赖。通俗一点的说,就是一个类里面依赖的创建及使用,不需要在类本身new出此依赖,只要注入即可使用。举个例子,我有一辆汽车,需要燃油才能发动,然而我总不能自己去开采燃油,我只需要去加油站加点油,不管你是中石油还是壳牌油,总之油注入进来了,车就可以发动了。这里的燃油就是一个依赖类,这样可以被注入的依赖类,我们称之为bean,而对bean进行初始化,配置和集成操作的,我们称之为Ioc容器(类似于加油站了)。Ioc容器的编译可以使用

那么问题来啦,bean到底是咋配置的呢?下面给出了简单的bean的定义结构

<??><beans ="http://www.springframework.org/schema/beans"  ="http://www.w3.org/2001/  xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans.xsd">  <bean id="..." class="...">    <!-- collaborators and configuration for this bean go here -->  </bean>  <bean id="..." class="...">    <!-- collaborators and configuration for this bean go here -->  </bean>  <!-- more bean definitions go here --></beans>

这是一段简单的

ApplicationContext context =  new ClassPathnew String[] {"services.

这里的ApplicationContext是context jar包里面的容器接口,它读取了两个

让我们来实际操作一下,首先定义两个

service.

<??><beans ="http://www.springframework.org/schema/beans"    ="http://www.w3.org/2001/    xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans.xsd">  <!-- services -->  <bean id="petStore" class="com.dabingguozi.generaltest.pojo.PetStoreService">    <property name="accountDAO" ref="accountDAO"/>    <property name="itemDAO" ref="itemDAO"/>    <!-- additional collaborators and configuration for this bean go here -->  </bean>    <!-- more bean definitions for services go here --></beans>

 

daos.

<??><beans ="http://www.springframework.org/schema/beans"    ="http://www.w3.org/2001/    xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans.xsd">  <bean id="accountDAO"     class="com.dabingguozi.generaltest.pojo.AccountDAO">    <!-- additional collaborators and configuration for this bean go here -->  </bean>  <bean id="itemDAO" class="com.dabingguozi.generaltest.pojo.ItemDAO">    <!-- additional collaborators and configuration for this bean go here -->  </bean>  <!-- more bean definitions for data access objects go here --></beans>

最后,不要忘记了,在Java里面定义你的三个类(PetStoreService.java, ItemDAO.java, AccountDAO.java注意要包含set方法)和你的主程序(如下)

public class App {  public static void main(String[] a){    ApplicationContext context =        new ClassPathnew String[] {"daos.});// retrieve configured instance    PetStoreService service = context.getBean("petStore", PetStoreService.class);// use configured instance    List<String> userList = service.getUsernameList();    System.out.print(userList);  }}

如有报错,看看是不是jar包没有导入,本人使用maven管理lib,详见:https://mvnrepository.com/artifact/org.springframework

在Intellij里面试一试,看看是不是我们想要的效果呢? 

 Spring学习之Ioc控制反转(1)

我们成功的打出了依赖的类名。散花^^如此一来,我们的Spring之旅正式开始啦~~

 

 

下一篇:Spring学习之Ioc控制反转(2)(这不是link,戳不开2333)

下一篇继续研究控制反转中bean定义的一些内容,博主首博,诚谢观看。

 

本博客内容参考spring framework官方文档,如有冲突,请参照原版内容

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/index.html




原标题:Spring学习之Ioc控制反转(1)

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流