标签: spring-annotations

spring ampq注解驱动一个队列两个监听器区分路由key

事实上我不明白这个运行。也许我误解了一些东西,无论如何这是不可能的。我正在尝试在同一个队列、相同的交换器上配置 2 个侦听器,但只有路由键应该不同。我的问题是事情不知何故变得混乱。结果是侦听器 A 收到了侦听器 B 的消息。但只是有时,有时一切正常。有什么建议么?

我的配置

@Bean
public ConnectionFactory connectionFactory() {
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory(getHostname());
    connectionFactory.setUsername(getUsername());
    connectionFactory.setPassword(getPassword());
    return connectionFactory;
}

@Bean
public RabbitAdmin rabbitAdmin() {
    return new RabbitAdmin(connectionFactory());
}

@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    factory.setMessageConverter(new CustomMessageConverter());
    factory.setConnectionFactory(connectionFactory());
    factory.setAcknowledgeMode(AcknowledgeMode.AUTO);
    factory.setConcurrentConsumers(10);
    factory.setMaxConcurrentConsumers(10);
    return factory;
}

@Override
public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
    registrar.setMessageHandlerMethodFactory(myHandlerMethodFactory());
}

@Bean
public DefaultMessageHandlerMethodFactory myHandlerMethodFactory() {
    DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory();
    factory.setMessageConverter(new MappingJackson2MessageConverter());
    return factory;
}
Run Code Online (Sandbox Code Playgroud)

我的听众A

@RabbitListener(bindings = @QueueBinding(value = @Queue(value …
Run Code Online (Sandbox Code Playgroud)

java rabbitmq spring-annotations spring-amqp

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

是否可以在自定义 PreAuthorize 方法中获取 RequestMethod 动词?

我正在使用带有@PreAuthorize 的自定义访问检查器:

@RestController
@RequestMapping("/users")
public class Users {

    @PreAuthorize("@customAccessChecker.hasAccessToMethod('USERS', 'GET')")
    @RequestMapping(method = RequestMethod.GET)
    User getUsers() {
        ...
    }

    @PreAuthorize("@customAccessChecker.hasAccessToMethod('USERS', 'POST')")
    @RequestMapping(method = RequestMethod.POST)
    User addUser() {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

我想去掉@PreAuthorize 注释中的字符串“GET”和“POST”。是否可以将@RequestMapping 中使用的 RequestMethod 以某种方式作为 hasAccessToMethod 的变量输入?

java spring spring-annotations spring-restcontroller request-mapping

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

何时使用 Spring 的 @Repository 注解?

在阅读有关在 Spring 中创建自定义查询的信息时,我注意到没有任何类 ( UserRepositoryCustom, UserRepositoryCustomImpl, UserRepository) 使用该@Repository注释。

我对此感到惊讶,因为通常所有 Spring 应用程序类都用一些东西进行注释,以便检测它们并为它们生成代理。这就是面向方面的框架在 Java 中的工作方式。

然后我检查了我的旧工作代码,发现我的存储库界面也没有使用注释。然而,它是@Autowired对使用它的控制器和其他类的依赖。例子:

// AddressRepository.java
public interface AddressRepository extends CrudRepository<Address, String> {
}

// Repositories.java
@Getter // lombok
@Component
public class Repositories { // autowire this to have access to all repo's
    private final AddressRepository addressRepository;

    @Autowired
    public Repositories(AddressRepository addressRepository) {
        this.addressRepository = addressRepository;
    }
}
Run Code Online (Sandbox Code Playgroud)

这就引出了一个问题:什么时候应该使用注释?以及Spring 的组件扫描如何自动检测到未注释的类?

autowired spring-annotations spring-repositories

3
推荐指数
2
解决办法
3999
查看次数

自动装配和在 Spring Boot 的自动装配类上使用 new 关键字的区别?

@Autowired
private EmployeeService employeeService;
Run Code Online (Sandbox Code Playgroud)

对比

@Autowired
private EmployeeService employeeService = new EmployeeService();
Run Code Online (Sandbox Code Playgroud)

使用new EmployeeService()上比只使用自动装配类不同new EmployeeService()

java spring autowired spring-annotations spring-boot

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

如何在 Kotlin 中表达数组注释参数?

当注释具有基本类型(如 String 或 Int)的数组参数时,如何使用它很简单:

public @interface MyAnnotation{
  String[] props();
}

@MyAnnotation(props = ["A", "B", "C"])
class Foo {}
Run Code Online (Sandbox Code Playgroud)

不幸的是,这不适用于注释本身的值。

一个例子是org.springframework.context.annotation.PropertySources

public @interface PropertySources {
  PropertySource[] value();
}

public @interface PropertySource { 
  String[] value();
}
Run Code Online (Sandbox Code Playgroud)

在Java中语法用法是

@PropertySources({
    @PropertySource({"A", "B", "C"}),
    @PropertySource({"D", "E", "F"}),
})
class Foo{}
Run Code Online (Sandbox Code Playgroud)

但在 Kotlin 中,具有类似方法的代码无法编译

@PropertySources([
    @PropertySource(["A", "B", "C"]),
    @PropertySource(["D", "E", "F"]),
])
class Foo{}
Run Code Online (Sandbox Code Playgroud)

如何在 Kotlin 中表达这个注解数组嵌套构造?

annotations spring-annotations kotlin

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

无法解析org.springframework.transaction.annotation.Transactional的依赖项

我是基于注释的编程的新手,不知道要添加到org.springframework.transaction.annotation.Transactional的 pom.xml中的maven工件,我用google搜索(如搜索结果的前5页)但无法找到什么是我在寻找.我正在使用弹簧3.此外,如果有任何在线教程,以了解更多关于春天的注释.谢谢!!

java hibernate spring-mvc spring-annotations

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

@Scheduled注释Spring

我正在使用Spring 3注释@Scheduled在服务器上创建预定作业.但我对@Scheduled注释的参数(cron,fixedDelay,fixedRate)感到困惑.请解释这些参数与我可以使用这些参数的情况之间的区别.

java spring scheduled-tasks spring-annotations

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

为什么@Qualifier不能与@Autowired一起使用?

我有一个类:

@Service
@Qualifier("VeoExecutionService")
public class VeoExecutionService implements ExecutionService {

}
Run Code Online (Sandbox Code Playgroud)

我在测试中使用它:

@Autowired
@Qualifier("VeoExecutionService")
private VeoExecutionService veoService;
Run Code Online (Sandbox Code Playgroud)

但是当我跑步时我得到:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.trizic.service.veo.VeoExecutionService com.trizic.service.veo.VeoServiceImportAccountsTest.veoService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.trizic.service.veo.VeoExecutionService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=VeoExecutionService)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
... 29 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.trizic.service.veo.VeoExecutionService] found for dependency: expected at …
Run Code Online (Sandbox Code Playgroud)

spring spring-annotations

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

是否可以将控制器/控制器作为内部类嵌套在Spring 4 MVC中?

我希望有一个映射到/site/两个不同控制器内部的控制器,看起来像:

@Controller
@RequestMapping(value="/api")
public class ApiController {
    @Controller
    @RequestMapping(value="/foo")
    public class FooController {
        //Some /foo/* methods here
    }

    @Controller
    @RequestMapping(value="/bar")
    public class BarController {
       //Some /bar/* methods here
    }

    //Other methods that don't match /foo or /bar
}
Run Code Online (Sandbox Code Playgroud)

这是可以的,或者将它分成两个独立的控制器/site/foo/site/bar映射是更好的做法吗?

java spring spring-mvc spring-annotations

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

配置中的Spring注释值

在我的Spring Boot应用程序中,我配置了以下JMS侦听器:

@Component
public class Consumer {

    @JmsListener(destination = "image.index.queue")
    public void receiveQueue(IndexRequest indexRequest) {
        ...
    }

}
Run Code Online (Sandbox Code Playgroud)

如何从配置(application.properties)中提供目标名称“ image.index.queue”,而不是硬编码值?

spring jms spring-annotations spring-boot

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