Spring容器的开启与关闭

第一步:创建Maven工程

 

第二步:在pom.xml中写入spring的jar包导入代码

  <dependencies>
              <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>4.1.3.RELEASE</version>
            </dependency>
  </dependencies>

第三步:查看Maven jar包是否正常生成

第四步:在resources下创建一个applicationContext.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"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

    <bean id="a" class="aoo.Aoo"/>
</beans>

第五步:创建一个类  

第六步:运行程序

第七步:实现一些功能

创建一个类,在aoo包下  类名为Aoo

里面写一个无参构造方法

在我们第四步中的ApplicationContext.xml中配置

加上

id属性:在整个文件中需要唯一,class是类的全名

再次运行的时候,发现Aoo中的无参构造输出语句出现了。

所以:加载配置文件的时候会自动生成类的实例

其他功能:init-method="init"     --- 初始化类实例的时候会加载这个名字的方法
                    destroy-method="destroy"  --Spring容器关闭的时候执行这个名字的方法
                    scope="singleton"/>  --默认的生成方式是单例模式  --且应该是饿汉模式

                   lazy-init="true"     延迟加载,就是容器启动的时候不创建类的实例,懒汉模式 

Spring框架中,如果你不希望Spring容器在应用程序结束时不自动关闭,你可以通过以下几种方式来管理: 1. **手动控制ApplicationContext**: 只在需要的时候创建和销毁ApplicationContext,而不是在整个应用生命周期内都保持打开状态。例如,可以使用`ConfigurableApplicationContext`的`refresh()`方法启动应用,然后在完成后调用`close()`方法。 ```java ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // ... 应用程序逻辑 context.close(); // 关闭上下文 ``` 2. **使用bean的scope属性**: 设置Bean的作用域(如`Singleton`、`Prototype`、`RequestScoped`等),这样Spring会在每个请求或事务结束时自动清理不再使用的bean,而不会立即关闭整个容器。比如,使用`@Scope("prototype")`来创建单例之外的实例。 3. **避免使用自动装配**: 如果你在配置文件中开启了自动装配(`autowire`默认为`byName`),则某些Bean在不再使用时可能会被自动移除。减少自动装配并手动管理依赖可以控制Bean的生存期。 4. **设置`ApplicationContext`的生命周期**: 使用`DisposableBean`接口或`Closeable`接口,允许你在bean被终止时执行清理操作,但不是强制关闭整个容器。 记得在处理完所有资源后,适时关闭ApplicationContext,以释放系统资源。如果不需要长期运行的Spring容器,考虑使用`@WebApplicationInitializer`或Spring Boot Actuator的`/shutdown`端点来优雅地停止服务。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值