【Spring】28、Spring中基于Java的配置@Configuration和@Bean用法.代替xml配置文件
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】28、Spring中基于Java的配置@Configuration和@Bean用法.代替xml配置文件的更多相关文章
- Spring中基于Java的配置@Configuration和@Bean用法
spring中为了减少xml中配置,可以声明一个配置类(例如SpringConfig)来对bean进行配置. 一.首先,需要xml中进行少量的配置来启动Java配置: <?xml version ...
- Spring中基于Java的配置@Configuration和@Bean用法 (转)
spring中为了减少xml中配置,可以生命一个配置类(例如SpringConfig)来对bean进行配置. 一.首先,需要xml中进行少量的配置来启动Java配置: <?xml version ...
- Spring框架入门之基于Java注解配置bean
Spring框架入门之基于Java注解配置bean 一.Spring bean配置常用的注解 常用的有四个注解 Controller: 用于控制器的注解 Service : 用于service的注解 ...
- Spring中基于java的配置
Spring中为了减少XML配置,可以声明一个配置类类对bean进行配置,主要用到两个注解@Configuration和@bean 例子: 首先,XML中进行少量的配置来启动java配置: <? ...
- Spring入门学习笔记(2)——基于Java的配置
目录 基于Java的配置 @Configuration & @Bean Annotations Example 注入Bean依赖 @Import注解 Lifecycle Callbacks(声 ...
- Spring基于Java的配置
以下内容引用自http://wiki.jikexueyuan.com/project/spring/java-based-configuration.html: 基于Java的配置选项,可以使你在不用 ...
- Spring IOC之基于JAVA的配置
基础内容:@Bean 和 @Configuration 在Spring中新的支持java配置的核心组件是 @Configuration注解的类和@Bean注解的方法. @Bean注解被用于表明一个方法 ...
- Spring 基于 Java 的配置
前面已经学习如何使用 XML 配置文件来配置 Spring bean. 基于 Java 的配置可以达到基于XML配置的相同效果. 基于 Java 的配置选项,可以使你在不用配置 XML 的情况下编写大 ...
- Spring MVC 5 + Thymeleaf 基于Java配置和注解配置
Spring MVC 5 + Thymeleaf 注解配置 Spring的配置方式一般为两种:XML配置和注解配置 Spring从3.0开始以后,推荐使用注解配置,这两种配置的优缺点说的人很多,我就不 ...
随机推荐
- 【分布式缓存系列】集群环境下Redis分布式锁的正确姿势
一.前言 在上一篇文章中,已经介绍了基于Redis实现分布式锁的正确姿势,但是上篇文章存在一定的缺陷——它加锁只作用在一个Redis节点上,如果通过sentinel保证高可用,如果master节点由于 ...
- 像纸质笔记本一样给div,textarea添加行的分割线
想要给textarea添加一个背景图来实现 但是背景图有几个问题, 1.每个div或者textarea的line-height不一样,对于每个不同的line-height都需要一个不同的背景图 2.当 ...
- 【洛谷4172】 [WC2006]水管局长(LCT)
传送门 洛谷 BZOJ Solution 如果不需要动态的话,那就是一个裸的最小生成树上的最大边权对吧. 现在动态了的话,把这个过程反着来,就是加边对吧. 现在问题变成了怎么动态维护加边的最小生成树, ...
- 聊一聊Java如何接入招行一网通支付功能
1.前提条件 相比较于支付宝和微信的支付功能接入这一块,银行相对来说更加严格,比如说支付宝,在你签约之前可以进行一些测试.但是银行来说就不是这样了,如果您现在要进行招行的支付功能开发的话,请务必先让相 ...
- Logistic回归Cost函数和J(θ)的推导(二)----梯度下降算法求解最小值
前言 在上一篇随笔里,我们讲了Logistic回归cost函数的推导过程.接下来的算法求解使用如下的cost函数形式: 简单回顾一下几个变量的含义: 表1 cost函数解释 x(i) 每个样本数据点在 ...
- 吴恩达机器学习笔记2-代价函数I(cost function)
我们选择的参数决定了我们得到的直线相对于我们的训练集的准确程度,模型所预测的值与训练集中实际值之间的差距(下图中蓝线所指)就是建模误差(modeling error). 我们的目标便是选择出可以使得建 ...
- PHP安装BCMath扩展
我们都知道,大多数编程语言对于浮点型数据格式遵循 IEEE 754 标准,PHP也不例外,这就会导致在使用浮点数运算的过程中会有精度丢失的问题.PHP提供了BCMath库来支持更加精确的计算.但是我的 ...
- Kali学习笔记30:身份认证与命令执行漏洞
文章的格式也许不是很好看,也没有什么合理的顺序 完全是想到什么写一些什么,但各个方面都涵盖到了 能耐下心看的朋友欢迎一起学习,大牛和杠精们请绕道 实验环境: Kali机器:192.168.163.13 ...
- [学习笔记]修改关键跳无效且关键CALL又不存在的情况
先用DI查下壳,VC++写的,无壳. 然后,打开软件看一下软件注册的情况 有弹窗,那载入OD看看能不能搜索到字符串 回到反汇编窗口,发现有两个JE都跳过了注册成功的代码 似乎很简单的样子,只要NOP掉 ...
- logstash常用插件解析
官方地址:https://www.elastic.co/guide/en/logstash-versioned-plugins/current/index.html 配置文件写法: # 日志导入inp ...