Questions tagged [autowired]

Autowiring is a DI container feature where the dependencies are automatically looked for according to some criteria.

In a like , the beans to be injected can be explicitely configured or they can be searched for automatically in what is called "autowiring".

Although the concept is not exclusive to Spring, the bulk of questions in Stack Overflow and Google seem to be directed towards this framework.

Spring

In the particular case of Spring, when "autowiring" is enabled, the container will look in the current ApplicationContext for a bean that matches the type/name of the corresponding setter method.

Autowiring can be configured globally or on each bean to be disabled; or enabled by type, by name, by constructor type. It can also be configured using the Autowired annotation.

2781 questions
8
votes
3 answers

Using Proguard with a library has a @Service bean which should autowire

I have a library Common.License which I am obfuscating with Proguard: com.pyx4me proguard-maven-plugin package
Grant Cermak
  • 1,808
  • 1
  • 19
  • 23
8
votes
2 answers

Autowire without annotation @Autowired

I am looking at some old example I have in the workspace. I can't see how is the autowiring done as there is no @Autowired. Spring boot + facebook default configurations. @Controller @RequestMapping("/") public class HelloController { private…
strash
  • 1,291
  • 2
  • 15
  • 29
8
votes
4 answers

What is the official spring boot way to start a simple non web based java application?

I am in the proces of converting a simple java project into a spring boot variant. The Spring Boot Reference Guide http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/ has been very helpful in general, but most examples of setting up…
Sander_M
  • 1,109
  • 2
  • 18
  • 36
8
votes
2 answers

Spring Autowiring and thread-safety

I am new to Spring and recently created a test RESTful web service application. I am following the Spring @Autowiring way of injecting the beans. Below is my code and a question: @Service public class HelloWorld { @Autowired private…
Nick M
  • 91
  • 1
  • 3
8
votes
2 answers

How does Spring framework autowire a collection

I've never seen an autowired collection: @Service public class SomeFactory { @Autowired private List foos; @PostConstruct public void init() { for(Foo foo: foos) { //do something } } } In…
choiapril
  • 183
  • 1
  • 4
  • 14
8
votes
2 answers

How to autowire this TokenStore

How do I trigger auto-logout of this sample Spring Boot OAuth2 app? I tried adding the following code from an answer to this other posting into a new controller class in the demo package of the authserver app: package demo; import…
CodeMed
  • 9,527
  • 70
  • 212
  • 364
8
votes
2 answers

What is a right way to initialize fields in Spring Beans?

I'm wondering how should I initialize fields in Spring Beans? Here is several possible solutions: 1. Initialize fields directly on declaration import org.springframework.stereotype.Component; @Component public class DeclarationInit { private…
8
votes
1 answer

if there are more than one classes implementing one interface ,then how does @autowired work?

(spring mvc)first i do not know whether the writing below are right or not.if it is right ,then i don't understand how the @autowired works here.if it is wrong , then how i should do when i have more than one classes to implement one…
lhao0301
  • 1,941
  • 4
  • 20
  • 26
8
votes
4 answers

Why @Autowired(required=false) do not work on @Configuration beans?

Let explain with an example: Having this bean: public class Foo { private String name; Foo(String name) { this.name = name; } public String getName() { return this.name; } } And this service: public class…
Christian Sisti
  • 199
  • 1
  • 3
  • 9
8
votes
2 answers

Again... method security with spring boot/security: Error creating bean with name 'methodSecurityInterceptor' "This object has already been built"

I want to implement method security. I'm facing a problem with @Secured and @PreAuth annotations. Whenever I add any of those to my service interface, I receive an exception like the following. Without them, my app runs just fine. Caused by:…
elysch
  • 1,846
  • 4
  • 25
  • 43
8
votes
4 answers

Java Config @Bean not autowired in other @Configuration class

Trying to setup a Spring 4 web application with Java Config, I encounter a problem with autowiring a bean created in a configuration class into another configuration class. The 'dataSource' bean has a null value in the MyBatisConfig class. This…
ProGig
  • 231
  • 1
  • 2
  • 8
8
votes
5 answers

What is the cleanest way to autowire Spring Beans in a JSP?

We're currently adding some new features to an old webapp which was using only JSP without any framework for the front. We have added Spring recently, and we would like to autowire our beans in our modified JSP, while not rewriting everything to use…
temsa
  • 305
  • 2
  • 3
  • 8
8
votes
3 answers

Spring Autowired exception (Could not autowire field..)

In RegistrationService, @Autowired registrationDao and registerCustomer work ok. But it cannot auto-wire ActivationMailService. I didn't find the problem. It works when I remove ActivationMailService in RegistrationService. Could anybody tell me…
node
  • 121
  • 1
  • 1
  • 8
8
votes
2 answers

Can't I use annotation to indicate a bean is a primary bean

We know in Spring, has an attribute "primary" to indicate a bean is the first candidate if there are multiple beans are available to be autowired to a property. But now all my bean definition are declared using @Component/@Service, etc, I…
Matt
  • 1,671
  • 5
  • 23
  • 34
8
votes
2 answers

How to add inner classes to Spring application context for Unit Testing?

I have a bean whose business logic loads beans of a certain type from the ApplicationContext in order to process them. For my jUnit tests, I would like to create some dummy beans within my unit test class and see if my bean under test properly…
Eric B.
  • 23,425
  • 50
  • 169
  • 316