Skip to content

Commit

Permalink
[ISSUE #321] Support spring 6.x (#324)
Browse files Browse the repository at this point in the history
* [ISSUE #315] Support spring 6.x
  • Loading branch information
SuperZ1999 authored Oct 6, 2023
1 parent e8cae2c commit a61a02c
Show file tree
Hide file tree
Showing 17 changed files with 1,123 additions and 21 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor;
import com.alibaba.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor;
import com.alibaba.spring.util.BeanUtils;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.List;
import java.util.Map;

import com.alibaba.nacos.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
Expand All @@ -48,7 +49,6 @@
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.spring.context.event.config.NacosConfigReceivedEvent;
import com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor;

/**
* Injected {@link NacosValue}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ public class NacosPropertySourcePostProcessor
*/
public static final String BEAN_NAME = "nacosPropertySourcePostProcessor";

private static BeanFactory beanFactory;
protected static BeanFactory beanFactory;

private final Set<String> processedBeanNames = new LinkedHashSet<String>();
protected final Set<String> processedBeanNames = new LinkedHashSet<String>();

private ConfigurableEnvironment environment;

private Collection<AbstractNacosPropertySourceBuilder> nacosPropertySourceBuilders;
protected Collection<AbstractNacosPropertySourceBuilder> nacosPropertySourceBuilders;

private ConfigServiceBeanBuilder configServiceBeanBuilder;
protected ConfigServiceBeanBuilder configServiceBeanBuilder;

public static void addListenerIfAutoRefreshed(
final NacosPropertySource nacosPropertySource, final Properties properties,
Expand Down Expand Up @@ -170,7 +170,7 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)

}

private void processPropertySource(String beanName,
protected void processPropertySource(String beanName,
ConfigurableListableBeanFactory beanFactory) {

if (processedBeanNames.contains(beanName)) {
Expand All @@ -179,6 +179,12 @@ private void processPropertySource(String beanName,

BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);

doProcessPropertySource(beanName, beanDefinition);

processedBeanNames.add(beanName);
}

protected void doProcessPropertySource(String beanName, BeanDefinition beanDefinition) {
// Build multiple instance if possible
List<NacosPropertySource> nacosPropertySources = buildNacosPropertySources(
beanName, beanDefinition);
Expand All @@ -190,8 +196,6 @@ private void processPropertySource(String beanName,
.resolveProperties(nacosPropertySource.getAttributesMetadata());
addListenerIfAutoRefreshed(nacosPropertySource, properties, environment);
}

processedBeanNames.add(beanName);
}

private List<NacosPropertySource> buildNacosPropertySources(String beanName,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
*
* * Licensed to the Apache Software Foundation (ASF) under one or more
* * contributor license agreements. See the NOTICE file distributed with
* * this work for additional information regarding copyright ownership.
* * The ASF licenses this file to You 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.spring.beans.factory.annotation;

import org.junit.Assert;
import org.junit.Test;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;

/**
* {@link AbstractAnnotationBeanPostProcessor} Test
* @author SuperZ1999
* @date 2023/9/28
*/
public class AbstractAnnotationBeanPostProcessorTest {
@Test
public void testPropertyDescriptor() throws IntrospectionException {
PropertyDescriptor pd = new PropertyDescriptor("name", Student.class);
Class<?> type = pd.getPropertyType();
Assert.assertEquals(type, String.class);
}

static class Student {
String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
*
* * Licensed to the Apache Software Foundation (ASF) under one or more
* * contributor license agreements. See the NOTICE file distributed with
* * this work for additional information regarding copyright ownership.
* * The ASF licenses this file to You 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.spring.context.annotation.config;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.core.env.MapPropertySource;

import java.util.HashMap;

/**
* {@link NacosPropertySourceBuilder} Test
* @author SuperZ1999
* @date 2023/9/28
*/
public class NacosPropertySourceBuilderTest {
@Test
public void test() {
MapPropertySource propertySource = new MapPropertySource("test", new HashMap<>());
Assert.assertNotNull(propertySource);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
*
* * Licensed to the Apache Software Foundation (ASF) under one or more
* * contributor license agreements. See the NOTICE file distributed with
* * this work for additional information regarding copyright ownership.
* * The ASF licenses this file to You 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.spring.context.annotation.config;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.TypeConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.core.MethodParameter;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.lang.reflect.Method;

/**
* {@link NacosValueAnnotationBeanPostProcessor} Test
* @author SuperZ1999
* @date 2023/9/28
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = NacosValueAnnotationBeanPostProcessorTest.class)
public class NacosValueAnnotationBeanPostProcessorTest {

@Autowired
private ConfigurableListableBeanFactory beanFactory;

@Bean
public NacosValueAnnotationBeanPostProcessor nacosValueAnnotationBeanPostProcessor() {
return new NacosValueAnnotationBeanPostProcessor();
}

@Test
public void testConvertIfNecessary() throws NoSuchMethodException {
TypeConverter converter = beanFactory.getTypeConverter();
Method method = NacosValueAnnotationBeanPostProcessorTest.class.getMethod("testMethodParameter", Integer.class);
Integer integer = converter.convertIfNecessary("12", Integer.class, new MethodParameter(method, 0));
System.out.println(integer);
Assert.assertEquals(integer, Integer.valueOf(12));
}

public void testMethodParameter(Integer i) {
System.out.println(i);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
*
* * Licensed to the Apache Software Foundation (ASF) under one or more
* * contributor license agreements. See the NOTICE file distributed with
* * this work for additional information regarding copyright ownership.
* * The ASF licenses this file to You 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.spring.context.config.xml;

import org.junit.Assert;
import org.junit.Test;

/**
* {@link NacosPropertySourceXmlBeanDefinition} Test
* @author SuperZ1999
* @date 2023/9/28
*/
public class NacosPropertySourceXmlBeanDefinitionTest {
@Test
public void test() {
NacosPropertySourceXmlBeanDefinition beanDefinition = new NacosPropertySourceXmlBeanDefinition();
Assert.assertNotNull(beanDefinition);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
*
* * Licensed to the Apache Software Foundation (ASF) under one or more
* * contributor license agreements. See the NOTICE file distributed with
* * this work for additional information regarding copyright ownership.
* * The ASF licenses this file to You 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.spring.context.properties.config;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.validation.BindException;
import org.springframework.validation.DataBinder;

/**
* {@link DataBinder} Test
* @author SuperZ1999
* @date 2023/9/28
*/
public class DataBinderTest {
@Test
public void test() throws BindException {
People people = new People();
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("name", "SuperZ1999");
propertyValues.add("age", 24);
DataBinder dataBinder = new DataBinder(people);
dataBinder.setAutoGrowNestedPaths(false);
dataBinder.setIgnoreInvalidFields(false);
dataBinder.setIgnoreUnknownFields(true);
dataBinder.bind(propertyValues);
dataBinder.close();

Assert.assertEquals("SuperZ1999", people.getName());
Assert.assertEquals(24, people.getAge());
}

static class People {
private String name;
private int age;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
*
* * Licensed to the Apache Software Foundation (ASF) under one or more
* * contributor license agreements. See the NOTICE file distributed with
* * this work for additional information regarding copyright ownership.
* * The ASF licenses this file to You 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.spring.factory;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.pojo.Service;
import com.alibaba.nacos.client.naming.NacosNamingMaintainService;
import org.junit.Assert;
import org.junit.Test;

/**
* {@link DelegatingNamingMaintainService} Test
* @author SuperZ1999
* @date 2023/9/28
*/
public class DelegatingNamingMaintainServiceTest {
@Test
public void testNamingMaintainService() throws NacosException {
NacosNamingMaintainService namingMaintainService = new NacosNamingMaintainService("127.0.0.1:8848");
Service service = namingMaintainService.queryService("example");
Assert.assertNotNull(service);
}
}
Loading

0 comments on commit a61a02c

Please sign in to comment.