Skip to content

A repo to understand how the dependency injection and autowiring works using java-based and xml-based configuration.

Notifications You must be signed in to change notification settings

the-pratik/spring-framework

Repository files navigation

Spring Basic Setup & Dependency Injection Guide

1. Project / Folder Structure Setup

Here’s a recommended simple layout (non-Maven, manual JAR setup) for a Spring core project:

MySpringProject/
│
├── lib/ ← folder to hold Spring and other JARs
│ ├── spring-core.jar
│ ├── spring-beans.jar
│ ├── spring-context.jar
│ └── … (other dependencies)
│
├── src/ ← your Java source code
│ ├── com/
│ │ └── example/
│ │ ├── AppMain.java
│ │ ├── service/
│ │ │ ├── MyService.java
│ │ │ └── impl/ …
│ │ └── config/
│ │ └── AppConfig.java
│ └── …
│
├── resources/ ← (optional) for config XML, property files
│ └── applicationContext.xml
│
└── README.md

Steps in IntelliJ IDEA

  1. Create the folders

    • In your file system (or via IntelliJ), create MySpringProject, then lib, src, resources, etc.
    • Inside src, create your package hierarchy (e.g. com.example.service, com.example.config, etc.)
  2. Open the project in IntelliJ

    • File → Open… → select the MySpringProject folder.
  3. Add the JAR dependencies

    • Right-click on the project → Open Module Settings (or press F4).
    • Under “Modules” → your module → tab Dependencies.
    • Click the + icon → JARs or directories → navigate to and select all JARs in lib/.
    • Choose whether to add them as Compile scope (usually yes).
    • IntelliJ will include those JARs on the module classpath.
  4. Mark source / resource roots

    • In the Project view, right-click the src folder → Mark Directory as → Sources Root (turns it blue).
    • Right-click resourcesMark Directory as → Resources Root (turns it a different color, e.g. light green).
    • This tells IntelliJ where to look for Java classes and non-Java resources.
  5. Verify classpath and build

    • Try creating a simple AppMain.java with public static void main(...) and compile/run.
    • The JARs in lib/ should be recognized (e.g., import org.springframework.context.ApplicationContext should resolve).

That’s the basic manual setup. (In real projects, using Maven or Gradle makes dependency management easier, but this is good for a learning setup.)


2. Dependency Injection (DI) & Autowiring in Spring

Below is a conceptual summary + code examples to illustrate how DI and Autowiring work, following the tutorial you referenced (which covers Spring IoC, DI, and autowiring). :contentReference[oaicite:0]{index=0}

2.1 What is Dependency Injection (DI) / Inversion of Control (IoC)

  • Inversion of Control (IoC) means that the control of creating and managing dependencies is inverted: instead of your code instantiating dependencies, you delegate that to a container (Spring).
  • Dependency Injection (DI) is a pattern that implements IoC: components declare their dependencies (via constructor, setter, or fields), and the container injects those dependencies at runtime.

Spring supports multiple styles:

  • Constructor-based DI
  • Setter-based DI
  • Field injection (via @Autowired)
  • Autowiring by name / by type / by constructor (automatically resolving dependencies)

2.2 Example: XML-based DI (legacy style)

Suppose you have:

<!-- resources/applicationContext.xml -->
<beans xmlns="http://www.springframework.org/schema/beans"
       … other namespaces …>
  
  <bean id="myService" class="com.example.service.impl.MyServiceImpl">
    <property name="myDao" ref="myDao" />
  </bean>

  <bean id="myDao" class="com.example.dao.impl.MyDaoImpl"/>
  
</beans>

And Java:

package com.example.service;

public interface MyService {
    void perform();
}

package com.example.service.impl;

import com.example.dao.MyDao;

public class MyServiceImpl implements MyService {
    private MyDao myDao;

    public void setMyDao(MyDao myDao) {
        this.myDao = myDao;
    }
  
    @Override
    public void perform() {
        myDao.save();
    }
}

Here, Spring container reads the XML, sees that myService bean has a property named myDao, looks for bean myDao, and invokes setMyDao(...) on the MyServiceImpl instance to inject dependency.

2.3 Annotation-based / Java Config + Autowiring

Modern approach uses @Configuration, @Bean, @Component, @Autowired, etc.

package com.example.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
    // You can also define explicit beans here if needed.
}

package com.example.service;

public interface MyService {
    void perform();
}

package com.example.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.dao.MyDao;

@Service
public class MyServiceImpl implements MyService {

    // Option A: field injection (not always recommended)
    @Autowired
    private MyDao myDao;

    @Override
    public void perform() {
        myDao.save();
    }
}

package com.example.dao.impl;

import org.springframework.stereotype.Repository;
import com.example.dao.MyDao;

@Repository
public class MyDaoImpl implements MyDao {
    @Override
    public void save() {
        System.out.println("Data saved...");
    }
}

And the main entry:

package com.example;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.example.config.AppConfig;
import com.example.service.MyService;

public class AppMain {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx =
                new AnnotationConfigApplicationContext(AppConfig.class);
        
        MyService svc = ctx.getBean(MyService.class);
        svc.perform();

        ctx.close();
    }
}

Here’s how it works, step by step:

  1. You bootstrap Spring with AnnotationConfigApplicationContext(AppConfig.class).

  2. @ComponentScan tells Spring to scan com.example package for components annotated with @Component, @Service, @Repository, @Controller, etc.

  3. Spring finds MyServiceImpl and MyDaoImpl and instantiates beans for them.

  4. It sees @Autowired on myDao field in MyServiceImpl → it finds a matching bean of type MyDao (MyDaoImpl) and injects it.

  5. When you call ctx.getBean(MyService.class), you get the bean instance with its dependency already injected.

2.4 Autowiring Modes & Behavior (from tutorial reference)

The tutorial mentions that Spring supports different autowiring strategies: by name, by type, constructor, etc. smartprogramming.in

  • byName: Spring looks for a bean whose name matches the property name.

  • byType: Spring looks for a bean whose type matches the property type.

  • constructor: Spring uses the constructor that best matches the bean definitions.

  • autodetect: Spring first tries constructor, then setter.

With annotations, default is by type (i.e. @Autowired injects by type). If multiple beans of same type exist, you can qualify with @Qualifier:

@Autowired @Qualifier("myDaoImpl")
private MyDao myDao;

You can also use @Primary on one bean to mark it as default when multiple candidates exist.

2.5 Lifecycle & Scope (brief mention)

  • Bean scope by default is singleton (one instance per container)

  • Other scopes: prototype, request, session, etc.

  • Spring also allows lifecycle callbacks (@PostConstruct, @PreDestroy) etc.

About

A repo to understand how the dependency injection and autowiring works using java-based and xml-based configuration.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages