autowired

kafkaendpointlistenerregistry.start() throws null pointer exception

怎甘沉沦 提交于 2020-06-17 09:41:06
问题 I have a requirement where I want to start Kakfa consumer manually. Code : class Dummy implements ConsumerSeekAware { @Autowired KafkaListenerEndpointRegistry registry; CountDownLatch latch; @Autowired ConcurrentKafkaListenerContainerFactory factory; onIdleEvent(){ latch.countdown() } @KafkaListener(id="myContainer", topics="mytopic", autoStartup="false") public void listen() {} @Scheduled(cron=" some time ") void do_some_consumption(){ latch = new CountDownLatch(1); this.registry

kafkaendpointlistenerregistry.start() throws null pointer exception

我的梦境 提交于 2020-06-17 09:41:04
问题 I have a requirement where I want to start Kakfa consumer manually. Code : class Dummy implements ConsumerSeekAware { @Autowired KafkaListenerEndpointRegistry registry; CountDownLatch latch; @Autowired ConcurrentKafkaListenerContainerFactory factory; onIdleEvent(){ latch.countdown() } @KafkaListener(id="myContainer", topics="mytopic", autoStartup="false") public void listen() {} @Scheduled(cron=" some time ") void do_some_consumption(){ latch = new CountDownLatch(1); this.registry

How to Autowire conditionally in spring boot?

可紊 提交于 2020-06-16 03:44:19
问题 I have created one scheduler class public class TestSchedulderNew { @Scheduled(fixedDelay = 3000) public void fixedRateJob1() { System.out.println("Job 1 running"); } @Scheduled(fixedDelay = 3000) public void fixedRateJob2() { System.out.println("Job 2 running"); } } In configuration i have put @ConditionalOnProperty annotation to enable this on conditional purpose. @Bean @ConditionalOnProperty(value = "jobs.enabled") public TestSchedulderNew testSchedulderNew() { return new TestSchedulderNew

NullPointerException @Autowired Mongodb collection

孤街醉人 提交于 2020-05-17 06:14:06
问题 I have a configuration class for MongoDB collections as example: @Configuration public class MongoDBConfiguration{ @Bean public MongoCollection<Document> collection() { MongoClient mongoClient = MongoClients.create(mongoURI); MongoDatabase database = mongoClient.getDatabase(mongoDatabase); return database.getCollection(mongoCollection); } } Then I have another class that where I want to use that collection. In this class I get NullPointerException. @Component public class ExampleClass {

Spring fallback bean implementation

∥☆過路亽.° 提交于 2020-05-11 05:48:23
问题 I'm currently trying to configure Spring Boot (using Java Annotations and ComponentScan ) for the following scenario: Scenario There's an interface MyService . I want to provide a default implementation for MyService , let's call it MyDefaultService . If the component scan detects no other implementation for MyService , Spring should instantiate MyDefaultService as a "fallback". If there is a different implementation of MyService present, let's say MyCustomService , then that bean should

Hibernate @Entity conflict with Spring @Autowired for non-column object

混江龙づ霸主 提交于 2020-04-16 02:33:07
问题 I have a table which contains item descriptions. Items have a price history which can be very extensive. It's that last bit that leads me to avoid using a normal one-to-many Hibernate mapping with lazy loading. Think a price history like ticks on a stock exchange, lots of history. So I have a cache which works well, it's all wired with Spring, the DAO is injected, the cache manages what needs to be queried vs what it already knows. So, the "natural" thing is to be able to ask an item about it

How to use Symfony autowiring with multiple entity managers

狂风中的少年 提交于 2020-03-22 09:13:09
问题 I would like to use the autowiring in a service that use 2 different entity manager. How to achieve something like that ? use Doctrine\ORM\EntityManager; class TestService { public function __construct(EntityManager $emA, EntityManager $emB) { } } My service.yml file use to be configured like that : app.testservice: class: App\Services\TestService arguments: - "@doctrine.orm.default_entity_manager" - "@doctrine.orm.secondary_entity_manager" 回答1: Dylan's answer violates the Demeter's Law

How to Autowire a Component which is having constructor with arguments in SpringBoot Application

邮差的信 提交于 2020-03-20 14:01:25
问题 I have a class having Autowired Constructor. now when i am autowiring this class object in my class. how do i pass arguments for constructor?? example code: Class having Autowired Constructor: @Component public class Transformer { private String dataSource; @Autowired public Transformer(String dataSource) { this.dataSource = dataSource; } } Class using autowire for component having constructor with arguments: @Component public class TransformerUser { private String dataSource; @Autowired

Spring inject without autowire annotation

烈酒焚心 提交于 2020-03-17 10:44:27
问题 I find some answer: https://stackoverflow.com/a/21218921/2754014 about Dependency Injection. There isn't any annotation like @Autowired , @Inject or @Resource . let's assume that there isn't any XML configuration for this example TwoInjectionStyles bean (except simple <context:component-scan base-package="com.example" /> . Is it correct to inject without specify annotation? 回答1: From Spring 4.3 annotations are not required for constructor injection. public class MovieRecommender { private

Spring @Autowired not working on new thread

吃可爱长大的小学妹 提交于 2020-03-17 05:35:47
问题 When I run TaskJob I am getting null pointer exception because Spring doesn't autowiring serviceJob service. Is new thread causing this problem because Spring autowired mysqlService without any problem? public class TaskJob implements Runnable { @Autowired private ServiceJob serviceJob; String name; String source; public TaskJob(String name, String source) { this.name = name; this.source = source; } public void run() { serviceJob.run(); } } @Service public class ServiceJob extends BaseJob{