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

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

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="SpringStudy.Model">
</context:component-scan>
</beans>

二、定义一个配置类

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

代码如下:

package SpringStudy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import SpringStudy.Model.Counter;
import SpringStudy.Model.Piano; @Configuration
public class SpringConfig { @Bean
public Piano piano(){
return new Piano();
}
@Bean(name = "counter")
public Counter counter(){
return new Counter(12,"Shake it Off",piano());
}
}

三、基础类代码

package SpringStudy.Model;  

public class Counter {
public Counter() {
} public Counter(double multiplier, String song,Instrument instrument) {
this.multiplier = multiplier;
this.song = song;
this.instrument=instrument;
} private double multiplier; private String song; @Resource
private Instrument instrument; public double getMultiplier() {
return multiplier;
} public void setMultiplier(double multiplier) {
this.multiplier = multiplier;
} public String getSong() {
return song;
} public void setSong(String song) {
this.song = song;
} public Instrument getInstrument() {
return instrument;
} public void setInstrument(Instrument instrument) {
this.instrument = instrument;
} }

Piano类

package SpringStudy.Model;  

public class Piano {
private String name="Piano";
private String sound; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getSound() {
return sound;
} public void setSound(String sound) {
this.sound = sound;
} }

四、调用测试类

package webMyBatis;  

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import SpringStudy.Model.Counter; public class SpringTest {
public static void main(String[] args) {
//ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/bean.xml");// 读取bean.xml中的内容
ApplicationContext annotationContext = new AnnotationConfigApplicationContext("SpringStudy");
Counter c = annotationContext.getBean("counter", Counter.class);// 创建bean的引用对象
System.out.println(c.getMultiplier());
System.out.println(c.isEquals());
System.out.println(c.getSong());
System.out.println(c.getInstrument().getName());
}
}

注意:如果是在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

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. vs2013配置opencv3.2.0

    工具/原料 l VS2013 l OpenCV3.20http://jaist.dl.sourceforge.net/project/opencvlibrary/opencv-win/3.2.0/op ...

  2. Windows "计划任务"功能设置闹钟~

    相信很多人和我一样在使用电脑时都会遇到这样一个麻烦:不知道如何在windows 中设置一个闹铃.当我们在“开始”菜单的所有程序中找了一遍又一遍,甚至使用Everything.exe做全盘的搜索,都没有 ...

  3. python2.0_s12_day11_SqlAlchemy使用介绍

    SqlAlchemy ORM ORM的解释; 简单点:对象关系映射. 需求:我们写一个主机管理,把主机信息存在数据库,一开始我们编程不熟练的时候,执行命令时候要调用数据库,会把相应的SQL语句写到代码 ...

  4. hadoop程序MapReduce之MaxTemperature

    需求:求每年当中最高的温度 样本:temp.log 2016080623 2016072330 2015030420 输出结果:2016 30 2015 20 MapReduce分析设计: Mappe ...

  5. docker nginx mysql

    docker run -p 9000:9000 --name myphp -v /docker/www/:/var/www/html/ -v /docker/php/php.ini:/usr/loca ...

  6. 如何设置SOLR的高亮 (highlight)?

    打开SOLR的核心配置文件: solrconfig.xml 找到 standard request handler 写入以下XML配置代码: <requestHandler name=" ...

  7. 【转】Go Channels

    转自: http://kdf5000.com/2017/07/16/Go-Channels/ Golang使用Groutine和channels实现了CSP(Communicating Sequent ...

  8. DOS cmd - how to ping a remote host with specified port

    You can use ping to test whether you can connect to a remote host: ping baidu.com ping 125.6.45.88 ( ...

  9. Linux下多任务间通信和同步-mmap共享内存

    Linux下多任务间通信和同步-mmap共享内存 嵌入式开发交流群280352802,欢迎加入! 1.简介 共享内存可以说是最有用的进程间通信方式.两个不用的进程共享内存的意思是:同一块物理内存被映射 ...

  10. 通过身份证分析出生年月日、性别、年龄的SQL语句

    ),) ) ),)<>'X' ) ) )<>'X' ),)),)),)) ),)),)),)) ) as int)) where [出生日期]<>'' #字符串格式 ...