日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring中的Spring JSR-250 注释

發(fā)布時(shí)間:2023/12/19 javascript 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring中的Spring JSR-250 注释 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Spring還使用基于 JSR-250 注釋,它包括 @PostConstruct, @PreDestroy 和 @Resource 注釋。

@PostConstruct 和 @PreDestroy 注釋

為了定義一個(gè) bean 的安裝和卸載,我們使用?init-method?和/或?destroy-method?參數(shù)簡(jiǎn)單的聲明一下?。init-method 屬性指定了一個(gè)方法,該方法在 bean 的實(shí)例化階段會(huì)立即被調(diào)用。同樣地,destroy-method 指定了一個(gè)方法,該方法只在一個(gè) bean 從容器中刪除之前被調(diào)用。

你可以使用?@PostConstruct?注釋作為初始化回調(diào)函數(shù)的一個(gè)替代,@PreDestroy?注釋作為銷毀回調(diào)函數(shù)的一個(gè)替代,看一個(gè)具體的例子來學(xué)習(xí)。

HelloWorld:

package com.sap;import javax.annotation.*;public class HelloWorld {private String message;public void setMessage(String message){this.message = message;}public String getMessage(){System.out.println("Your Message : " + message);return message;}@PostConstructpublic void init(){System.out.println("Bean is going through init.");}@PreDestroypublic void destroy(){System.out.println("Bean will destroy now.");}}

在Main.app里注冊(cè)一個(gè)關(guān)閉鉤?registerShutdownHook()?方法,該方法在 AbstractApplicationContext 類中被聲明。

import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp {public static void main(String[] args) {AbstractApplicationContext context =new ClassPathXmlApplicationContext("Beans.xml");HelloWorld obj = (HelloWorld) context.getBean("helloWorld");obj.getMessage();context.registerShutdownHook();}}

Beans.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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><context:annotation-config/><bean id="helloWorld"class="com.sap.HelloWorld"init-method="init" destroy-method="destroy"><property name="message" value="Hello World!"/></bean></beans>

輸出:

總結(jié)

以上是生活随笔為你收集整理的Spring中的Spring JSR-250 注释的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。