热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

spring笔记(2)spring的3种IOC容器配置方式

1.通过 @Configuration和@bean实现@ConfigurationpublicclassCh2BeanConfiguration{@BeanpublicAccoun

1.通过 @Configuration 和 @bean 实现

@Configuration
public class Ch2BeanConfiguration {

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

public AccountDao accountDao(){
AccountDaoInMemoryImpl bean = new AccountDaoInMemoryImpl();
return bean;
}
}
public class Main

{
public static void main( String[] args )
{
AnnotationConfigApplicationContext applicatiOnContext= new AnnotationConfigApplicationContext(Ch2BeanConfiguration.class);
//在容器中获取bean类
AccountService accountService = applicationContext.getBean("accountService", AccountService.class);

System.out.println("Before money transfer");
System.out.println("Account 1 balance :" + accountService.getAccount(1L).getBalance());
System.out.println("Account 2 balance :" + accountService.getAccount(2L).getBalance());
accountService.transferMoney(1L,2L,5.0);
System.out.println("After money transfer");
System.out.println("Account 1 balance :" + accountService.getAccount(1L).getBalance());
System.out.println("Account 2 balance :" + accountService.getAccount(2L).getBalance());
}
}


2.通过 xml 实现


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">






public class Main
{
public static void main( String[] args )
{
ClassPathXmlApplicationContext applicatiOnContext= new ClassPathXmlApplicationContext("file:E:\\spring-book-ch2\\src\\main\\resources\\ch2-beans.xml");
//在容器中获取bean类
AccountService accountService = applicationContext.getBean("accountService", AccountService.class);

System.out.println("Before money transfer");
System.out.println("Account 1 balance :" + accountService.getAccount(1L).getBalance());
System.out.println("Account 2 balance :" + accountService.getAccount(2L).getBalance());
accountService.transferMoney(1L,2L,5.0);
System.out.println("After money transfer");
System.out.println("Account 1 balance :" + accountService.getAccount(1L).getBalance());
System.out.println("Account 2 balance :" + accountService.getAccount(2L).getBalance());
}
}

3.通过 注解 和 xml 实现
在对应的类上写@Component @Repository @Service @Controller




















@Component最普通的组件,可以被注入到spring容器进行管理
@Repository作用于持久层
@Service作用于业务逻辑层
@Controller作用于表现层(spring-mvc的注解)

因为原生的java操作数据库所产生的异常只定义了几种,但是产生数据库异常的原因却有很多种,这样对于数据库操作的报错排查造成了一定的影响;而Spring拓展了原生的持久层异常,针对不同的产生原因有了更多的异常进行描述。所以,在注解了@Repository的类上如果数据库操作中抛出了异常,就能对其进行处理,转而抛出的是翻译后的spring专属数据库异常,方便我们对异常进行排查处理

public class Main
{
public static void main( String[] args )
{

ClassPathXmlApplicationContext applicatiOnContext= new ClassPathXmlApplicationContext("file:E:\\spring-book-ch2\\src\\main\\java\\org\\lin\\maven\\test\\resources\\ch2-beans.xml");
//在容器中获取bean类
AccountService accountService = applicationContext.getBean("accountService", AccountService.class);

System.out.println("Before money transfer");
System.out.println("Account 1 balance :" + accountService.getAccount(1L).getBalance());
System.out.println("Account 2 balance :" + accountService.getAccount(2L).getBalance());
accountService.transferMoney(1L,2L,5.0);
System.out.println("After money transfer");
System.out.println("Account 1 balance :" + accountService.getAccount(1L).getBalance());
System.out.println("Account 2 balance :" + accountService.getAccount(2L).getBalance());
}
}

注:ClassPathXmlApplicationContext路劲如果找不到,直接用("file:绝对路径")

推荐阅读
  • Todayatworksomeonetriedtoconvincemethat:今天在工作中有人试图说服我:{$obj->getTableInfo()}isfine ... [详细]
  • 本文介绍了ASP.NET Core MVC的入门及基础使用教程,根据微软的文档学习,建议阅读英文文档以便更好理解,微软的工具化使用方便且开发速度快。通过vs2017新建项目,可以创建一个基础的ASP.NET网站,也可以实现动态网站开发。ASP.NET MVC框架及其工具简化了开发过程,包括建立业务的数据模型和控制器等步骤。 ... [详细]
  • Spring框架《一》简介
    Spring框架《一》1.Spring概述1.1简介1.2Spring模板二、IOC容器和Bean1.IOC和DI简介2.三种通过类型获取bean3.给bean的属性赋值3.1依赖 ... [详细]
  • 本文讨论了在ASP中创建RazorFunctions.cshtml文件时出现的问题,即ASP.global_asax不存在于命名空间ASP中。文章提供了解决该问题的代码示例,并详细解释了代码中涉及的关键概念,如HttpContext、Request和RouteData等。通过阅读本文,读者可以了解如何解决该问题并理解相关的ASP概念。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • springmvc学习笔记(十):控制器业务方法中通过注解实现封装Javabean接收表单提交的数据
    本文介绍了在springmvc学习笔记系列的第十篇中,控制器的业务方法中如何通过注解实现封装Javabean来接收表单提交的数据。同时还讨论了当有多个注册表单且字段完全相同时,如何将其交给同一个控制器处理。 ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • Spring常用注解(绝对经典),全靠这份Java知识点PDF大全
    本文介绍了Spring常用注解和注入bean的注解,包括@Bean、@Autowired、@Inject等,同时提供了一个Java知识点PDF大全的资源链接。其中详细介绍了ColorFactoryBean的使用,以及@Autowired和@Inject的区别和用法。此外,还提到了@Required属性的配置和使用。 ... [详细]
  • 本文介绍了在iOS开发中使用UITextField实现字符限制的方法,包括利用代理方法和使用BNTextField-Limit库的实现策略。通过这些方法,开发者可以方便地限制UITextField的字符个数和输入规则。 ... [详细]
  • 本文介绍了MVP架构模式及其在国庆技术博客中的应用。MVP架构模式是一种演变自MVC架构的新模式,其中View和Model之间的通信通过Presenter进行。相比MVC架构,MVP架构将交互逻辑放在Presenter内部,而View直接从Model中读取数据而不是通过Controller。本文还探讨了MVP架构在国庆技术博客中的具体应用。 ... [详细]
  • Asp.net Mvc Framework 七 (Filter及其执行顺序) 的应用示例
    本文介绍了在Asp.net Mvc中应用Filter功能进行登录判断、用户权限控制、输出缓存、防盗链、防蜘蛛、本地化设置等操作的示例,并解释了Filter的执行顺序。通过示例代码,详细说明了如何使用Filter来实现这些功能。 ... [详细]
  • 本文介绍了如何在Azure应用服务实例上获取.NetCore 3.0+的支持。作者分享了自己在将代码升级为使用.NET Core 3.0时遇到的问题,并提供了解决方法。文章还介绍了在部署过程中使用Kudu构建的方法,并指出了可能出现的错误。此外,还介绍了开发者应用服务计划和免费产品应用服务计划在不同地区的运行情况。最后,文章指出了当前的.NET SDK不支持目标为.NET Core 3.0的问题,并提供了解决方案。 ... [详细]
  • wpf+mvvm代码组织结构及实现方式
    本文介绍了wpf+mvvm代码组织结构的由来和实现方式。作者回顾了自己大学时期接触wpf开发和mvvm模式的经历,认为mvvm模式使得开发更加专注于业务且高效。与此同时,作者指出mvvm模式相较于mvc模式的优势。文章还提到了当没有mvvm时处理数据和UI交互的例子,以及前后端分离和组件化的概念。作者希望能够只关注原始数据结构,将数据交给UI自行改变,从而解放劳动力,避免加班。 ... [详细]
  • SpringMVC工作流程概述
    SpringMVC工作流程概述 ... [详细]
author-avatar
yfx132435
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有