Skip to content

Commit

Permalink
[ISSUE-alibaba#4256] Just inject environment in StartingSpringApplica…
Browse files Browse the repository at this point in the history
…tionRunListener (alibaba#4257)

* just inject environment in StartingSpringApplicationRunListener

* make nacosStartingListener is decoupling with springApplicationRunListener.

* add api doc

* refactor. transfer nacos listeners to SpringApplicationRunListener.

* remove unuseful import

* add doc info
  • Loading branch information
horizonzy committed Nov 24, 2020
1 parent 3d97e36 commit 7f2a330
Show file tree
Hide file tree
Showing 9 changed files with 274 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ public String getProperty(String key, String defaultValue) {

@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
EnvUtil.setEnvironment(configurableApplicationContext.getEnvironment());
loadSetting();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* 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 com.alibaba.nacos.core.code;

import com.alibaba.nacos.core.listener.LoggingApplicationListener;
import com.alibaba.nacos.core.listener.NacosApplicationListener;
import com.alibaba.nacos.core.listener.StartingApplicationListener;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.EventPublishingRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.ArrayList;
import java.util.List;

/**
* {@link org.springframework.boot.SpringApplicationRunListener} before {@link EventPublishingRunListener} execution.
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
* @since 0.2.2
*/
public class SpringApplicationRunListener implements org.springframework.boot.SpringApplicationRunListener, Ordered {

private final SpringApplication application;

private final String[] args;

private List<NacosApplicationListener> nacosApplicationListeners = new ArrayList<>();

{
nacosApplicationListeners.add(new LoggingApplicationListener());
nacosApplicationListeners.add(new StartingApplicationListener());
}

public SpringApplicationRunListener(SpringApplication application, String[] args) {
this.application = application;
this.args = args;
}

@Override
public void starting() {
for (NacosApplicationListener nacosApplicationListener : nacosApplicationListeners) {
nacosApplicationListener.starting();
}
}

@Override
public void environmentPrepared(ConfigurableEnvironment environment) {
for (NacosApplicationListener nacosApplicationListener : nacosApplicationListeners) {
nacosApplicationListener.environmentPrepared(environment);
}
}

@Override
public void contextPrepared(ConfigurableApplicationContext context) {
for (NacosApplicationListener nacosApplicationListener : nacosApplicationListeners) {
nacosApplicationListener.contextPrepared(context);
}
}

@Override
public void contextLoaded(ConfigurableApplicationContext context) {
for (NacosApplicationListener nacosApplicationListener : nacosApplicationListeners) {
nacosApplicationListener.contextLoaded(context);
}
}

@Override
public void started(ConfigurableApplicationContext context) {
for (NacosApplicationListener nacosApplicationListener : nacosApplicationListeners) {
nacosApplicationListener.started(context);
}
}

@Override
public void running(ConfigurableApplicationContext context) {
for (NacosApplicationListener nacosApplicationListener : nacosApplicationListeners) {
nacosApplicationListener.running(context);
}
}

@Override
public void failed(ConfigurableApplicationContext context, Throwable exception) {
for (NacosApplicationListener nacosApplicationListener : nacosApplicationListeners) {
nacosApplicationListener.failed(context, exception);
}
}

/**
* Before {@link EventPublishingRunListener}.
*
* @return HIGHEST_PRECEDENCE
*/
@Override
public int getOrder() {
return HIGHEST_PRECEDENCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public class StandaloneProfileApplicationListener
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {

ConfigurableEnvironment environment = event.getEnvironment();
EnvUtil.setEnvironment(environment);


if (environment.getProperty(STANDALONE_MODE_PROPERTY_NAME, boolean.class, false)) {
environment.addActiveProfile(STANDALONE_SPRING_PROFILE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,49 +14,35 @@
* limitations under the License.
*/

package com.alibaba.nacos.core.code;
package com.alibaba.nacos.core.listener;

import com.alibaba.nacos.sys.env.EnvUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.boot.context.event.EventPublishingRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;

import static org.springframework.boot.context.logging.LoggingApplicationListener.CONFIG_PROPERTY;
import static org.springframework.core.io.ResourceLoader.CLASSPATH_URL_PREFIX;

/**
* Logging {@link SpringApplicationRunListener} before {@link EventPublishingRunListener} execution.
* For init logging configuration.
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
* @since 0.2.2
* @author horizonzy
* @since 1.4.1
*/
public class LoggingSpringApplicationRunListener implements SpringApplicationRunListener, Ordered {
public class LoggingApplicationListener implements NacosApplicationListener {

private static final String DEFAULT_NACOS_LOGBACK_LOCATION = CLASSPATH_URL_PREFIX + "META-INF/logback/nacos.xml";

private static final Logger LOGGER = LoggerFactory.getLogger(LoggingSpringApplicationRunListener.class);

private final SpringApplication application;

private final String[] args;

public LoggingSpringApplicationRunListener(SpringApplication application, String[] args) {
this.application = application;
this.args = args;
}
private static final Logger LOGGER = LoggerFactory.getLogger(LoggingApplicationListener.class);

@Override
public void starting() {

}

@Override
public void environmentPrepared(ConfigurableEnvironment environment) {
EnvUtil.setEnvironment(environment);
if (!environment.containsProperty(CONFIG_PROPERTY)) {
System.setProperty(CONFIG_PROPERTY, DEFAULT_NACOS_LOGBACK_LOCATION);
if (LOGGER.isInfoEnabled()) {
Expand Down Expand Up @@ -91,14 +77,4 @@ public void running(ConfigurableApplicationContext context) {
public void failed(ConfigurableApplicationContext context, Throwable exception) {

}

/**
* Before {@link EventPublishingRunListener}.
*
* @return HIGHEST_PRECEDENCE
*/
@Override
public int getOrder() {
return HIGHEST_PRECEDENCE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* 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 com.alibaba.nacos.core.listener;

import com.alibaba.nacos.core.code.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

/**
* Nacos Application Listener, execute init process.
*
* @author horizonzy
* @since 1.4.1
*/
public interface NacosApplicationListener {

/**
* {@link SpringApplicationRunListener#starting}.
*/
void starting();

/**
* {@link com.alibaba.nacos.core.code.SpringApplicationRunListener#environmentPrepared}.
*
* @param environment environment
*/
void environmentPrepared(ConfigurableEnvironment environment);

/**
* {@link com.alibaba.nacos.core.code.SpringApplicationRunListener#contextLoaded}.
*
* @param context context
*/
void contextPrepared(ConfigurableApplicationContext context);

/**
* {@link com.alibaba.nacos.core.code.SpringApplicationRunListener#contextLoaded}.
*
* @param context context
*/
void contextLoaded(ConfigurableApplicationContext context);

/**
* {@link com.alibaba.nacos.core.code.SpringApplicationRunListener#started}.
*
* @param context context
*/
void started(ConfigurableApplicationContext context);

/**
* {@link com.alibaba.nacos.core.code.SpringApplicationRunListener#running}.
*
* @param context context
*/
void running(ConfigurableApplicationContext context);

/**
* {@link com.alibaba.nacos.core.code.SpringApplicationRunListener#failed}.
*
* @param context context
* @param exception exception
*/
void failed(ConfigurableApplicationContext context, Throwable exception);
}
Loading

0 comments on commit 7f2a330

Please sign in to comment.