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
Spring中基于Java的配置@Configuration和@Bean用法相关推荐
- java中配置bean_Spring中基于Java的配置@Configuration和@Bean用法
Spring中为了减少xml中配置,可以声明一个配置类(例如SpringConfig)来对bean进行配置. 一.首先,需要xml中进行少量的配置来启动Java配置: xmlns:xsi=" ...
- spring中基于Java容器配置注解的区别及使用场景
转载自百家号作者:有趣的代码 @Component.@Service.@Controller.@Repository:@Configuration.@Bean区别,使用场景 从spring3.0开始, ...
- spring 加载java类_在Spring中基于Java类进行配置的完整步骤
在Spring中基于Java类进行配置的完整步骤 发布于 2020-7-7| 复制链接 基于Java配置选项,可以编写大多数的Spring不用配置XML,下面 前言JavaConfig 原来是 Spr ...
- spring java配置_Spring基于java的配置
我们之前都了解过spring基于xml的配置,我们也可以通过配置类来完成基于xml的配置,我们会在下面以一个例子来讲述一下Spring基于java的配置. 首先第一步准备工作: 1)创建一个Dog类 ...
- spring java配置_Spring:使基于Java的配置更加优雅
spring java配置 大家好,我很久没有写新文章了. 积累了很多资料,需要在不久的将来在我的博客中发布. 但是现在我想谈谈Spring MVC应用程序配置. 确切地说,我想谈谈基于Java的Sp ...
- Spring:使基于Java的配置更加优雅
大家好,我很久没有写新文章了. 积累了很多资料,需要在不久的将来在我的博客中发布. 但是现在我想谈谈Spring MVC应用程序配置. 确切地说,我想谈谈基于Java的Spring配置. 尽管在3.0 ...
- Spring MVC:使用基于Java的配置创建一个简单的Controller
这是我博客上与Spring MVC相关的第一篇文章. 开端总是令人兴奋的,因此我将尽量简洁明了. Spring MVC允许以最方便,直接和快速的方式创建Web应用程序. 开始使用这项技术意味着需要Sp ...
- 使用Spring 3.1和基于Java的配置构建RESTful Web服务,第2部分
1.概述 本文介绍了如何在Spring中设置REST –控制器和HTTP响应代码,有效负载编组配置和内容协商. 2.在Spring了解REST Spring框架支持两种创建RESTful服务的方式: ...
- spring中基于XML的AOP配置步骤
spring中基于XML的AOP配置步骤 IAccountService.java package com.itheima.service;/*** 账户的业务层接口*/ public interfa ...
最新文章
- Flutter 配置
- 网络编程学习笔记(getservbyname和getservbyport函数)
- 计算机蠕虫的存在形式,计算机蠕虫
- HAN:基于双层注意力机制的异质图深度神经网络
- DGL教程【五】使用自己的数据集
- MyBatis 插件原理与自定义插件-PageHelper 原理
- 2019ICPC(上海) - Spanning Tree Removal(构造)
- 数据结构与索引-- B+树索引
- HDU 5652 India and China Origins(二分 + BFS)
- jQuery 事件 - bind() 方法
- [转载] [转载] python 去除字符串的标点符号 用_Python成为专业人士笔记–String字符串方法
- 冲突等价(ConflictEquivalence) 可串行化调度(Serializable Schedules)
- Matplotlib--legend函数
- ioca0中断 pic单片机_关于PIC单片机的一些经验总结
- 自学结构体(小甲鱼c语言)
- 测试开发实习日记(DAY4)
- linux音频函数接口,Linux音频驱动-OSS和ALSA声音系统简介及其比较
- Oracle配置本地网络服务器测试不成功,无监听程序
- 「镁客早报」微软总市值超过苹果亚马逊;Linux内核出现漏洞
- 【基础入门题072】转置矩阵
热门文章
- QT的QDateTimeAxis类的使用
- c++类与类之间关系
- c++命令模式command
- c++11中static类对象构造函数线程安全
- pop3服务器协议初始化失败,Exchange2003系统管理器中的默认POP3 虚拟服务器无法自动启动...
- 先天性脑部发育异常_宝宝出现发育迟缓,会有哪些症状?修正育儿知识分享!...
- 02/03_Pytorch安装、Conda安装Pythorch,换源、pytorch官网、验证、安装jupyter、卸载、安装、启动jupyter、配置Jupyter notebook、使用
- Zookeeper的功能以及工作原理 (转自:http://www.cnblogs.com/felixzh/p/5869212.html)
- 5.非关系型数据库(Nosql)之mongodb:创建集合,备份与导入导出, 数据还原,导入导出
- Java关于文件上传的一个例子