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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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()); 
    
}

三、基础类代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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; 
    
   
}

四、调用测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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

posted @   人艰不拆_zmc  阅读(486)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2016-08-10 浅谈 man 命令的日常使用
2016-08-10 Linux命令行–走进shell
2016-08-10 Linux命令行–初识Linux shell
2014-08-10 poj3468A Simple Problem with Integers(线段树的区域更新)
2014-08-10 hdu1166敌兵布阵&&hdu1754I Hate It(线段树入门)
点击右上角即可分享
微信分享提示