1先在一个类上面注册ConfigurationProperties s使他读取配置文件信息
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@Data
@Getter
@ConfigurationProperties(prefix = “zzy.fjgx.zj”)
public class Common {
//控制金额
private String pjqsje;
private String xhzfbl;
private String addr;
}
2需要在配置类中加载进去
@ComponentScan(“com.inspur.zzy.fjgx”)将类扫描进容器
@EnableConfigurationProperties({Common.class}) 一样的作用大概将那个类加载进去 并声明事读取的外部文件 个人理解
import com.inspur.zzy.fjgx.fsex.core.service.FSExtendImpl;
import com.inspur.zzy.fjgx.fsex.core.utils.TRTSQLUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.inspur.zzy.fjgx.fsex.*;
@Configuration(proxyBeanMethods = false)
@Slf4j
@EnableConfigurationProperties({Common.class})
@ComponentScan(“com.inspur.zzy.fjgx”)
public class FSExAutoConfiguation {
public FSExAutoConfiguation() {
}
@Bean("com.inspur.zzy.fjgx.fsex.core.createfsex")
public FSExtend createFSExtend() {
log.debug("加载FSExtend");
return new FSExtendImpl();
}
@Bean("com.inspur.zzy.fjgx.core.utils")
public TRTSQLUtils createTRTSQLUtils(){
log.debug("加载TRTSQLUtils");
return new TRTSQLUtils();
}
@Bean("com.inspur.zzy.fjgx.core.common")
public Common createCommon(){
log.debug("加载createCommon");
return new Common();
}
}
3
@Autowired
private Common common;
将类注入到要用的类即可