• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Spring dependency injection with Java configuration

June 19, 2017 By j2eereference Leave a Comment

Here we are going to see how to configure dependency injection in spring with java . In the below example we have two classes MyClass and Myclass2 .There is a dependency of MyClass2 in Myclass . So lets understand how to inject the depended class into another using Spring.

Java
1
2
3
4
5
6
7
public class MyClass {
MyClass2 myClass2;
MyClass(MyClass2 myClass2){
this.myClass2=myClass2;
}
}

Java
1
2
3
4
5
6
public class MyClass2 {
MyClass2(){
System.out.println("Initializing MyClass2");
}
}

Below is the configuration for injecting MyClass2 into MyClass .

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class MySpringConf {
 
    @Bean
    public MyClass myClass() {
        return new MyClass(getClass2());
    }
    
    @Bean
    public MyClass2 getClass2() {
        return new MyClass2();
    }
 
}

Java
1
2
3
4
5
6
7
8
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
public class SpringConfDemo {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MySpringConf.class);
MyClass myClass = ctx.getBean(MyClass.class);
}
}

Output :

Jun 13, 2017 5:26:43 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2c634b: startup date [Tue Jun 13 17:26:43 IST 2017]; root of context hierarchy
Initializing MyClass2

Here we can notice that MyClass2 is getting injected in MyClass .

 

 

Related Posts

  • What is Spring Boot?
  • @import annotation in Spring
  • Configuring Spring Lifecycle Callbacks with java
  • Configuring Spring Bean Scope with java
  • Spring configuration using java
  • @Required annotation in spring
  • Event handling in Spring
  • Using MessageSource in Spring
  • Component annotation in spring
  • Spring JSR-250 Annotations

Filed Under: Spring

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.