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

5 篇文章 0 订阅

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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
        
        <bean id="people" class="com.java.people.People">
            <property name="id" value="1"></property>
            <property name="name" value="人1"></property>
            <property name="age" value="11"></property>
            <property name="dog">    //dog属性又是一个bean
                <bean id="dog2" class="com.java.dog.Dog">
                    <property name="name" value="jack"></property>
                </bean>
            </property>
            <property name="hobbys">
                <list>
                    <value>唱歌</value>
                    <value>跳舞</value>
                    <value>111</value>
                </list>
            </property>
            <property name="loves">
                <set>
                    <value>唱歌</value>
                    <value>唱歌</value>
                    <value>跳舞</value>
                </set>
            </property>
            <property name="works">
                <map>
                    <entry>
                      <key><value>第一</value></key>    //要注意key标签里还有value标签
                      <value>上班</value>                   
                    </entry>
                    <entry>
                       <key><value>第二</value></key>
                       <value>下班</value>
                    </entry>
                </map>
            </property>
            <property name="properties">     //properties和map相似 key-- value的形式
                <props>
                    <prop key="上午">加班</prop>    
                    <prop key="下午">还是加班</prop>
                </props>
            </property>
        </bean>
        
        <bean id="people2" class="com.java.people.People">
            <property name="id" value="2"></property>
            <property name="name" value="人2"></property>
            <property name="age" value="22"></property>
            <property name="dog">
                <null></null>
            </property>
        </bean>



          <bean id="people" class="com.java.people.People">
            <property name="id" value="1"></property>
            <property name="name" value="人1"></property>
            <property name="age" value="11"></property>

            <property name="dog"  ref="dog"></property>   //ref关联dog对象

           </bean>

 

          <bean id="dog" class="com.java.dog.Dog">
                 <constructor-arg type="String" value="tom"></constructor-arg>
          </bean>
        
        
  
</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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值