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

本文内容纲要:Spring中基于Java的配置@Configuration和@Bean用法

转自:https://blog.csdn.net/vvhesj/article/details/47661001

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

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

1 <?xml version="1.0" encoding="UTF-8"?>  

2 <beans xmlns="http://www.springframework.org/schema/beans"

3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"

4 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"

5 xmlns:context="http://www.springframework.org/schema/context"

6 xsi:schemaLocation="

7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd

10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

11 <context:component-scan base-package="SpringStudy.Model">

12 </context:component-scan>

13 </beans>

二、定义一个配置类

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

代码如下:

1     package SpringStudy;  

2 import org.springframework.context.annotation.Bean;

3 import org.springframework.context.annotation.Configuration;

4 import SpringStudy.Model.Counter;

5 import SpringStudy.Model.Piano;

6

7 @Configuration

8 public class SpringConfig {

9

10 @Bean

11 public Piano piano(){

12 return new Piano();

13 }

14 @Bean(name = "counter")

15 public Counter counter(){

16 return new Counter(12,"Shake it Off",piano());

17 }

18 }

三、基础类代码

Counter:

1     package SpringStudy.Model;  

2

3 public class Counter {

4 public Counter() {

5 }

6

7 public Counter(double multiplier, String song,Instrument instrument) {

8 this.multiplier = multiplier;

9 this.song = song;

10 this.instrument=instrument;

11 }

12

13 private double multiplier;

14

15 private String song;

16

17 @Resource

18 private Instrument instrument;

19

20 public double getMultiplier() {

21 return multiplier;

22 }

23

24 public void setMultiplier(double multiplier) {

25 this.multiplier = multiplier;

26 }

27

28

29 public String getSong() {

30 return song;

31 }

32

33 public void setSong(String song) {

34 this.song = song;

35 }

36

37 public Instrument getInstrument() {

38 return instrument;

39 }

40

41 public void setInstrument(Instrument instrument) {

42 this.instrument = instrument;

43 }

44

45 }

Piano类

1     package SpringStudy.Model;  

2

3

4 public class Piano {

5 private String name="Piano";

6 private String sound;

7

8 public String getName() {

9 return name;

10 }

11

12 public void setName(String name) {

13 this.name = name;

14 }

15

16 public String getSound() {

17 return sound;

18 }

19

20 public void setSound(String sound) {

21 this.sound = sound;

22 }

23

24 }

四、调用测试类

1     package webMyBatis;  

2

3 import org.springframework.context.ApplicationContext;

4 import org.springframework.context.annotation.AnnotationConfigApplicationContext;

5 import SpringStudy.Model.Counter;

6

7 public class SpringTest {

8 public static void main(String[] args) {

9 //ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/bean.xml");// 读取bean.xml中的内容

10 ApplicationContext annotationContext = new AnnotationConfigApplicationContext("SpringStudy");

11 Counter c = annotationContext.getBean("counter", Counter.class);// 创建bean的引用对象

12 System.out.println(c.getMultiplier());

13 System.out.println(c.isEquals());

14 System.out.println(c.getSong());

15 System.out.println(c.getInstrument().getName());

16 }

17 }

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

原文链接:https://www.cnblogs.com/sharpest/p/7748549.html

以上是 Spring中基于Java的配置@Configuration和@Bean用法 的全部内容, 来源链接: utcz.com/z/296487.html

回到顶部