AnnotationConfigApplicationContext的register方法

发布时间:2017-6-29 10:23:54编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"AnnotationConfigApplicationContext的register方法",主要涉及到AnnotationConfigApplicationContext的register方法方面的内容,对于AnnotationConfigApplicationContext的register方法感兴趣的同学可以参考一下。

public class Main {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

		applicationContext.register(Ch2BeanConfiguration.class);
		// applicationContext.scan("com.mlf");
		// # must user refress() method
		applicationContext.refresh();

		AccountService accountService = (AccountService) applicationContext.getBean("accountService");
		System.out.println("Beafore money transfer");
		System.out.println("Account1 balance:" + accountService.getAccount(1l).getBalance());
		System.out.println("Account2 balance:" + accountService.getAccount(2l).getBalance());
		System.out.println("------------------------------------");
		accountService.transferMoney(1l, 2l, 5.5);

		System.out.println("After money transfer");
		System.out.println("Account1 balance:" + accountService.getAccount(1l).getBalance());
		System.out.println("Account2 balance:" + accountService.getAccount(2l).getBalance());
		System.out.println("------------------------------------");

	}
}
package com.mlf.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import com.mlf.spring.dao.AccountDao;
import com.mlf.spring.dao.impl.AccountDaoInMemoryImpl;
import com.mlf.spring.service.AccountService;
import com.mlf.spring.service.impl.AccountServiceImpl;

@Component
public class Ch2BeanConfiguration {

	@Bean
	public AccountService accountService() {
		AccountServiceImpl bean = new AccountServiceImpl();
		bean.setAccountDao(accountDao());
		return bean;
	}

	@Bean
	public AccountDao accountDao() {
		AccountDaoInMemoryImpl beAccountDaoInMemoryImpl = new AccountDaoInMemoryImpl();
		return beAccountDaoInMemoryImpl;
	}
}



上一篇:Getting started with unit tests in Qt Creator and Catch
下一篇:学习使用MySQL 5.7的sys库

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。

好贷网好贷款