1. 程式人生 > >Spring的 @Bean和@Configuration註解使用

Spring的 @Bean和@Configuration註解使用

上程式碼:

@Configuration
public class TestBeanConfig {
	
	@Bean(autowire=Autowire.BY_NAME,name="testBean")
	@Scope(value="prototype")
	public TestBean testBean(){
		System.out.println("You are going to new a objec TestBean ");
		return new TestBean();
	}
	
}
public class TestBean {

	public void sayHello(){
		System.out.println("Hello how are you?");
	}
}

在springBoot中測試 TestBean的sayHello方法

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestBeanConfigTest {
	@Resource(name="testBean")
	private TestBean testBean;
	
	@Test
	public void test() {
		System.out.println(testBean);
		testBean.sayHello();
	}

}

列印

You are going to new a objec TestBean 
[email protected]
Hello how are you?

有後續詳細引數設定,再新增

相關推薦

Spring的 @Bean@Configuration註解使用

上程式碼: @Configuration public class TestBeanConfig { @Bean(autowire=Autowire.BY_NAME,name="testBean"

spring學習(十一)——spring官方文件閱讀(5.0.7)——spring的@Bean與@Configuration註解

@Bean與@Configuration註解 @Bean註解用於方法上,返回的例項將由Spring IOC管理,當在@Configuration註解的類中使用@Bean註解時,@Bean相當於<bean/>元素,@Configuration相當於<bean

Spring bean 單例bean的執行緒安全

Bean的作用域    Spring 3中為Bean定義了5中作用域,分別為singleton(單例)、prototype(原型)、request、session和global session,5種作用域說明如下: singleton:單例模式,Spring I

spring bean管理的註解

Class path掃描與元件的管理    @Component是一個通用的註解,可用於任何bean    @Repository,@Service,@Controller是更有針對性的註解       @Repository:通常用於註解DAO類,即持久層       @S

@Component註解@Configuration註解的之間的區別

直白的說Component包含了Configuration的所用註解:Configuration和Component都是註解在類上的,Configuration類中的方法成員變數是@Value註解,方法上的註解是@BeanComponent類中的方法和成員變數沒有任何限制

Spring @Autowired@Qualifier註解

介紹: @Autowired和@Qualifier是Spring專案中比較常用的兩個註解。一般在同一個實現Bean可以在不同的平臺使用,Controller呼叫Service,Service呼叫DAO。其中很多例項都是使用@Autowired自動實現,但是如果同一份Bean

spring bean java bean的區別

什麼是SpringBean? 引用《Spring實戰》(Spring in action) 中的一句話:在傳統的Java應用中,bean的生命週期很簡單。使用Java關鍵字new進行bean例項化,然後該bean就可以使用了。一旦該bean不再被使用,則由Java自動進行垃

Spring程式碼例項系列-05:通過註解@Configuration、@Bean@Import定義bean

Spring中有三種定義Bean的方式: 1. *.xml/<bean>/<import> 2. @Configuration/@Bean/@Import 3. @Component/@Controller/@Service/@

使用@Configuration註解來代替Springbean配置

ble get new .com 安全 class alt 一個 需要 下面是一個典型的Spring配置文件(application-config.xml): 1 2 3 4 5 6 7 8 <beans> <bean

Spring註解注入bean配置檔案注入bean

註解的方式確實比手動寫xml檔案注入要方便快捷很多,省去了很多不必要的時間去寫xml檔案 按以往要注入bean的時候,需要去配置一個xml,當然也可以直接掃描包體,用xml注入bean有以下方法: 1 <?xml version="1.0" encoding="UTF-8"?> 2

spring(一)註解使用(@Configuration 、@Bean、@ComponentScan、@TypeFilter)

  1、spring的框架: spring的核心是控制反轉(IOC)和橫向切面(AOP) 核心容器:核心容器提供 Spring 框架的基本功能。核心容器的主要元件是 BeanFactory,它是工廠模式的實現。BeanFactory 使用控制

Spring整理系列(11)——@Configuration註解、@Bean註解以及配置自動掃描、bean作用域

1、@Configuration標註在類上,相當於把該類作為spring的xml配置檔案中的<beans>,作用為:配置spring容器(應用上下文) package com.test.spring.support.configuration; @Configuration pub

Spring原始碼解析 – @Configuration配置類及註解Bean的解析

  在分析Spring 容器建立過程時,我們知道容器預設會載入一些後置處理器PostPRocessor,以AnnotationConfigApplicationContext為例,在建構函式中初始化reader時,載入預設後置處理器。其中 ConfigurationClassPostProcessor這個後置

spring xml配置beancontext:component-scan掃描註解註冊bean的區別

本文啟動spring容器用的是dubbo的main方法 1.xml配置bean: 2.<context:component-scan>掃描註解註冊bean: 測試猜測結果: 前者啟動spring容器時建立bean

spring配置檔案中bean標籤中init-methoddestroy-method註解方式配置

Person類 public class Person { private int i = 0; public Person(){ System.out.println("例項化一個物件"

Spring註解】@Configuration@ComponentScan註解

Spring註解 一、組建註冊 包結構: [email protected]和@ComponentScan @ComponentScan的includeFilters

Spring註解bean事務?有哪些相關的註解?踩坑

<context:component-scanbase-package="org.example"/>然後可以用@Component、@Controller、@Service、@Repository註解來標註需要由Spring IoC容器進行物件託管的類。這幾個註

Springbean管理(註解注入屬性註解檔案混合使用)

Spring的bean管理(註解注入屬性和註解檔案混合使用) 註解方式注入屬性  1 建立service類,建立dao類,在service得到dao物件     注入屬性第一個註解 @Autowire

spring註解註解使用bean的流程原理

在spring非註解使用一個容器建立好的bean的時候,首先,在容器中進行bean的宣告及bean的初始化,然後再業務程式碼中通過applicationContext來獲取bean的例項。這裡可以有兩種方法進行得到bean的例項。ac.getbean("bean的id/be

Spring bean中的properties元素內的name ref都代表什麽意思啊?

spring code 文件 use bsp div 字符 sco nbsp <bean id="userAction" class="com.neusoft.gmsbs.gms.user.action.UserAction" scope="pr