标签: annotations

定义上的@NonNull 注释-java 8

如何@NonNullList项目上使用注释。

让我们考虑一下,如果我想强制一个非空的字符串列表

我们可以这样声明: @NonNull List<String>

如果我们想强制,一个非空字符串列表。

我们怎么能做到这一点?

java annotations non-nullable java-8

1
推荐指数
1
解决办法
2422
查看次数

Component-Scan 和 @Component 之间的区别?

我对这些注释有点困惑,因为我对 Spring 很陌生。我试图在谷歌上找到它并找到了很多答案,但仍然没有弄清楚。我开始知道@Component 是@Repository、@Service 和@Controller 的超级注释,但我仍然怀疑何时使用@Component 以及何时使用@ComponentScan 谁能帮我清楚地了解这两者注释,以及两者的区别。

spring annotations

1
推荐指数
1
解决办法
9236
查看次数

如何在注释处理器中获取注释方法的返回类型?

我正在学习编写自定义注释。我有一个简单的注释,需要验证方法的返回类型是否与注释中指定的返回类型匹配。下面是代码。

注释代码:

@Target(ElementType.METHOD)
public @interface ReturnCheck {
    String value() default "void";
}
Run Code Online (Sandbox Code Playgroud)

注释处理器:

@SupportedAnnotationTypes("com.rajesh.customannotations.ReturnCheck")
public class ReturnCheckProcessor extends AbstractProcessor {

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {

        for ( Element element : roundEnv.getElementsAnnotatedWith(ReturnCheck.class) ) {

            //Get return type of the method


        }

        return false;
    }

}
Run Code Online (Sandbox Code Playgroud)

我想获取注释方法的返回类型,以便我可以将其与注释中指定的值进行比较。

如何获取方法的返回类型?

java annotations

1
推荐指数
1
解决办法
1096
查看次数

拆箱 cameraCharacteristics 可能会产生 Nullpointer Exception 和 FindBugs

我无法处理导致FindBugs抛出错误的以下警告。

错误

我正在使用camera2 api。如您所见,我已经在检查 null 并另外捕获可能的 NullPointer 异常。CameraCharacteristics 类的 .get 方法使用 Nullable 进行注释,因此出现此错误。我不知道如何防止这种情况发生。检查 null 似乎不能完成这项工作。

同时,我将 SuppressFBWarnings Annotation 添加到我的项目中。但即使我抑制这样的警告:

@SuppressFBWarnings(value = "NP_NULL_ON_SOME_PATH")
private void setUpCamera(int width, int height) {
    try {
        for (String cameraId : cameraManager.getCameraIdList()) {
            CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId);

            if (cameraCharacteristics != null && cameraCharacteristics.get(CameraCharacteristics.LENS_FACING) != null) {
                int lensFaceingCameraCharacteristics = cameraCharacteristics.get(CameraCharacteristics.LENS_FACING);

                if (cameraFacing == lensFaceingCameraCharacteristics) {
                    StreamConfigurationMap streamConfigurationMap = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
                    previewSize = getPreviewSize(streamConfigurationMap.getOutputSizes(SurfaceTexture.class), width, height);
                    this.cameraId = cameraId;
                }
            }
        }
    } catch (CameraAccessException | NullPointerException …
Run Code Online (Sandbox Code Playgroud)

java android unboxing annotations findbugs

1
推荐指数
1
解决办法
1586
查看次数

使用 @Value 获取 int 值

我有 application.properties 文件,我成功地使用 @Value 从中获取了字符串值。我在从中获取 int 时遇到问题。

jedisHostName=127.0.0.1
redisPort=6379
Run Code Online (Sandbox Code Playgroud)

在我的配置类中,我有

@Value("${jedisHostName}")
private String hostName;
Run Code Online (Sandbox Code Playgroud)

它工作正常,但是当我尝试

@Value("#{new Integer.parseInt('${redisPort}')}")
private Integer redisPort;
Run Code Online (Sandbox Code Playgroud)

我得到

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'secret***': Unsatisfied dependency expressed through field 'redisPort';
Run Code Online (Sandbox Code Playgroud)

我也只尝试

@Value("#{new Integer('${redisPort}')}")
Run Code Online (Sandbox Code Playgroud)

但我得到同样的例外。我什至试图简单地做一个

@Value("${redisPort}")
private String redisPort;

int jedisPort = Integer.parseInt(redisPort.trim());
Run Code Online (Sandbox Code Playgroud)

但后来我得到

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'secret***' defined in file [secret***.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate **** Constructor threw exception; nested exception is java.lang.NullPointerException
Run Code Online (Sandbox Code Playgroud)

我有普通的类名,但我使用“secret***”作为例子

java spring annotations

1
推荐指数
1
解决办法
4249
查看次数

SpringBootTest 注解无法解析为类型

我的 SpringBootTest 注释无法解析为类型。这里有同样的问题,但似乎添加了依赖项

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>2.0.5.RELEASE</version>
    <scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

不能做的伎俩。整个问题是我想使用 @ContextConfiguration 但它已被弃用,建议的处理方式是使用 @SpringBootTest(classes = MyMainClass.class)

java spring annotations maven

1
推荐指数
2
解决办法
1万
查看次数

java如何在属性中配置注释调度程序fixedDelay

@Scheduled(cron = "0 10 0 5 * ?")   
//@Scheduled(fixedDelay = 10000)
public void task() {
}
Run Code Online (Sandbox Code Playgroud)

有没有办法配置:

在测试环境中:预定选择fixedDelay = 10000

在产品环境中:预定选择 cron = "0 10 0 5 * ?"

这个注解语法糖filed=value在java中叫什么?

java spring annotations config

1
推荐指数
1
解决办法
1168
查看次数

如何在 Spring Boot 应用程序中排除给定运行时配置文件中的包

在我的应用我有两个配置文件devprod,我可以用排除豆类org.springframework.context.annotation.Profile注释:

package com.example.prod;

@Service
@Profile("prod")
public class MyService implements MyBaseService {}
Run Code Online (Sandbox Code Playgroud)

问题是,我有几个以这种方式注释的 bean,它们都在同一个 package 中com.example.proddev配置文件(com.example.dev包)存在类似的结构,将来我可能会有一些额外的配置文件。是否有可能一次排除整个包裹?我尝试使用org.springframework.context.annotation.ComponentScan,但无法根据实际配置文件添加排除过滤器,我想知道是否有简单的方法可以解决我的问题。

java profile spring annotations spring-boot

1
推荐指数
1
解决办法
3521
查看次数

Lombok 的 @NonNull 或 javax @Nonnull

龙目岛的@NonNull VS javax.annotation.Nonnull

哪个更适合用于方法参数以及何时使用?

这里的答案模糊地比较了所有注释,但没有推断出哪一个最好。我只是想知道两者中哪个更好。

java null annotations non-nullable lombok

1
推荐指数
1
解决办法
1万
查看次数

添加文本注释以从熊猫数据框中绘图

我的代码:

import matplotlib.pyplot as plt
import pandas as pd
import os, glob

path = r'C:/Users/New folder'
all_files = glob.glob(os.path.join(path, "*.txt"))
df = pd.DataFrame()
for file_ in all_files:
    file_df = pd.read_csv(file_,sep=',', parse_dates=[0], infer_datetime_format=True,header=None, usecols=[0,1,2,3,4,5,6], names=['Date','Time','open', 'high', 'low', 'close','volume','tradingsymbol'])

df = df[['Date','Time','close','volume','tradingsymbol']]
df["Time"] = pd.to_datetime(df['Time'])
df.set_index('Time', inplace=True)
print(df)

fig, axes = plt.subplots(nrows=2, ncols=1)
################### Volume ###########################
df.groupby('tradingsymbol')['volume'].plot(legend=True, rot=0, grid=True, ax=axes[0])
################### PRICE ###########################
df.groupby('tradingsymbol')['close'].plot(legend=True, rot=0, grid=True, ax=axes[1])

plt.show()
Run Code Online (Sandbox Code Playgroud)

我的当前输出是这样的: 输出

我需要向 matplotlib 图中添加文本注释。我想要的输出类似于下图想要的

python annotations matplotlib python-3.x pandas

1
推荐指数
1
解决办法
2955
查看次数