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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

【Spring注解系列01】@Configuration与@Bean

發布時間:2025/3/20 javascript 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Spring注解系列01】@Configuration与@Bean 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

1. @Configuration與@Bean

@Configuration:

  • 告訴Spring這是一個配置類,配置類==配置文件。
  • @Configuration==beans.xml

@Bean:

  • 給容器中注冊一個Bean;類型為返回值的類型,id默認是用方法名作為id。
  • @Bean 等價于 <bean></bean>
  • 可以給@Bean設置value來修改id,比如@Bean("personAlias")。
  • 可以在@bean中指定初始化和銷毀方法

@Bean(value = "beanLife",initMethod = "init", destroyMethod = "destroy")

等價于

<bean id="person" class="com.java.model.Person"? init-method="init" destroy-method="destroy"></bean>

?

2.配置對比與驗證

通常的xml配置方式如下,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/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"><!-- use-default-filters若想自定義過濾器生效,則必須將該值設為false--><!--<context:component-scan base-package="com.java" ></context:component-scan>--><!--注入bean--><beans><bean id="person" class="com.java.model.Person"><property name="id" value="11111"></property><property name="name" value="vhsj"></property></bean></beans> </beans>

采用@Configuration注入方式如下:

@Configuration public class BeanConfig {//@Bean@Bean("personAlias")public Person person2(){return new Person("2222","lisi");} }

測試如下:

public class BeanDependencyInjectionTest {public static void main(String[] args) {System.out.println("-----------------------------xml 方式注入-----------------------------------");//xml 方式注入ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");Person person = (Person) applicationContext.getBean("person");System.out.println("person---->"+person);//獲取所有注入Bean的名稱String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();for (String name : beanDefinitionNames) {System.out.println(name);}System.out.println("-----------------------------annotation 注解方式注入-----------------------------------");//annotation 注解方式注入ApplicationContext applicationContext1 = new AnnotationConfigApplicationContext(BeanConfig.class);Person person1 = applicationContext1.getBean(Person.class);System.out.println("person1---->"+person1);//獲取所有注入Bean的名稱String[] beanDefinitionNames1 = applicationContext1.getBeanDefinitionNames();for (String name : beanDefinitionNames1) {System.out.println(name);}} }

運行結果:

-----------------------------xml 方式注入----------------------------------- person---->Person{id='11111', name='vhsj'} person -----------------------------annotation 注解方式注入----------------------------------- person1---->Person{id='2222', name='lisi'} org.springframework.context.annotation.internalConfigurationAnnotationProcessor org.springframework.context.annotation.internalAutowiredAnnotationProcessor org.springframework.context.annotation.internalCommonAnnotationProcessor org.springframework.context.event.internalEventListenerProcessor org.springframework.context.event.internalEventListenerFactory beanConfig personAlias

?

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的【Spring注解系列01】@Configuration与@Bean的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。