Java Code Examples for

The following examples show how to use . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
protected void processListenerMethod(String beanName, final Object bean,
		Class<?> beanClass, final NacosConfigListener listener, final Method method,
		ApplicationContext applicationContext) {

	final String dataId = NacosUtils.readFromEnvironment(listener.dataId(),
	final String groupId = NacosUtils.readFromEnvironment(listener.groupId(),
	final String type = StringUtils.isEmpty(NacosUtils.readTypeFromDataId(dataId))
			? listener.type().getType()
			: NacosUtils.readTypeFromDataId(dataId);
	long timeout = listener.timeout();

	Assert.isTrue(StringUtils.hasText(dataId), "dataId must have content");
	Assert.isTrue(StringUtils.hasText(groupId), "groupId must have content");
	Assert.isTrue(timeout > 0, "timeout must be greater than zero");

	ConfigService configService = configServiceBeanBuilder

	try {
		configService.addListener(dataId, groupId,
				new TimeoutNacosConfigListener(dataId, groupId, timeout) {

					protected void onReceived(String config) {
						Class<?> targetType = method.getParameterTypes()[0];
						NacosConfigConverter configConverter = determineNacosConfigConverter(
								targetType, listener, type);
						Object parameterValue = configConverter.convert(config);
						// Execute target method
						ReflectionUtils.invokeMethod(method, bean, parameterValue);
	catch (NacosException e) {
		logger.error("ConfigService can't add Listener for dataId : " + dataId
				+ " , groupId : " + groupId, e);

	publishMetadataEvent(beanName, bean, beanClass, dataId, groupId, listener,

Example 2
protected void bind(final Object bean, final String beanName,
		final NacosConfigurationProperties properties) {

	Assert.notNull(bean, "Bean must not be null!");

	Assert.notNull(properties, "NacosConfigurationProperties must not be null!");

	// support read data-id and group-id from spring environment
	final String dataId = NacosUtils.readFromEnvironment(properties.dataId(),
	final String groupId = NacosUtils.readFromEnvironment(properties.groupId(),
	String fileType = NacosUtils.readTypeFromDataId(dataId);
	final String type = StringUtils.isEmpty(fileType)
			? (properties.yaml() ? ConfigType.YAML.getType()
					: properties.type().getType())
			: fileType;

	final ConfigService configService = configServiceBeanBuilder

	// Add a Listener if auto-refreshed
	if (properties.autoRefreshed()) {

		Listener listener = new AbstractListener() {
			public void receiveConfigInfo(String config) {
				doBind(bean, beanName, dataId, groupId, type, properties, config,
		try {//
			if (configService instanceof EventPublishingConfigService) {
				((EventPublishingConfigService) configService).addListener(dataId,
						groupId, type, listener);
			else {
				configService.addListener(dataId, groupId, listener);
		catch (NacosException e) {
			if (logger.isErrorEnabled()) {
				logger.error(e.getMessage(), e);

	String content = getContent(configService, dataId, groupId);

	if (hasText(content)) {
		doBind(bean, beanName, dataId, groupId, type, properties, content,
Example 3
protected NacosPropertySource doBuild(String beanName, T beanDefinition,
		Map<String, Object> runtimeAttributes) {

	// Get annotation metadata
	String name = (String) runtimeAttributes.get(NAME_ATTRIBUTE_NAME);
	String dataId = (String) runtimeAttributes.get(DATA_ID_ATTRIBUTE_NAME);
	String groupId = (String) runtimeAttributes.get(GROUP_ID_ATTRIBUTE_NAME);
	String type = ((ConfigType) runtimeAttributes.get(CONFIG_TYPE_ATTRIBUTE_NAME))

	dataId = NacosUtils.readFromEnvironment(dataId, environment);
	groupId = NacosUtils.readFromEnvironment(groupId, environment);
	type = StringUtils.isEmpty(NacosUtils.readTypeFromDataId(dataId)) ? type
			: NacosUtils.readTypeFromDataId(dataId);
	Map<String, Object> nacosPropertiesAttributes = (Map<String, Object>) runtimeAttributes

	Properties nacosProperties = resolveProperties(nacosPropertiesAttributes,
			environment, globalNacosProperties);

	String nacosConfig = nacosConfigLoader.load(dataId, groupId, nacosProperties);

	if (!StringUtils.hasText(nacosConfig)) {
		if (logger.isWarnEnabled()) {
					"There is no content for NacosPropertySource from dataId[%s] , groupId[%s] , properties[%s].",
					dataId, groupId, nacosPropertiesAttributes));

	if (!StringUtils.hasText(name)) {
		name = buildDefaultPropertySourceName(dataId, groupId, nacosProperties);

	NacosPropertySource nacosPropertySource = new NacosPropertySource(dataId, groupId,
			name, nacosConfig, type);


	String beanClassName = beanDefinition.getBeanClassName();
	if (StringUtils.hasText(beanClassName)) {
		nacosPropertySource.setBeanType(resolveClassName(beanClassName, classLoader));

	initNacosPropertySource(nacosPropertySource, beanDefinition, runtimeAttributes);

	return nacosPropertySource;