标签: annotations

CGLib - 创建一个带有一些字段的 bean 并在其上放置注释?

是否可以生成一个 bean 类,其中的字段用指定的注释进行注释?我知道可以创建 bean,但是注释呢...?我找不到任何关于它的信息,所以我对此有强烈的怀疑,唯一确定的方法就是在这里询问......

// 我发现了一些可能有用的东西...请验证此代码。它使用 javassist 功能。

    // pool creation
    ClassPool pool = ClassPool.getDefault();
    // extracting the class
    CtClass cc = pool.getCtClass(clazz.getName());
    // looking for the method to apply the annotation on
    CtField fieldDescriptor = cc.getDeclaredField(fieldName);
    // create the annotation
    ClassFile ccFile = cc.getClassFile();
    ConstPool constpool = ccFile.getConstPool();
    AnnotationsAttribute attr = new AnnotationsAttribute(constpool,
            AnnotationsAttribute.visibleTag);

    Annotation annot = new Annotation("sample.PersonneName", constpool);
    annot.addMemberValue("name",
            new StringMemberValue("World!! (dynamic annotation)", ccFile.getConstPool()));
    attr.addAnnotation(annot);

    // add the annotation to the method descriptor
    fieldDescriptor.getFieldInfo().addAttribute(attr);
Run Code Online (Sandbox Code Playgroud)

它的问题是我不知道如何在新创建的类型上应用现有注释......有没有办法做到这一点?

java annotations cglib

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

@CacheEvict 和 @TriggersRemove 注解之间的区别

我想在我的 portlet 应用程序中使用 Ehcache。如果我想从缓存中删除数据,最好使用@CacheEvictor @TriggersRemove?

根据文档,@CacheEvict@TriggersRemove注释看起来非常相似。

caching annotations ehcache spring-annotations spring-cache

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

带有自定义注释的 Spring bean

我已经用自定义注释注释了 Spring bean,但似乎 Spring 在创建 bean 后删除了我的自定义注释。

AnnotatedBean bean = ctx.getBean(AnnotatedBean.class);

Foo.findAndDoStuffWithAnnotatedThings(bean);
Run Code Online (Sandbox Code Playgroud)

第二步不起作用,我的自定义注释丢失了。(可能是由于代理的东西)

我的豆子

@Rule(name = "RoutePickupRule")
@Transactional
@Component
public class AnnotatedBean{


    @Autowired
    private ICarpoolDoa carpoolDAO;

    @Condition
    public boolean condition(CustomLocation customLocation, String userId) {
        //snip
    }

    @Action
    public void action() {
        //snip
    }  
Run Code Online (Sandbox Code Playgroud)

我的自定义注释之一的示例

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Condition {

}
Run Code Online (Sandbox Code Playgroud)

findAndDoStuffWithAnnotatedThings Bean中的错误
被传递到验证我的自定义注释的类,但我的验证程序找不到任何注释。(Util 使用isAnnotationPresent方法)。同样,当我使用“new”自己创建 bean 时,没有问题。

public class RuleAnnotationVerifier {


    public void RuleAnnotationVerifier(final Object rule) {
        checkRuleClass(rule);
        checkConditionMethod(rule);
        checkActionMethod(rule);
    }

    private void checkRuleClass(final Object rule) {
        if …
Run Code Online (Sandbox Code Playgroud)

spring annotations spring-bean

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

向 Google 烛台图表添加注释

我正在使用 Google Charts 烛台图(横向翻转)来模拟范围条形图。关于当前显示在最左侧行中的文本(TU300、TU-01、TU-10 等)——我希望它也(或者相反)立即显示在每个水平条的右侧,以便更有效地标记每个条。

我是新手,希望得到详细的帮助。这是 JSFiddle:

https://jsfiddle.net/jdscomms/xLe83g80/

  google.charts.load('current', {'packages':['corechart']});

  google.charts.setOnLoadCallback(drawChart);

  function drawChart() {

    var data = new google.visualization.arrayToDataTable([
      ['TU300', 438, 438, 447, 447],
      ['TU-01', null, null, null, null],
      ['TU-10', 436, 436, 445, 445],
      ['TU-12EX', 438, 438, 445, 445],
      ['TU-2', 438, 438, 445, 445],
          ['TU-3', 436, 436, 445, 445],
      ['TU-3s', 436, 436, 445, 445],
      [' TU-3w', 436, 436, 445, 445],         
      ['NS Micro', 435, 435, 445, 445],
      ['NS Micro II', 410, 410, 480, 480],
      ['Universal II ', 435, 435, 445, 445], …
Run Code Online (Sandbox Code Playgroud)

javascript annotations candlestick-chart

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

Mapbox 长按手势后在地图上查找注释

我是 iOS 开发和 Mapbox 的新手,如果问题听起来很愚蠢,我很抱歉,但我在任何地方都找不到答案。\n我有一张地图和上面的注释。我想在用户触摸并按住注释(长按手势)时显示一些其他信息。我有长按手势来工作,但找到了找到被触摸的注释的方法,或者至少是它的索引。到目前为止我是这样的:

\n\n
class eventsMapController: UIViewController, MGLMapViewDelegate {\noverride func viewDidLoad() {\n    super.viewDidLoad()\n    mapView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]\n\n    // Set the map\'s bounds to Oslo 59.920269,10.71167\n    //let bounds = MGLCoordinateBounds(sw: CLLocationCoordinate2D(latitude: 59.925861, longitude: 10.712185),\n                                //     ne: CLLocationCoordinate2D(latitude: 59.889798, longitude: 10.794754))        \n    view.addSubview(mapView)\n\n    // Set the map view\xe2\x80\x98s delegate property\n    mapView.delegate = self\n\n    let myGesture = UILongPressGestureRecognizer(target: self, action: #selector(eventsMapController.testLongGesture))\n    myGesture.minimumPressDuration = 0.8\n    mapView.addGestureRecognizer(myGesture) \n}\nfunc testLongGesture(long: UILongPressGestureRecognizer){\n    if long.state == .Began{ \n        print("begin", long)\n    }\n}\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我正在添加这样的注释

\n\n
let pointAnotation …
Run Code Online (Sandbox Code Playgroud)

annotations ios mapbox swift

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

这些函数和参数注释是什么?

busybox syslogd 实现的源代码包含一些我不熟悉的注释。语言是C,而不是C++。

int syslogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int syslogd_main(int argc UNUSED_PARAM, char **argv)
Run Code Online (Sandbox Code Playgroud)

具体来说,MAIN_EXTERNALLY_VISIBLE并且UNUSED_PARAM

  1. 这些注释到底在做什么?我在哪里可以阅读有关它们和其他注释的更多信息?
  2. 这些是 C 标准的一部分,还是编译器扩展?如果它们是编译器扩展,它们的支持范围有多大?
  3. 我认为第一个原因是这个文件没有功能的原因main()。如果这些是编译器扩展而不是标准的一部分,这是否意味着仅遵循 C 标准的编译器无法按原样有意义地编译该文件?
  4. 为什么他们syslogd_main在完整定义之前声明函数的原型?MAIN_EXTERNALLY_VISIBLE注解只能应用于函数原型吗?

c gcc annotations

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

注释另一个模型中字段值出现的计数

使用 Django 1.11。这些是我的模型:

class UserPromoCode(models.Model):
    promo_code = models.ForeignKey(PromoCode, related_name="user_promo_code")

class PromoCode(models.Model):
    code = models.CharField(max_length=20)
Run Code Online (Sandbox Code Playgroud)

我需要一个查询集,在一个或两个数据库请求中执行以下操作:

promocodes = PromoCode.objects.all()
for p in promocodes:
   p.assigned_times = UserPromoCode.objects.filter(promo_code__code=p.code).count()
Run Code Online (Sandbox Code Playgroud)

问题是 PromoCode.code 不是唯一的,所以我不能做PromoCode.objects.annontate(assigned_times=Count('user_promo_code'))以下类似的事情:

promocodes = PromoCode.objects.all()
for p in promocodes:
   p.assigned_times = p.user_promo_code.count()
Run Code Online (Sandbox Code Playgroud)

我想它应该是类似 PromoCode.objects.annontate(assigned_times=Count(???)) 的东西。

python django annotations

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

Kotlin 中的导入注释中应包含的名称

我有一个用 Kotlin 实现的 Spring Boot App (2.1.6)。是一个想要使用 Keycloak 使用 oAuth 2 的 Rest api。我在 Java 中有这段代码可以编译:

package com.talleres.paco.mako.config.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

@Configuration
@EnableWebSecurity
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
@ConditionalOnProperty(prefix = "rest.security", value = "enabled", havingValue = "true")
@Import({SecurityProperties.class})
public class SecurityConfigurer extends ResourceServerConfigurerAdapter {

  @Autowired
  private ResourceServerProperties resourceServerProperties;

  @Autowired
  private …
Run Code Online (Sandbox Code Playgroud)

annotations compiler-errors kotlin spring-boot

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

如何正确使用Mybatis的@Param注解

一开始我没有使用@Param注解,这是我的mapper.java

public void changeUserAuth(Integer userId,int identity);
Run Code Online (Sandbox Code Playgroud)

,这是我的 mapper.xml

<update id="changeUserAuth">
    update user
    <set>
        <if test="identity != 0">identity = #{identity}</if>
    </set>
    <where>
        <if test="userId != 0">userId = #{userId}</if>
    </where>
</update>
Run Code Online (Sandbox Code Playgroud)

那么就正常运行了!我继续这样写,如下:

//this's mapper.java
public void updateUserStatus(Integer userId);

<!--this is mapper.xml>
<update id="changeUserAuth">
    update user
    set deleteFlag= true
    <where>
        <if test="userId != 0">userId = #{userId}</if>
    </where>
</update>
Run Code Online (Sandbox Code Playgroud)

然而,它给出了一个错误,消息是

'class.java.lang.Integer' 中没有名为 'userId' 的属性的 getter

我可以理解mybatis无法解析Integer,但是为什么它不像我第一次使用那样错误,仅仅因为我有一个int类型的参数?在第二种方法中,我必须使用@Param注解

annotations mybatis

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

如何将 JUnit 注释组合成自定义注释?

(使用 OpenJDK-13 和 JUnit5-Jupiter)

问题是我的单元测试每个都使用了一个不小的 JUnit 注释系统,如下所示:

@ParameterizedTest
@MethodSource("myorg.ccrtest.testlogic.DataProviders#standardDataProvider")
@Tags({@Tag("ccr"), @Tag("standard")})
Run Code Online (Sandbox Code Playgroud)

这使得测试编写有点乏味,测试代码有点长,当然,当需要更改时,这是一件苦差事!

想知道我是否可以创建自己的 JUnit annotation: @CcrStandardTest,这意味着上面的所有注释?

我还尝试将类定义中的注释向上移动(希望它们随后适用于类的所有方法),但编译器说不:“@ParameterizedTest 不适用于类型”

java reflection annotations junit5 junit-jupiter

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