梦境迷离

梦境亦是美,醒来亦是空。

项目重构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中的效果是一摸一样的。



阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34446485/article/details/80333851
文章标签: Scala
个人分类: Scala
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

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

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭