Spring IoC 容器和 bean 对象


  • 程序的耦合性:

    耦合性(Coupling),又叫耦合度,是对模块间关联程度的度量。耦合的强弱取决于模块间接口的复杂性、调用模块的方式以及通过界面传送数据的多少。模块间的耦合度是指模块之间的依赖关系,包括控制关系、调用关 系、数据传递关系。模块间联系越多,其耦合性越强,同时表明其独立性越差( 降低耦合性,可以提高其独立 性)。

  • I o C容器

    IoCInversion of Control的缩写,即控制反转的意思,是解决程序耦合性的一种思想。通常创建对象的时候使用new关键字,控制权由程序员控制,而"控制反转"是指new实例工作不由程序员来做而是交给Spring容器来做。解决程序耦合性的办法有很多,而依赖注入(DI)这一功能就是IOC思想的一种具体实现

  • Spring pom.xml配置:

    <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.2.0.RELEASE</version>
            </dependency>
    </dependencies>
  • Spring 项目结构依赖:

  • Spring bean.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">
    
      //id 是自己定义的一个名字,相当于取一个别名 ,class是需要访问实现类的全限定类名
        <bean id="daoService" class="com.Test.Dao.DaoServiceImpl"></bean>
      <bean id="userService" class="com.Test.Service.UserServiceImpl"></bean>
    </beans>
  • 入门案例:

    import com.Test.Dao.DaoService;
    import com.Test.Service.UserService;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MyTest {
    
        public static void main(String[] args){
    
            ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
            //daoService,userService就是之前bean.xml文件里的id
            DaoService dao = (DaoService)context.getBean("daoService");
            UserService user = (UserService)context.getBean("userService");
    
            System.out.println(dao);
            System.out.println(user);
    
        }
    }

    Application Context 是 spring 中较高级的容器,它采用立即加载的策略。而BeanFactory是采用延迟加载的策略,但是也可以加载配置文件中定义的 bean,将所有的 bean 集中在一起,当有请求的时候分配 bean。ApplicationContext包含 BeanFactory所有的功能,一般情况下,相对于 BeanFactoryApplicationContext会更加优秀。

  • ApplicationContext的三个实现类:

    1.ClassXmlPathApplicationContext:加载类路径下的配置文件,不需要提供 XML 文件 的完整路径,只需正确配置 CLASSPATH 环境变量即可,因为,容器会从CLASSPATH 中搜索 bean 配置文件。

    2.FileSystemXmlApplicationContext:加载磁盘任意路径下的配置文件。

    3.AnnotationConfingApplictionContext:用于读取注解创建容器。


  • 创建bean的三种方式:

    1.使用默认构造函数创建:

     <bean id="daoService" class="com.Test.Dao.DaoServiceImpl"></bean>

    2.(使用别人创建好的类时)使用普通工厂中的方法创建对象(或使用某个类中的方法创建对象):

    import com.Test.Service.UserService;
    import com.Test.Service.UserServiceImpl;
    
    public class InstanceFactory {
        public UserService getUserService(){
            return new UserServiceImpl();
        }
    }

    bean配置:

    <bean id="userService" factory-bean="instanceFactory" factory-method="getUserService">

    3.使用工厂中的静态方法创建bean对象:

    import com.Test.Service.UserService;
    import com.Test.Service.UserServiceImpl;
    
    public class InstanceFactory {
        public static UserService getUserService(){
            return new UserServiceImpl();
        }
    }
    <bean id="userService" class="com.Test.Factory.StaticInstanceFactory" factory-method="getUserService"></bean>
  • bean的作用域:

当在 Spring 中定义一个 bean 时,你必须声明该 bean 的作用域的选项。例如,为了强制 Spring 在每次需要时都产生一个新的 bean 实例,你应该声明 bean 的作用域的属性为 prototype。同理,如果你想让 Spring 在每次需要时都返回同一个bean实例,你应该声明 bean 的作用域的属性为 singleton

Spring 框架支持以下五个作用域,分别为singleton、prototype、request、session和global session,5种作用域说明如下所示,

注意,如果你使用 web-aware ApplicationContext 时,其中三个是可用的。

作用域 描述
singleton 在spring IoC容器仅存在一个Bean实例,Bean以单例方式存在,默认值
prototype 每次从容器中调用Bean时,都返回一个新的实例,即每次调用getBean()时,相当于执行newXxxBean()
request 每次HTTP请求都会创建一个新的Bean,该作用域仅适用于WebApplicationContext环境
session 同一个HTTP Session共享一个Bean,不同Session使用不同的Bean,仅适用于WebApplicationContext环境
global-session 一般用于Portlet应用环境,该运用域仅适用于WebApplicationContext环境

singleton 作用域:

singleton 是默认的作用域,也就是说,当定义 Bean 时,如果没有指定作用域配置项,则 Bean 的作用域被默认为 singleton。

当一个bean的作用域为Singleton,那么Spring IoC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。

也就是说,当将一个bean定义设置为singleton作用域的时候,Spring IoC容器只会创建该bean定义的唯一实例。

Singleton是单例类型,就是在创建起容器时就同时自动创建了一个bean的对象,不管你是否使用,他都存在了,每次获取到的对象都是同一个对象。注意,Singleton作用域是Spring中的缺省作用域。你可以在 bean 的配置文件中设置作用域的属性为 singleton,如下所示:

<!-- A bean definition with singleton scope -->
<bean id="..." class="..." scope="singleton">
    <!-- collaborators and configuration for this bean go here -->
</bean>

prototype 作用域

当一个bean的作用域为Prototype,表示一个bean定义对应多个对象实例。Prototype作用域的bean会导致在每次对该bean请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法)时都会创建一个新的bean实例。Prototype是原型类型,它在我们创建容器的时候并没有实例化,而是当我们获取bean的时候才会去创建一个对象,而且我们每次获取到的对象都不是同一个对象。根据经验,对有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域。

为了定义 prototype 作用域,你可以在 bean 的配置文件中设置作用域的属性为 prototype,如下所示:

<!-- A bean definition with singleton scope -->
<bean id="..." class="..." scope="prototype">
   <!-- collaborators and configuration for this bean go here -->
</bean>

​ ----相关参考见w3cschool文档

猜你喜欢

转载自www.cnblogs.com/coding-996/p/12288014.html