Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.baeldung.dependencyinjections;

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

@Configuration
public class AppConfig {

@Bean
public WebsiteControllerConstructorInjection websiteControllerConstructorInjection() {
return new WebsiteControllerConstructorInjection(homepageService());
}

@Bean
public HomepageService homepageService() {
return new HomepageService();
}

@Bean
public WebsiteControllerSetterInjection websiteControllerSetterInjection() {
return new WebsiteControllerSetterInjection();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.baeldung.dependencyinjections;

import org.springframework.stereotype.Service;

@Service
public class HomepageService {

public String getContents() {
return "Home page contents";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.baeldung.dependencyinjections;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Website {

@SuppressWarnings("resource")
public static void main(String[] args) {

ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

WebsiteControllerConstructorInjection controller = context.getBean(WebsiteControllerConstructorInjection.class);
System.out.println("The contents of the Website includes : " + controller.getContentsViaConstructorInjection());

WebsiteControllerSetterInjection controllerwithSetterInjection = (WebsiteControllerSetterInjection) context.getBean("websiteControllerSetterInjection");
System.out.println("The contents of the Website includes : " + controllerwithSetterInjection.getContentsViaSetterInjection());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.baeldung.dependencyinjections;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class WebsiteControllerConstructorInjection {

private HomepageService homeService;

@Autowired
public WebsiteControllerConstructorInjection(HomepageService homeService) {
this.homeService = homeService;
}

public String getContentsViaConstructorInjection() {
return homeService.getContents();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.baeldung.dependencyinjections;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class WebsiteControllerSetterInjection {

private HomepageService homeService;

@Autowired
public void setHomepageService(HomepageService homeService) {
this.homeService = homeService;
}

public String getContentsViaSetterInjection() {
return homeService.getContents();
}
}
21 changes: 21 additions & 0 deletions spring-core/src/main/resources/dependencyinjections-context.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">

<context:annotation-config/>

<bean id="homeService" class="com.baeldung.dependencyinjections.HomepageService">
</bean>

<bean id="websiteControllerConstructorInjection"
class="com.baeldung.dependencyinjections.WebsiteControllerConstructorInjection">
<constructor-arg ref="homeService" />
</bean>

<bean id="websiteControllerSetterInjection"
class="com.baeldung.dependencyinjections.WebsiteControllerSetterInjection">
</bean>

</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.baeldung.dependencyinjections;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class WebsiteTest {

ApplicationContext annotationContext = new AnnotationConfigApplicationContext(AppConfig.class);
ApplicationContext xmlContext = new ClassPathXmlApplicationContext("dependencyinjections-context.xml");

public static final String EXPECTED_STRING = "Home page contents";

@Test
public void testAnnotationConfigConstructorBasedDI() {
WebsiteControllerConstructorInjection controller = annotationContext.getBean(WebsiteControllerConstructorInjection.class);
assertEquals(EXPECTED_STRING, controller.getContentsViaConstructorInjection());
}

@Test
public void testXMLConfigConstructorBasedDI() {
WebsiteControllerConstructorInjection controller = xmlContext.getBean(WebsiteControllerConstructorInjection.class);
assertEquals(EXPECTED_STRING, controller.getContentsViaConstructorInjection());
}

@Test
public void testAnnotationConfigSetterBasedDI() {
WebsiteControllerSetterInjection controllerwithSetterInjection = (WebsiteControllerSetterInjection) annotationContext.getBean("websiteControllerSetterInjection");
assertEquals(EXPECTED_STRING, controllerwithSetterInjection.getContentsViaSetterInjection());
}

@Test
public void testXMLConfigSetterBasedDI() {
WebsiteControllerSetterInjection controllerwithSetterInjection = (WebsiteControllerSetterInjection) xmlContext.getBean("websiteControllerSetterInjection");
assertEquals(EXPECTED_STRING, controllerwithSetterInjection.getContentsViaSetterInjection());
}

}