1. 程式人生 > >@Bean Spring容器的唯一bean

@Bean Spring容器的唯一bean

[email protected] 思維導圖

這裡寫圖片描述
三月 18, 2018. Created by XMind

[email protected] Example

2.1 定義一個Bean 的生命週期

package com.spring.annotation.bean;
/**
* Created by Calvin on 2018/3/13
*  Bean實體
*/
public class Bean {

public void init(){
System.out.println("@Bean 初始化");
}

public void runtime(){
System.out.println("@Bean 正在執行"
); } public void destroy(){ System.out.println("@Bean 銷燬"); } }

2.1.1 如同 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:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jdbchttp://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd http://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-4.0.xsd"
default-lazy-init="false">
<!--在spring 容器中,註冊 bean--> <bean id="bean" class="com.spring.annotation.bean.Bean" scope="prototype"> </bean> </beans> <[email protected] 相當於 spring-configuration-annonation.xml 中的<beans></beans>-->

2.2 放入spring 容器中,進行Bean 註冊, 並且指定Bean 屬性的Socpe 範圍

package com.spring.annotation.bean;
import org.springframework.context.annotation.Scope;

/**
* Created by Calvin on 2018/3/13
*/
@org.springframework.context.annotation.Configuration
public class Bean_Configuration {

/**
* @Bean 在Spring 容器中註冊Bean
* @Bean 在返回例項的方法上,如果未通過@Bean 指定bean 的名稱,則預設與標註的方法名相同
* @Bean(name="bean"bnjl.bvbvbbvnn,initMethod="init",destroyMethod="destory") 同時可以指定初始化和銷燬
* @Bean 標註在方法上
*
*/
@org.springframework.context.annotation.Bean(name="bean",initMethod = "init",destroyMethod="destroy")
/**
* @Socpe 作用域,在裝配Bean時必須在配置檔案中指明。
* @Socpe 1.基本作用域(singleton:單例、prototype 原型模式)
* 2.Web 作用域(request、session、globalsession)
* 3.自定義作用域
* @Socpe("singleton") 全域性有且僅有一個例項
* @Socpe("prototype") 每次獲取bean的時候都會new 一個例項
* @Socpe("request") 表示該針對每一次HTTP 請求都會產生一個新的Bean, 同時該bean 僅當對當前http requet 內有效,配置例項
* @Socpe("session") 表示該針對每一次HTTP請求都會產生一個新的bean,同時該bean僅在當前HTTP session內有效
* @Socpe("globalsession") 作用域類似於標準的HTTP Session作用域,不過它僅僅在基於portlet的web應用中才有意義。
* Portlet規範定義了全域性Session的概念,它被所有構成某個 portlet web應用的各種不同的portlet所共享。在global session作用域中定義的bean被限定於全域性portlet Session的生命週期範圍內。如果你在web中使用global session作用域來標識bean,那麼web會自動當成session型別來使用。
*/
@Scope("prototype")
public Bean bean(){
return new Bean();
}

Bean_Configuration(){
System.out.println("Spring 容器初始化");
}
}

2.3 測試結果

三月 17, 2018 7:58:58 下午
org.springframework.context.annotation.AnnotationConfigApplicationContext
prepareRefresh 資訊: Refreshing
org.spring[email protected]246ae04d:
startup date [Sat Mar 17 19:58:58 CST 2018]; root of context hierarchy
@Configuration 容器啟動初始化

[email protected] 流程圖

這裡寫圖片描述

三月 18, 2018. Created by XMind

相關推薦

@Bean Spring容器唯一bean

[email protected] 思維導圖 三月 18, 2018. Created by XMind [email protected] Example 2.1 定義一

Spring容器bean概要

com init 管理 pass XML efi AC pre color 容器:   通俗的理解容器就是用來管理bean和bean之間依賴的一個組件。很多參考資料說容器就是org.springframework.context.ApplicationContext,但筆者

Spring容器Bean的生命周期

pub run down xsd 信息 ini exc rop throw Spring生命周期分為以下步驟: 1.instantiate bean 對象實例化 2.populate properties 封裝屬性 3.如果Bean實現BeanNameAware執行setB

普通Java類獲取spring 容器bean的5種方法 Spring注入非單例bean以及scope的作用範圍

本文轉載自:http://www.cnblogs.com/duanxz/archive/2014/06/18/3794075.html 方法一:在初始化時儲存ApplicationContext物件方法二:通過Spring提供的工具類獲取ApplicationContext物件方法三:繼承自抽象類Appli

filter中使用Spring容器注入bean

[1] XML配置形式 Tomcat容器初始化順序:監聽器–>過濾器–>servlet,因此springMVCservlet初始化之前,過濾器就已經初始化過了,如果在過濾器中需要注入spring容器管理的bean是注入不進去的,因此需要在spring監聽器中初始化需要注入的bean,

實現ApplicationContextAware介面,java(new或者java反射獲取的物件)中獲取spring容器bean

本文參考了https://blog.csdn.net/bailinbbc/article/details/76446594,其實是拷貝了很多內容: 在Web應用中,Spring容器通常採用宣告式方式配置產生:開發者只要在web.xml中配置一個Listener,該Listener將會負責初始化S

Spring容器bean的注入(1)

第一節 在IOC容器中裝配Bean 1.1Spring容器成功啟動條件 1.匯入Spring框架相關的jar包 2.正確配置spring配置檔案 3.Bean的類都已放到應用程式的類路徑下 1.2Bean配置資訊的組成 Bean配置資

spring容器建立bean物件的方式

1)xml檔案中有bean的配置,而且這個bean所對應的java類中存在一個無參構造器,那麼這個時候spring容器就可以使用反射呼叫無參構造器來建立例項了(常規的方式) 2)通過工廠類獲得例項(工廠類實現了介面FactoryBean<?>) 例如: xml中的配置

Spring學習筆記一(Spring容器bean的注入)

第一節 在IOC容器中裝配Bean 1.1Spring容器成功啟動條件 1.匯入Spring框架相關的jar包 2.正確配置spring配置檔案 3.Bean的類都已放到應用程式的類路徑下 1.2Bean配置資訊的組成 Bean配置資訊是Bean的元資料資訊(

Spring容器啟動 Bean的3種初始化方式

通常我們建立一個Bean的時候,可能有這樣的需求,比如我建立了一個連線資料庫的Bean類,我希望在Spring容器建立的時候,就檢查下是否能正常訪問資料庫。這時候我們可以藉助於以下方式進行實現。1、如果

Spring容器Bean的生命週期(init-method destroy-method)

Spring容器中Bean的生命週期 這一篇很詳細的講了Bean生命週期的每一個過程。     我主要是想實現一下init方法核destory方法,因為這個和AOP程式設計的環繞通知有點兒相似的感覺,所以特別來研究一下這兩個方法。      在Spring配置中,i

java 如何在listener(監聽器) 中使用Spring容器管理bean

問題來源:在Listener監聽器中無法使用Spring容器的@Resource或者@Autowired 註解的方法注入bean,因為,在web Server容器中,無論是Servlet,Filter,還是Listener都不是Spring容器管理的,因此我們都無法在這些

spring容器建立bean的時機

由於自己用的仍然是spring3版本,也沒有過多的研究過原始碼,在這裡只是標註一下,如有什麼不對還請各位指出。在xml中配置bean的時候,如果沒有設定 lazy-init=true這個屬性,那麼這個bean的建立時機就是容器啟動時就自動建立了這個類的例項,如果設定了lazy

spring容器bean載入機制原始碼解讀

前言:這是本人第一個部落格,早就想記錄些總結和理解,可一直不知道從哪開始,最近正好在解決一個spring的問題,正好這個問題涉及到了spring的一些相關基礎,整理一下就從這部分開始了。 歡迎所有閱讀者和愛好者批評從各個方面(特別是文件和技術方面)批評,指正。

Spring如何管理Java普通類(Java類獲取Spring容器bean

方法一:在初始化時儲存ApplicationContext物件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.x

模擬Spring容器使用bean.xml建立物件的過程

正式使用spring之前先來體驗一下spring的自動建立物件,讓面向物件程式設計變成面向介面程式設計。思路:    在呼叫spring的bean.xml配置時,就已經自動建立Dao層和Service層的物件一、JAR包        因為是來體驗spring的bean.xm

spring學習教程5-spring容器管理bean的生命週期

例如:我們需要從容器中獲得一個Bean的物件:1、容器建立該Bean的物件(預設呼叫無參構造器)         2、容器給這個Bean物件注入依賴(預設是set方式)        3、如果這個Bean已經實現了BeanNameAware介面,容器會呼叫它實現的setBeanName(String)方法,此

java 在listener(監聽器) 中使用Spring容器注入bean

今天在SSH的專案開發中,用到了監聽器Listener,並且需要在Listener中使用到Spring容器中的Bean。Spring容器本身就是在web.xml中使用listener的方式啟動的。想在例

Spring 容器Bean與生命週期

開發十年,就只剩下這套架構體系了! >>>   

Spring IOC 一——Spring容器裝配Bean

寫在前面 這篇文章去年寫的,緣起於去年某段時間被領導臨時“抓壯丁”般的叫過去做java開發,然後在網上找了一個 SpringMVC 的 demo,學習一下,然後依葫蘆畫瓢,開始了自己的專案開發,也還順利完成了任務。在使用 SpringMVC 的過程中,我被這個被稱作“最優秀”的 java 框架 —— Spri