`

springboo-t杂记

阅读更多

    Spring中为了减少xml中配置可以在类中添加注释来对bean进行配置。

 

    1、定义一个配置类

 

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

package com.wanxx;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Config {
    @Value(value = "${dubbo.port}")
    private String port = "";

    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                container.setPort(Integer.parseInt(port));
            }
        };
    }
}

 在项目启动的时间就会加载Config类。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics