spring中为了减少xml中配置,可以生命一个配置类(例如SpringConfig)来对bean进行配置。

一、首先,需要xml中进行少量的配置来启动Java配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  4. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
  11. <context:component-scan base-package="SpringStudy.Model">
  12. </context:component-scan>
  13. </beans>

二、定义一个配置类

用@Configuration注解该类,等价 与XML中配置beans;用@Bean标注方法等价于XML中配置bean。

代码如下:

  1. package SpringStudy;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import SpringStudy.Model.Counter;
  5. import SpringStudy.Model.Piano;
  6. @Configuration
  7. public class SpringConfig {
  8. @Bean
  9. public Piano piano(){
  10. return new Piano();
  11. }
  12. @Bean(name = "counter")
  13. public Counter counter(){
  14. return  new Counter(12,"Shake it Off",piano());
  15. }
  16. }

三、基础类代码

Counter:

  1. package SpringStudy.Model;
  2. public class Counter {
  3. public  Counter() {
  4. }
  5. public  Counter(double multiplier, String song,Instrument instrument) {
  6. this.multiplier = multiplier;
  7. this.song = song;
  8. this.instrument=instrument;
  9. }
  10. private double multiplier;
  11. private String song;
  12. @Resource
  13. private Instrument instrument;
  14. public double getMultiplier() {
  15. return multiplier;
  16. }
  17. public void setMultiplier(double multiplier) {
  18. this.multiplier = multiplier;
  19. }
  20. public String getSong() {
  21. return song;
  22. }
  23. public void setSong(String song) {
  24. this.song = song;
  25. }
  26. public Instrument getInstrument() {
  27. return instrument;
  28. }
  29. public void setInstrument(Instrument instrument) {
  30. this.instrument = instrument;
  31. }
  32. }

Piano类

  1. package SpringStudy.Model;
  2. public class Piano {
  3. private String name="Piano";
  4. private String sound;
  5. public String getName() {
  6. return name;
  7. }
  8. public void setName(String name) {
  9. this.name = name;
  10. }
  11. public String getSound() {
  12. return sound;
  13. }
  14. public void setSound(String sound) {
  15. this.sound = sound;
  16. }
  17. }

四、调用测试类

  1. package webMyBatis;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  4. import SpringStudy.Model.Counter;
  5. public class SpringTest {
  6. public static void main(String[] args) {
  7. //ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/bean.xml");// 读取bean.xml中的内容
  8. ApplicationContext annotationContext = new AnnotationConfigApplicationContext("SpringStudy");
  9. Counter c = annotationContext.getBean("counter", Counter.class);// 创建bean的引用对象
  10. System.out.println(c.getMultiplier());
  11. System.out.println(c.isEquals());
  12. System.out.println(c.getSong());
  13. System.out.println(c.getInstrument().getName());
  14. }
  15. }

注意:如果是在xml中配置beans和bean的话,或者使用自动扫描调用的话,代码为

ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/bean.xml");// 读取bean.xml中的内容
Counter c = ctx.getBean("counter", Counter.class);// 创建bean的引用对象

五、运行结果

12.0
false
Shake it Off
Piano

本文转自http://blog.csdn.net/vvhesj/article/details/47661001 感谢原作者

Spring中基于Java的配置@Configuration和@Bean用法 (转)的更多相关文章

  1. 【Spring】28、Spring中基于Java的配置@Configuration和@Bean用法.代替xml配置文件

    Spring中为了减少xml中配置,可以生命一个配置类(例如SpringConfig)来对bean进行配置. 一.首先,需要xml中进行少量的配置来启动Java配置: <?xml version ...

  2. Spring中基于Java的配置@Configuration和@Bean用法

    spring中为了减少xml中配置,可以声明一个配置类(例如SpringConfig)来对bean进行配置. 一.首先,需要xml中进行少量的配置来启动Java配置: <?xml version ...

  3. Spring中基于java的配置

    Spring中为了减少XML配置,可以声明一个配置类类对bean进行配置,主要用到两个注解@Configuration和@bean 例子: 首先,XML中进行少量的配置来启动java配置: <? ...

  4. spring+mybati java config配置引起的bean相互引用日志报警告问题

    摘要: Error creating bean with name 'XXX': Requested bean is currently in creation: Is there an unreso ...

  5. Spring入门学习笔记(2)——基于Java的配置

    目录 基于Java的配置 @Configuration & @Bean Annotations Example 注入Bean依赖 @Import注解 Lifecycle Callbacks(声 ...

  6. Spring IOC之基于JAVA的配置

    基础内容:@Bean 和 @Configuration 在Spring中新的支持java配置的核心组件是 @Configuration注解的类和@Bean注解的方法. @Bean注解被用于表明一个方法 ...

  7. Spring Security基于Java配置

    Maven依赖 <dependencies> <!-- ... other dependency elements ... --> <dependency> < ...

  8. Spring基于Java的配置

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/java-based-configuration.html: 基于Java的配置选项,可以使你在不用 ...

  9. Spring框架入门之基于Java注解配置bean

    Spring框架入门之基于Java注解配置bean 一.Spring bean配置常用的注解 常用的有四个注解 Controller: 用于控制器的注解 Service : 用于service的注解 ...

随机推荐

  1. BA-楼宇自控系统设计论文(转载)潘翌庆 元晨

    楼宇自控系统设计 潘翌庆 元晨 一.概述 楼宇自控系统(Building Automation System-BAS)是智能建筑中不可缺少的重要组成部分,在智能建筑中占有举足轻重的地位.它对建筑物内部 ...

  2. Java设计模式透析之 —— 模板方法(Template Method)

    今天你还是像往常一样来上班,一如既往地開始了你的编程工作. 项目经理告诉你,今天想在server端添加一个新功能.希望写一个方法.能对Book对象进行处理.将Book对象的全部字段以XML格式进行包装 ...

  3. Linux系统编程——多线程实现多任务

    概述 每一个进程都拥有自己的数据段.代码段和堆栈段.这就造成进程在进行创建.切换.撤销操作时,须要较大的系统开销. 为了降低系统开销,从进程中演化出了线程.为了让进程完毕一定的工作.进程必须至少包括一 ...

  4. .NET泛型初探

    总所周知,.NET出现在.net framework 2.0,为什么要在2.0引入泛型那,因为微软在开始开发.net框架时并没有想过多个类型参数传输时对方法的重构,这样一来,开发人员就要面对传输多种类 ...

  5. 【BZOJ 2821】作诗

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2821 [算法] 如果不强制在线,显然莫队是可以解决此题的,那么,强制在线怎么办呢? ...

  6. Linux就该这么学 20181007(第十一章ftp)

    参考链接https://www.linuxprobe.com/ iptables -F #ftp 21端口 #主动模式,被动模式 #匿名用户 本地用户 虚拟用户 vim /etc/vsftpd/vsf ...

  7. Vmware VM共享

    打开虚拟机,设置,选项点击共享文件夹 点击启用,将电脑windows的目录添加过来 进入终端即可以进入

  8. How to include custom library into maven local repository?--转

    原文地址:https://www.mkyong.com/maven/how-to-include-library-manully-into-maven-local-repository/ There ...

  9. Python学习网络爬虫--转

    原文地址:https://github.com/lining0806/PythonSpiderNotes Python学习网络爬虫主要分3个大的版块:抓取,分析,存储 另外,比较常用的爬虫框架Scra ...

  10. MVC中添加模块区域,并设置RedirectToAction跳转

    废话少说,直接上图: