项目重构01 Scala中,实现Java接口,重写Java方法

1、Java 类,打印环境变量
package cn.edu.jxnu.base;

import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * 获得环境变量
 * 
 * @author 梦境迷离
 * @version V1.0
 *
 */
@Configuration
public class MyEnvironmentAware implements EnvironmentAware {

	/**
	 * 设置环境的时候,获取属性
	 */
	@Override
	public void setEnvironment(Environment env) {
		// 通过 environment 获取到系统属性.
		System.out.println(env.getProperty("JAVA_HOME"));
		// 通过 environment 同样能获取到application.properties配置的属性.
		System.out.println(env.getProperty("spring.datasource.master.url"));
		System.out.println(env.getProperty("spring.datasource.cluster.url"));
		// 获取到前缀是"spring.datasource." 的属性列表值.
		RelaxedPropertyResolver relaxedPropertyResolver = new RelaxedPropertyResolver(env, "spring.datasource.");
		System.out.println("spring.datasource.master.driver-class-name="
				+ relaxedPropertyResolver.getProperty("master.driver-class-name"));
	}

}

2、EnvironmentAware 从源码看方法签名

/*
 * Copyright 2002-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.context;

import org.springframework.beans.factory.Aware;
import org.springframework.core.env.Environment;

/**
 * Interface to be implemented by any bean that wishes to be notified
 * of the {@link Environment} that it runs in.
 *
 * @author Chris Beams
 * @since 3.1
 * @see org.springframework.core.env.EnvironmentCapable
 */
public interface EnvironmentAware extends Aware {

	/**
	 * Set the {@code Environment} that this component runs in.
	 */
	void setEnvironment(Environment environment);

}

3、Scala 实现Java接口

package cn.edu.jxnu.base

import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import scala.Unit

/**
 * 获得环境变量
 *
 * @author 梦境迷离
 * @time 2018年5月16日
 * @version V2.0
 *
 */
@Configuration
class EnvironmentAwareUtil extends EnvironmentAware {

    /**
     * 1、extends实现Java接口
     * 2、覆盖Java的抽象方法
     * 3、override可选,入参和返回值必须相同
     * 4、如果方法访问权限是protected则可能需要使用Scala包级访问控制 protected[packageName]
     */
    override def setEnvironment(env: Environment): Unit = {
        // 通过 environment 获取到系统属性.
        println(env.getProperty("JAVA_HOME"));
        // 通过 environment 同样能获取到application.properties配置的属性.
        println(env.getProperty("spring.datasource.master.url"));
        println(env.getProperty("spring.datasource.cluster.url"));
        // 获取到前缀是"spring.datasource." 的属性列表值.
        val relaxedPropertyResolver = new RelaxedPropertyResolver(env, "spring.datasource.");
        println("spring.datasource.master.driver-class-name="
            + relaxedPropertyResolver.getProperty("master.driver-class-name"));
    }

}

在Spring中的效果是一摸一样的。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值