美文网首页
Spring特性之IOC

Spring特性之IOC

作者: 小明童鞋6666 | 来源:发表于2018-04-24 16:46 被阅读0次

IOC简介

众所周知, Spring为Web开发三层架构中最重要的一环. 在业务层中, 我们往往需要根据需求处理各种各样的逻辑, 这其中就免不了对实例对象的创建.

往常, 我们会在代码中直接创建对象, 并对对象的属性进行赋值, 但按照面向对象的开放封闭原则(对扩展开放, 对修改关闭), 如果后期需要对对象的属性值进行修改, 则免不了要去直接修改代码. Spring框架则为我们提供了一种思路, 也就是通过注解或xml的方式进行配置, 然后将对象的创建与属性的赋值交由框架管理. 这种解耦合的方式称之为IOC(Inversion Of Control: 控制反转)或DI(Dependency Injection: 依赖注入).

xml方式

无参构造创建对象

application.xml中配置相关信息 :

<?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">
           <!-- 
            告诉spring需要创建哪个类的实例, 以后只要拿id就够了 
            class: 类的全路径名称
            scope: 单例还是多例 默认为单例
            --> 
    <bean id="xxx" class = "包名.类名"></bean>
</beans>

在测试代码中通过idname属性获取这个实例,

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
类名 对象名 = context.getBean("id或name");

有参构造创建对象

xml文件中的变化 :

<bean id="" class=""> 
        <constructor-arg name = "xxx" value=""></constructor-arg>
</bean>

set方式注入属性

注意: 一定要在bean中提供set方法

注入数据属于普通数据类型

xml文件中的变化 :

<bean id="" class=""> 
    <property name="" value=""></property>
</bean>  

注入数据属于数组或集合类型

xml文件中的变化 :

<bean id="" class=""> 
    <property name="" value="">
        <array>
            <value></value>
        </array>
    </property>
</bean>

集合则可以举一反三了~

注入数据属于自定义对象类型

xml文件中的变化 :

<bean id="" class="A类" >
    <property name="" ref="B类的id"></property>
</bean> 
<bean id="" class = "B类"></bean>

Web项目的处理

对于Web项目, 我们需要在web.xml文件中添加监听器, 并配置环境参数. 这样项目一旦发布, 马上会加载这个监听器, 同时加载参数.

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

在测试代码中, 则需要使用另外一种方式根据id获取对象

WebApplicationContext context = WebApplicationContextUtils.findWebApplicationContext(getServletContext());
类名 对象名 = context.getBean("id");

注解方式

注解方式与xml方式非常类似, 只不过写法不同而已.

创建对象

  1. xml文件中的变化
<?xml version="1.0" encoding="UTF-8"?>
<!-- 
    1. 导入约束
    2. 打开扫描开关 扫描给定包下的所有类 去看看是否有对应的注解
 -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="xxx"></context:component-scan>
</beans>
  1. 在被托管的类上声明Component注解, 并指定id, 如果不指定, 则默认与该类名相同(但要将首字母改成小写)
@Component(value = "")

注意
对于Web项目, Spring提供了几个作用与Component相同的注解, 使用起来效果完全一样.

action -- @Controller
业务层 -- @Service
持久层 -- @Repository

注入属性

注入数据为普通数据类型

@Value(value = "")

注入数据为自定义对象类型

@Resource(name = "")

相关文章

网友评论

      本文标题:Spring特性之IOC

      本文链接:https://www.haomeiwen.com/subject/qmvjlftx.html