How to use @Autowired in manually new instance in Spring Boot

NingLee

As we know, @Autowired can be used only in instances managed by spring container, If you new an instance, and @Autowired member in it will not effect.

But I think In some situation, new an instance can't be avoid.

Such as a RunnableTask. which contains the DAOService, which managed by spring. Because the task is manually new. So I can't use the DAOService in the ThreadTask.

So I want to know how to get the ApplicationContext in Spring Boot, and so I can get the bean by context.getBean().

I knew in main() I can Autowired the ApplicationContext. But I can't pass the context as a parameter everywhere!

I want to get the ApplicationContext anywhere.

Any help would be greatly appreciated.

Dave Syer

I want to get the ApplicationContext anywhere.

That's an anti-pattern. Try to avoid it.

Why can't you inject your DAOService into the thing that creates the RunnableTask?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to use spring annotations like @Autowired in kotlin?

From Dev

Spring Boot Autowired null

From Dev

Spring @Autowired - Instantiate new bean

From Dev

spring-boot properties not @Autowired

From Dev

How to use Dozer with Spring Boot?

From Dev

How Spring @Autowired binds the SessionFactory object even if there is no SessionFactory instance available

From Dev

How to use the new Vaadin Spring Boot addon together with Vaadin4Spring EventBus

From Dev

How to use CommonsMultipartResolver in Spring Boot

From Dev

How is @Autowired implemented in Spring

From Dev

How to listen to multiple queues with autowired Spring Boot?

From Dev

Spring Boot Unit Test Autowired

From Dev

Spring Autowired in new java object

From Dev

Spring boot Constructor Autowired Exception

From Dev

How to mock autowired dependencies in Spring Boot MockMvc Unit tests?

From Dev

Java How use Spring Autowired in SystemInitializer class

From Dev

Understanding Spring Boot @Autowired

From Dev

Spring-boot how to use autowired class properties in called method

From Dev

Spring @Autowired on a class new instance

From Dev

Java Spring-boot - How to use @Autowired with @ServerEndpoint?

From Dev

Spring Boot Autowired Repository null

From Dev

Is there way to reload autowired instance or replace autowired behavior in spring

From Dev

Understanding Spring Boot @Autowired

From Dev

Spring @Autowired not working on new thread

From Dev

AutoWired Setters rather than AutoWired instance variables in Spring

From Dev

Spring boot Constructor Autowired Exception

From Dev

Spring boot vaadin project @Autowired

From Dev

Spring boot @Autowired repository instance null when using spring security

From Dev

How @Repository and @Autowired work in spring boot Dao and Service Layer?

From Dev

how to use instence variable in static area if class is autowired in spring boot

Related Related

  1. 1

    How to use spring annotations like @Autowired in kotlin?

  2. 2

    Spring Boot Autowired null

  3. 3

    Spring @Autowired - Instantiate new bean

  4. 4

    spring-boot properties not @Autowired

  5. 5

    How to use Dozer with Spring Boot?

  6. 6

    How Spring @Autowired binds the SessionFactory object even if there is no SessionFactory instance available

  7. 7

    How to use the new Vaadin Spring Boot addon together with Vaadin4Spring EventBus

  8. 8

    How to use CommonsMultipartResolver in Spring Boot

  9. 9

    How is @Autowired implemented in Spring

  10. 10

    How to listen to multiple queues with autowired Spring Boot?

  11. 11

    Spring Boot Unit Test Autowired

  12. 12

    Spring Autowired in new java object

  13. 13

    Spring boot Constructor Autowired Exception

  14. 14

    How to mock autowired dependencies in Spring Boot MockMvc Unit tests?

  15. 15

    Java How use Spring Autowired in SystemInitializer class

  16. 16

    Understanding Spring Boot @Autowired

  17. 17

    Spring-boot how to use autowired class properties in called method

  18. 18

    Spring @Autowired on a class new instance

  19. 19

    Java Spring-boot - How to use @Autowired with @ServerEndpoint?

  20. 20

    Spring Boot Autowired Repository null

  21. 21

    Is there way to reload autowired instance or replace autowired behavior in spring

  22. 22

    Understanding Spring Boot @Autowired

  23. 23

    Spring @Autowired not working on new thread

  24. 24

    AutoWired Setters rather than AutoWired instance variables in Spring

  25. 25

    Spring boot Constructor Autowired Exception

  26. 26

    Spring boot vaadin project @Autowired

  27. 27

    Spring boot @Autowired repository instance null when using spring security

  28. 28

    How @Repository and @Autowired work in spring boot Dao and Service Layer?

  29. 29

    how to use instence variable in static area if class is autowired in spring boot

HotTag

Archive