Resource里特殊值$改成#

This commit is contained in:
redkale
2023-07-31 08:36:54 +08:00
parent 4804eed0a7
commit 25a7aeacfa
7 changed files with 34 additions and 24 deletions

View File

@@ -8,7 +8,7 @@ package org.redkale.annotation;
import java.lang.annotation.*;
/**
* &#64;Resource(name = "$") 表示资源name采用所属对象的name <br>
* &#64;Resource(name = "#") 表示资源name采用所属对象的name <br>
* &#64;Resource(name = "@name") 表示资源对象自身的name <br>
* &#64;Resource(name = "@type") 表示资源对象自身的类型 <br>
*
@@ -20,7 +20,7 @@ import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
public @interface Resource {
public static final String PARENT_NAME = "$";
public static final String PARENT_NAME = "#";
public static final String SELF_NAME = "@name";
@@ -39,7 +39,7 @@ public @interface Resource {
* 资源名称 <br>
* <blockquote><pre>
* name规则:
* 1: "$"有特殊含义, 表示资源本身,"$"不能单独使用
* 1: "#"有特殊含义, 表示资源本身,"#"不能单独使用
* 2: "@name"、"@type"有特殊含义
* 3: 只能是字母、数字、(短横)-、(下划线)_、点(.)的组合
* </pre></blockquote>

View File

@@ -458,8 +458,8 @@ public abstract class NodeServer {
continue;
}
}
if (entry.getName().contains("$")) {
throw new RedkaleException("<name> value cannot contains '$' in " + entry.getProperty());
if (entry.getName().contains(Resource.PARENT_NAME)) {
throw new RedkaleException("<name> value cannot contains '" + Resource.PARENT_NAME + "' in " + entry.getProperty());
}
if (!entry.isEmptyGroup() && !entry.isRemote() && rpcGroups.containsGroup(entry.getGroup())) {
throw new RedkaleException("Not found group(" + entry.getGroup() + ")");

View File

@@ -52,13 +52,13 @@ public abstract class WebSocketNode implements Service {
@RpcRemote
protected WebSocketNode remoteNode;
@Resource(name = "$_sendconvert", required = false)
@Resource(name = Resource.PARENT_NAME + "_sendconvert", required = false)
protected Convert sendConvert;
//存放所有用户分布在节点上的队列信息,Set<WebSocketAddress> 为 sncpnode 的集合, key: groupid
//集合包含 localSncpAddress
//如果不是分布式(没有SNCP)source 将不会被用到
@Resource(name = "$", required = false)
@Resource(name = Resource.PARENT_NAME, required = false)
protected CacheSource source;
//当前节点的本地WebSocketEngine

View File

@@ -111,7 +111,7 @@ public abstract class WebSocketServlet extends HttpServlet implements Resourcabl
@Resource(name = "$_sendconvert", required = false)
protected Convert sendConvert;
@Resource(name = "$")
@Resource(name = Resource.PARENT_NAME)
protected WebSocketNode webSocketNode;
@Resource(name = RESNAME_SERVER_RESFACTORY)

View File

@@ -5,11 +5,11 @@
*/
package org.redkale.service;
import org.redkale.util.*;
import org.redkale.util.AnyValue;
/**
* 所有Service的实现类不得声明为final 允许远程模式的public方法都不能声明为final。<br>
* 注意: "$"是一个很特殊的Service.name值 。 被标记为&#64;Resource(name = "$") 的Service的资源名与所属父Service的资源名一致。<br>
* 注意: "#"是一个很特殊的Service.name值 。 被标记为&#64;Resource(name = "#") 的Service的资源名与所属父Service的资源名一致。<br>
*
* <blockquote><pre>
* Service的资源类型

View File

@@ -24,11 +24,11 @@ import org.redkale.convert.*;
*
* 依赖注入功能主类 <br>
*
* 如果&#64;Resource(name = "$") 表示资源name采用所属对象的name <br>
* 如果&#64;Resource(name = "#") 表示资源name采用所属对象的name <br>
* 如果没有&#64;Resource且对象实现了Resourcable, 则会取对象的resourceName()方法值
* <blockquote><pre>
* name规则:
* 1: "$"有特殊含义, 表示资源本身,"$"不能单独使用
* 1: "#"有特殊含义, 表示资源本身,"#"不能单独使用
* 2: "@name"、"@type"有特殊含义
* 3: 只能是字母、数字、(短横)-、(下划线)_、点(.)的组合
* </pre></blockquote>
@@ -124,7 +124,7 @@ public final class ResourceFactory {
* 检查资源名是否合法
* <blockquote><pre>
* name规则:
* 1: "$"有特殊含义, 表示资源本身,"$"不能单独使用
* 1: "#"有特殊含义, 表示资源本身,"#"不能单独使用
* 2: "@name"、"@type"有特殊含义
* 3: 只能是字母、数字、(短横)-、(下划线)_、点(.)的组合
* </pre></blockquote>
@@ -912,9 +912,19 @@ public final class ResourceFactory {
|| classType == Float.class || classType == Double.class
|| classType == BigInteger.class || classType == BigDecimal.class) {
re = findEntry(rcname, String.class);
if (re == null && rcname.startsWith("property.")) { //兼容2.8.0之前版本自动追加property.开头的配置项
if (re == null && rcname.startsWith("${")) {
if (rcname.charAt(rcname.length() - 1) != '}') {
throw new ResourceInjectException("resource(type=" + field.getType().getSimpleName() + ".class, field=" + field.getName() + ", name='" + rcname + "') not endWith } ");
}
re = findEntry(rcname.substring(2, rcname.length() - 1), String.class);
} else if (re == null && rcname.startsWith("property.")) { //兼容2.8.0之前版本自动追加property.开头的配置项
re = findEntry(rcname.substring("property.".length()), String.class);
}
} else if (re == null && rcname.startsWith("${")) {
if (rcname.charAt(rcname.length() - 1) != '}') {
throw new ResourceInjectException("resource(type=" + field.getType().getSimpleName() + ".class, field=" + field.getName() + ", name='" + rcname + "') not endWith } ");
}
re = findEntry(rcname.substring(2, rcname.length() - 1), String.class);
} else if (classType == String.class && rcname.startsWith("property.")) {//兼容2.8.0之前版本自动追加property.开头的配置项
re = findEntry(rcname.substring("property.".length()), String.class);
} else {

View File

@@ -5,11 +5,11 @@
*/
package org.redkale.test.util;
import java.math.*;
import java.math.BigInteger;
import java.util.Properties;
import org.redkale.annotation.*;
import org.junit.jupiter.api.Test;
import org.redkale.annotation.ConstructorParameters;
import org.redkale.annotation.*;
import org.redkale.annotation.ResourceListener;
import org.redkale.util.*;
@@ -30,7 +30,7 @@ public class ResourceTest {
@Test
public void run() throws Exception {
ResourceFactory factory = ResourceFactory.create();
factory.register("property.id", "2345"); //注入String类型的property.id
factory.register("id", "2345"); //注入String类型的property.id
AService aservice = new AService();
BService bservice = new BService("eeeee");
@@ -47,13 +47,13 @@ public class ResourceTest {
factory.register("bigint", new BigInteger("666666666666666")); //放进Resource池内, 同时ResourceFactory会自动更新aservice对象的bigint值
System.out.println(aservice); //输出结果为:{id:"2345", intid: 2345, bigint:666666666666666, bservice:{name:eeeee}} 可以看出seqid与bigint值都已自动更新
factory.register("property.id", "6789"); //更新Resource池内的id资源值, 同时ResourceFactory会自动更新aservice、bservice的id值
factory.register("id", "6789"); //更新Resource池内的id资源值, 同时ResourceFactory会自动更新aservice、bservice的id值
System.out.println(aservice); //输出结果为:{id:"6789", intid: 6789, bigint:666666666666666, bservice:{name:eeeee}}
System.out.println(bservice); //输出结果为:{name:"eeeee", id: 6789, aserivce:{id:"6789", intid: 6789, bigint:666666666666666, bservice:{name:eeeee}}}
Properties props = new Properties();
props.put("property.id", "5555");
props.put("property.desc", "my desc");
props.put("id", "5555");
props.put("desc", "my desc");
factory.register(props);
bservice = new BService("ffff");
@@ -67,10 +67,10 @@ public class ResourceTest {
class BService {
@Resource(name = "property.id")
@Resource(name = "${id}")
private String id;
@Resource(name = "property.desc", required = false)
@Resource(name = "${desc}", required = false)
private String desc;
@Resource
@@ -122,10 +122,10 @@ class BService {
class AService {
@Resource(name = "property.id")
@Resource(name = "${id}")
private String id;
@Resource(name = "property.id") //property.开头的资源名允许String自动转换成primitive数值类型
@Resource(name = "id") //property.开头的资源名允许String自动转换成primitive数值类型
private int intid;
@Resource(name = "bigint", required = false)