Spring中基于Java的配置@Configuration和@Bean用法 (转)
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());
- }
- }
三、基础类代码
Counter:
- 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
本文转自http://blog.csdn.net/vvhesj/article/details/47661001 感谢原作者
Spring中基于Java的配置@Configuration和@Bean用法 (转)的更多相关文章
- 【Spring】28、Spring中基于Java的配置@Configuration和@Bean用法.代替xml配置文件
Spring中为了减少xml中配置,可以生命一个配置类(例如SpringConfig)来对bean进行配置. 一.首先,需要xml中进行少量的配置来启动Java配置: <?xml version ...
- Spring中基于Java的配置@Configuration和@Bean用法
spring中为了减少xml中配置,可以声明一个配置类(例如SpringConfig)来对bean进行配置. 一.首先,需要xml中进行少量的配置来启动Java配置: <?xml version ...
- Spring中基于java的配置
Spring中为了减少XML配置,可以声明一个配置类类对bean进行配置,主要用到两个注解@Configuration和@bean 例子: 首先,XML中进行少量的配置来启动java配置: <? ...
- spring+mybati java config配置引起的bean相互引用日志报警告问题
摘要: Error creating bean with name 'XXX': Requested bean is currently in creation: Is there an unreso ...
- Spring入门学习笔记(2)——基于Java的配置
目录 基于Java的配置 @Configuration & @Bean Annotations Example 注入Bean依赖 @Import注解 Lifecycle Callbacks(声 ...
- Spring IOC之基于JAVA的配置
基础内容:@Bean 和 @Configuration 在Spring中新的支持java配置的核心组件是 @Configuration注解的类和@Bean注解的方法. @Bean注解被用于表明一个方法 ...
- Spring Security基于Java配置
Maven依赖 <dependencies> <!-- ... other dependency elements ... --> <dependency> < ...
- Spring基于Java的配置
以下内容引用自http://wiki.jikexueyuan.com/project/spring/java-based-configuration.html: 基于Java的配置选项,可以使你在不用 ...
- Spring框架入门之基于Java注解配置bean
Spring框架入门之基于Java注解配置bean 一.Spring bean配置常用的注解 常用的有四个注解 Controller: 用于控制器的注解 Service : 用于service的注解 ...
随机推荐
- BA-传感器
01.室内温度传感器 壁装,西门子,QAA2061D 1.默认范围:温度0-50℃,湿度0-100% 2.供电方式:24vac 3.穿线方式:4芯屏蔽线 02.风管温度传感器 西门子,QAM2120. ...
- Python Study(02)之 Context Manager
上下文管理器(context manager)是Python2.5开始支持的一种语法,用于规定某个对象的使用范围.一旦对象进入或者离开该使用范围,会有特殊操作被调用 (比如为对象分配或者释放内存).它 ...
- PPAPI插件与浏览器的通信
PPAPI的插件,原本是能够使用JS与浏览器交互的,https://code.google.com/p/ppapi/wiki/InterfacingWithJavaScript.这里还提供了一个JS与 ...
- Hdu 4280 Island Transport(最大流)
Island Transport Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- oracle经常使用函数(2)
1.TRIM([ { { LEADING | TRAILING | BOTH }[ trim_character ]| trim_character} FROM ]trim_source) 函数 參数 ...
- tensorflow利用预训练模型进行目标检测(二):预训练模型的使用
一.运行样例 官网链接:https://github.com/tensorflow/models/blob/master/research/object_detection/object_detect ...
- spark 随机森林算法案例实战
随机森林算法 由多个决策树构成的森林,算法分类结果由这些决策树投票得到,决策树在生成的过程当中分别在行方向和列方向上添加随机过程,行方向上构建决策树时采用放回抽样(bootstraping)得到训练数 ...
- oracle 11gR2 如何修改vip
因为业务需要,需要将p570a主机和p570b主机上的vip做修改 修改前ip 192.168.128.12 p570a-vip 192.168.128.13 p570b-vip 修改后ip ...
- BigInteger类型转换成Long类型或int类型问题
BigInteger bi = new BigInteger("123"); int i = bi.intValue(); lo ...
- python中在py代码中如何去调用操作系统的命令
import socket import subprocess sk = socket.socket() sk.bind(('127.0.0.1',10800)) sk.listen() conn,a ...