Questions tagged [spring-ioc]

The Spring IoC is a core component of the Spring Framework. It enforces the dependency injection pattern for beans, making them loosely coupled and allowing application developers to code with abstractions.

The Spring IoC (Inversion of Control) is a core component of the Spring Framework. It enforces the dependency injection pattern for beans, making them loosely coupled and allowing application developers to code with abstractions.

It is a process whereby objects define their dependencies only through constructor arguments, factory method arguments, and properties to be set on the object after it is constructed. The container then injects those dependencies when it creates the bean.

192 questions
117
votes
8 answers

exclude @Component from @ComponentScan

I have a component that I want to exclude from a @ComponentScan in a particular @Configuration: @Component("foo") class Foo { ... } Otherwise, it seems to clash with some other class in my project. I don't fully understand the collision, but if I…
ykaganovich
  • 14,736
  • 8
  • 59
  • 96
60
votes
3 answers

Spring bean scopes: session and globalSession

What is the difference between session and globalSession in Spring framework? As per my…
Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82
59
votes
5 answers

Spring default behavior for lazy-init

I am beginner to spring, ESP Inversion of control. I was puzzled understanding the difference between the following
srk
  • 4,857
  • 12
  • 65
  • 109
58
votes
9 answers

Is spring default scope singleton or not?

Could you please explain why Spring is creating two objects for the configuration of beans shown below, since by default spring default scope is singleton? The Spring configuration is here:
Raj
  • 677
  • 3
  • 8
  • 16
53
votes
4 answers

Spring Boot: autowire beans from library project

I'm struggling to autowire beans from my custom library, imported with gradle. after reading couple of similar topics I am still unable to find solution. I have a Spring Boot project that depends on another project (my custom library with…
maret
  • 1,826
  • 1
  • 13
  • 18
21
votes
2 answers

How to check for Request Scope availability in Spring?

I'm trying to setup some code that will behave one way if spring's request scope is available, and another way if said scope is not available. The application in question is a web app, but there are some JMX triggers and scheduled tasks (i.e.…
Taylor
  • 3,942
  • 2
  • 20
  • 33
21
votes
2 answers

Difference between @InjectMocks and @Autowired usage in mockito?

When I was writing test case using the Mockito and Junit, I was using the @InjectMocks for the class to be tested. In other parts of project, I also see @Autowired is being used for the class to be tested. When can I use @InjectMocks and @Autowired?…
21
votes
2 answers

How do Spring annotations work?

Motivation As a follow-up to my previous questions on classloading How is the Classloader for a class chosen? How Classloader determines which classes it can load? Where does bytecode injection happen? I'm curious about how do annotations work in…
ipavlic
  • 4,906
  • 10
  • 40
  • 77
15
votes
1 answer

spring boot @Autowired a bean from another module

My question is how can i add a package to my list of component to scan @ComponentScan(basePackages = {"io.swagger", "com.company.project", like add it here }), but this package is in another module in my project, here is the structure of my…
hamou amroun
  • 151
  • 1
  • 1
  • 5
12
votes
2 answers

How to avoid using ApplicationContext.getBean() when implementing Spring IOC

I'm just getting started with Spring IOC concept. I often see most of the examples found in the internet use the code to get the object. ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Hello hello =…
srk
  • 4,857
  • 12
  • 65
  • 109
9
votes
4 answers

Why Spring framework does not allow autowire primitive types?

As per spring documentation it has been mentioned it is by design I want to understand the possible thinking behind this design.
8
votes
2 answers

Interfaces are annotated with @Component annotation in spring IoC/DI. What could be the reason?

Some times interfaces are annotated with @Component annotation. Then my obvious reasoning was that classes that implement such interface will be treated as components as well. But if I am right that is not the case. So what is the purpose of…
8
votes
1 answer

Spring dependency issue - no matching editors or conversion strategy found

I have a web application which fails while deployment. I get following error: Aug 8, 2014 7:00:21 PM org.springframework.web.context.ContextLoader initWebApplicationContext SEVERE: Context initialization…
Ankit
  • 3,083
  • 7
  • 35
  • 59
6
votes
4 answers

Spring Boot test doesn't find bean from sibling package

I have a @SpringBootTest annotated test class which wants to make use of a test utility: package org.myproject.server; // ... @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public class ServerITest { …
oberlies
  • 11,503
  • 4
  • 63
  • 110
6
votes
1 answer

Spring @Autowired behavior different in tests than components

Are the rules/behaviors around @Autowired different when writing tests? It seems that with a test, you can autowire to a concrete type, but if you try the same thing inside a @Component it will fail. This is a contrived example, but it's something I…
chinabuffet
  • 5,278
  • 9
  • 40
  • 64
1
2 3
12 13