SlideShare a Scribd company logo
Dependency Injection with Spring
in 10 minutes
Corneil du Plessis
corneil.duplessis@gmail.com
@corneil
Introduction
● Assumptions
– You have some appreciation of component-oriented
development
– You have seen Java code
– You have heard of the Spring Framework or Dependency
Injection
● Take away
– Some appreciation for benefits of dependency injection
– Some understanding of how Spring supports dependency
injection.
What is Dependency Injection?
● Robert C Martin and Martin Fowler has written some of the best
articles on the subject.
● Dependency Injection is also known as Inversion of Control or
Dependency Inversion.
● DI is a specific form of IoC.
● When an object is composed the responsibility for composing the
dependents of the object is not handled by the specific object.
● Modern applications uses one or more containers or frameworks to
take care of DI.
● Examples are EJB container and Spring Framework.
● We will look at mechanisms provider by Spring Framework
What is Spring Framework?
● The Spring Framework provides support for a large
number of useful programming patterns.
● Patterns in questions are:
– Singleton
– Factory
– Locator
– Visitor
DI – Best Practice
● Required unchanging dependencies via constructor.
● Assemble and fail early
● Be careful of the cost of DI frameworks.
– Don't use to create Data Transfer Objects.
DI – Sample Classes
DI – Sample Objects
DI – Code without DI
public ProfileDataAccessComponent() throws NamingException, FileNotFoundException, IOException {
super();
// In EJB or Web Container
try {
InitialContext ctx = new InitialContext();
dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/profile");
} catch(Throwable x) {
// Assume we are not in container.
}
if(dataSource == null) {
// Manual creation outside of container
Properties props = new Properties();
File propFile = new File("db.properties");
props.load(new FileInputStream(propFile));
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(props.getProperty("db.driver"));
ds.setUsername(props.getProperty("db.username"));
ds.setPassword(props.getProperty("db.password"));
ds.setUrl(props.getProperty("db.url"));
ds.setMaxActive(Integer.parseInt(props.getProperty("db.maxActive", "10")));
ds.setMaxIdle(Integer.parseInt(props.getProperty("db.maxIdle", "5")));
ds.setInitialSize(Integer.parseInt(props.getProperty("db.initialSize", "5")));
ds.setValidationQuery(props.getProperty("db.validationQuery", "SELECT 1"));
dataSource = ds;
}
}
DI – More Code
public ProfileNotificationSender() throws NamingException {
super();
InitialContext ctx = new InitialContext();
factory = (ConnectionFactory)
ctx.lookup("java:comp/env/jms/queue/ConnectionFactory");
destination = (Destination) ctx.lookup("java:comp/env/jms/queue/Profile");
}
DI – Spring Beans from XML
<context:property-placeholder location="db.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
<bean id="profileDAC"
class="org.springframework.di.demo.ProfileDataAccessComponent">
<constructor-arg ref="dataSource" />
</bean>
DI – Spring Beans from XML
<jee:jndi-lookup id="destination" jndi-name="jms/queue/Profile"
expected-type="javax.jms.Destination" />
<jee:jndi-lookup id="connectionFactory" jndi-name="jms/queue/ConnectionFactory"
expected-type="javax.jms.ConnectionFactory" />
<bean id="notificationSender"
class="org.springframework.di.demo.ProfileNotificationSender">
<constructor-arg ref="connectionFactory" />
<constructor-arg ref="destination" />
</bean>
<bean id="profileService" class="org.springframework.di.demo.ProfileService">
<constructor-arg ref="profileDAC" />
<constructor-arg ref="notificationSender" />
</bean>
DI – Spring Annotations Config
<context:property-placeholder location="db.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
<jee:jndi-lookup id="destination" jndi-name="jms/queue/Profile"
expected-type="javax.jms.Destination" />
<jee:jndi-lookup id="connectionFactory" jndi-name="jms/queue/ConnectionFactory"
expected-type="javax.jms.ConnectionFactory" />
<context:component-scan base-package="org.springframework.di.autowire" />
DI – Spring Annotations Code
@Autowired
public ProfileNotificationSender(ConnectionFactory factory,
Destination destination) {
super();
this.factory = factory;
this.destination = destination;
}
@Service("profileService")
public class ProfileService implements ProfileInteface {
private ProfileDataAccessInterface dataAccess;
private ProfileNotificationInterface notification;
@Autowired
public ProfileService(ProfileDataAccessInterface dataAccess,
ProfileNotificationInterface notification) {
super();
this.dataAccess = dataAccess;
this.notification = notification;
}
DI – Spring Java Config
@Configuration
public class AppConfig {
@Autowired
Environment env;
@Bean
public Destination destination() {
JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
bean.setJndiName("jms/queue/Profile");
bean.setExpectedType(Destination.class);
return (Destination) bean.getObject();
}
@Bean
public ConnectionFactory connectionFactory() {
JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
bean.setJndiName("jms/queue/ConnectionFactory");
bean.setExpectedType(ConnectionFactory.class);
return (ConnectionFactory) bean.getObject();
}
}
DI – Spring Java Config
@Configuration
@PropertySource("db.properties")
public class AppConfig {
@Autowired
Environment env;
@Bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(env.getProperty("db.driver"));
dataSource.setUrl(env.getProperty("db.url"));
dataSource.setUsername(env.getProperty("db.username"));
dataSource.setPassword(env.getProperty("db.password"));
return dataSource;
}
}
Questions
● Demo code at
https://github.com/corneil/demos/tree/master/spring-di-sample
● Discuss on JUG Facebook https://www.facebook.com/groups/jozijug
● Discuss on JUG Meetup

More Related Content

PDF
Dependency Injection
Giovanni Scerra ☃
 
PDF
JavaScript - Chapter 15 - Debugging Techniques
WebStackAcademy
 
PPTX
Java Spring
AathikaJava
 
PPTX
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Arjun Thakur
 
PDF
Spring Data JPA
Cheng Ta Yeh
 
PPTX
Dependency injection ppt
Swati Srivastava
 
PPTX
Spring beans
Roman Dovgan
 
Dependency Injection
Giovanni Scerra ☃
 
JavaScript - Chapter 15 - Debugging Techniques
WebStackAcademy
 
Java Spring
AathikaJava
 
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Arjun Thakur
 
Spring Data JPA
Cheng Ta Yeh
 
Dependency injection ppt
Swati Srivastava
 
Spring beans
Roman Dovgan
 

What's hot (20)

PPTX
Spring boot
sdeeg
 
PDF
Introduction to Spring's Dependency Injection
Richard Paul
 
PDF
Karate - powerful and simple framework for REST API automation testing
Roman Liubun
 
PDF
REST API and CRUD
Prem Sanil
 
PDF
Redux Saga - Under the hood
Waqqas Jabbar
 
PDF
Spring Framework - Core
Dzmitry Naskou
 
PDF
REST APIs with Spring
Joshua Long
 
PDF
Workshop 21: React Router
Visual Engineering
 
PPTX
java 8 new features
Rohit Verma
 
PDF
Spring boot introduction
Rasheed Waraich
 
PDF
Spring Boot
Jaran Flaath
 
PPTX
React + Redux Introduction
Nikolaus Graf
 
PPTX
Spring data jpa
Jeevesh Pandey
 
PPTX
Redux workshop
Imran Sayed
 
PPT
Struts
s4al_com
 
PDF
PUC SE Day 2019 - SpringBoot
Josué Neis
 
PPT
jQuery
Mohammed Arif
 
PDF
Introduction to Spring Framework
Hùng Nguyễn Huy
 
PPTX
Spring Boot Tutorial
Naphachara Rattanawilai
 
PDF
Spring Framework - AOP
Dzmitry Naskou
 
Spring boot
sdeeg
 
Introduction to Spring's Dependency Injection
Richard Paul
 
Karate - powerful and simple framework for REST API automation testing
Roman Liubun
 
REST API and CRUD
Prem Sanil
 
Redux Saga - Under the hood
Waqqas Jabbar
 
Spring Framework - Core
Dzmitry Naskou
 
REST APIs with Spring
Joshua Long
 
Workshop 21: React Router
Visual Engineering
 
java 8 new features
Rohit Verma
 
Spring boot introduction
Rasheed Waraich
 
Spring Boot
Jaran Flaath
 
React + Redux Introduction
Nikolaus Graf
 
Spring data jpa
Jeevesh Pandey
 
Redux workshop
Imran Sayed
 
Struts
s4al_com
 
PUC SE Day 2019 - SpringBoot
Josué Neis
 
Introduction to Spring Framework
Hùng Nguyễn Huy
 
Spring Boot Tutorial
Naphachara Rattanawilai
 
Spring Framework - AOP
Dzmitry Naskou
 
Ad

Viewers also liked (9)

PPTX
Spring dependency injection
srmelody
 
PDF
Spring and dependency injection
Steve Ng
 
ODT
Types of Dependency Injection in Spring
Sunil kumar Mohanty
 
DOCX
Junit With Eclipse
Sunil kumar Mohanty
 
ODT
Spring IOC advantages and developing spring application sample
Sunil kumar Mohanty
 
PPTX
Types of containers
Sagar Gadhiya(PaTel)
 
ODP
Different Types of Containers in Spring
Sunil kumar Mohanty
 
PPTX
Types of containers
MAX GALARZA HERNANDEZ
 
PPT
A Brief presentation on Containerisation
subhash_ae
 
Spring dependency injection
srmelody
 
Spring and dependency injection
Steve Ng
 
Types of Dependency Injection in Spring
Sunil kumar Mohanty
 
Junit With Eclipse
Sunil kumar Mohanty
 
Spring IOC advantages and developing spring application sample
Sunil kumar Mohanty
 
Types of containers
Sagar Gadhiya(PaTel)
 
Different Types of Containers in Spring
Sunil kumar Mohanty
 
Types of containers
MAX GALARZA HERNANDEZ
 
A Brief presentation on Containerisation
subhash_ae
 
Ad

Similar to Dependency Injection in Spring in 10min (20)

PPT
Spring talk111204
ealio
 
PPT
Story ofcorespring infodeck
Makarand Bhatambarekar
 
PPTX
Spring (1)
Aneega
 
PDF
Introduction to Spring Framework
Rajind Ruparathna
 
PPT
Spring Basics
Dhaval Shah
 
PPT
Spring training
shah_d_p
 
PDF
Getting Started With Spring Framework J Sharma Ashish Sarin
moineittay
 
PPTX
Spring Basics
Emprovise
 
PPTX
Introduction to Spring Framework
Dineesha Suraweera
 
PPTX
Skillwise-Spring framework 1
Skillwise Group
 
PPTX
Spring framework part 2
Haroon Idrees
 
PPTX
Spring framework
Rajkumar Singh
 
PPT
Spring overview &amp; architecture
saurabhshcs
 
PPT
Hybernat and structs, spring classes in mumbai
Vibrant Technologies & Computers
 
DOCX
02 java spring-hibernate-experience-questions
Dhiraj Champawat
 
PDF
Spring db-access mod03
Guo Albert
 
PDF
Spring Reference
asas
 
PDF
Manual tutorial-spring-java
sagicar
 
PDF
Spring Reference
Syed Shahul
 
PPT
SpringIntroductionpresentationoverintroduction.ppt
imjdabhinawpandey
 
Spring talk111204
ealio
 
Story ofcorespring infodeck
Makarand Bhatambarekar
 
Spring (1)
Aneega
 
Introduction to Spring Framework
Rajind Ruparathna
 
Spring Basics
Dhaval Shah
 
Spring training
shah_d_p
 
Getting Started With Spring Framework J Sharma Ashish Sarin
moineittay
 
Spring Basics
Emprovise
 
Introduction to Spring Framework
Dineesha Suraweera
 
Skillwise-Spring framework 1
Skillwise Group
 
Spring framework part 2
Haroon Idrees
 
Spring framework
Rajkumar Singh
 
Spring overview &amp; architecture
saurabhshcs
 
Hybernat and structs, spring classes in mumbai
Vibrant Technologies & Computers
 
02 java spring-hibernate-experience-questions
Dhiraj Champawat
 
Spring db-access mod03
Guo Albert
 
Spring Reference
asas
 
Manual tutorial-spring-java
sagicar
 
Spring Reference
Syed Shahul
 
SpringIntroductionpresentationoverintroduction.ppt
imjdabhinawpandey
 

More from Corneil du Plessis (15)

PPTX
Sweet Streams (Are made of this)
Corneil du Plessis
 
PPTX
Cloud Native Applications for Cloud Foundry using Spring Cloud : A Workshop
Corneil du Plessis
 
PPTX
QueryDSL - Lightning Talk
Corneil du Plessis
 
PPTX
Enhancements in Java 9 Streams
Corneil du Plessis
 
PPTX
Reactive Spring 5
Corneil du Plessis
 
PPTX
Empathic API-Design
Corneil du Plessis
 
ODP
Performance Comparison JVM Languages
Corneil du Plessis
 
ODP
Microservices Patterns and Anti-Patterns
Corneil du Plessis
 
ODP
Consume Spring Data Rest with Angularjs
Corneil du Plessis
 
ODP
The Evolution of Java
Corneil du Plessis
 
ODP
Gradle: The Build System you have been waiting for!
Corneil du Plessis
 
ODP
Polyglot persistence with Spring Data
Corneil du Plessis
 
ODP
Data repositories
Corneil du Plessis
 
PPT
Gradle: The Build system you have been waiting for
Corneil du Plessis
 
ODP
Spring Data in 10 minutes
Corneil du Plessis
 
Sweet Streams (Are made of this)
Corneil du Plessis
 
Cloud Native Applications for Cloud Foundry using Spring Cloud : A Workshop
Corneil du Plessis
 
QueryDSL - Lightning Talk
Corneil du Plessis
 
Enhancements in Java 9 Streams
Corneil du Plessis
 
Reactive Spring 5
Corneil du Plessis
 
Empathic API-Design
Corneil du Plessis
 
Performance Comparison JVM Languages
Corneil du Plessis
 
Microservices Patterns and Anti-Patterns
Corneil du Plessis
 
Consume Spring Data Rest with Angularjs
Corneil du Plessis
 
The Evolution of Java
Corneil du Plessis
 
Gradle: The Build System you have been waiting for!
Corneil du Plessis
 
Polyglot persistence with Spring Data
Corneil du Plessis
 
Data repositories
Corneil du Plessis
 
Gradle: The Build system you have been waiting for
Corneil du Plessis
 
Spring Data in 10 minutes
Corneil du Plessis
 

Recently uploaded (20)

PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
PDF
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Software Development Methodologies in 2025
KodekX
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 

Dependency Injection in Spring in 10min

  • 1. Dependency Injection with Spring in 10 minutes Corneil du Plessis [email protected] @corneil
  • 2. Introduction ● Assumptions – You have some appreciation of component-oriented development – You have seen Java code – You have heard of the Spring Framework or Dependency Injection ● Take away – Some appreciation for benefits of dependency injection – Some understanding of how Spring supports dependency injection.
  • 3. What is Dependency Injection? ● Robert C Martin and Martin Fowler has written some of the best articles on the subject. ● Dependency Injection is also known as Inversion of Control or Dependency Inversion. ● DI is a specific form of IoC. ● When an object is composed the responsibility for composing the dependents of the object is not handled by the specific object. ● Modern applications uses one or more containers or frameworks to take care of DI. ● Examples are EJB container and Spring Framework. ● We will look at mechanisms provider by Spring Framework
  • 4. What is Spring Framework? ● The Spring Framework provides support for a large number of useful programming patterns. ● Patterns in questions are: – Singleton – Factory – Locator – Visitor
  • 5. DI – Best Practice ● Required unchanging dependencies via constructor. ● Assemble and fail early ● Be careful of the cost of DI frameworks. – Don't use to create Data Transfer Objects.
  • 6. DI – Sample Classes
  • 7. DI – Sample Objects
  • 8. DI – Code without DI public ProfileDataAccessComponent() throws NamingException, FileNotFoundException, IOException { super(); // In EJB or Web Container try { InitialContext ctx = new InitialContext(); dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/profile"); } catch(Throwable x) { // Assume we are not in container. } if(dataSource == null) { // Manual creation outside of container Properties props = new Properties(); File propFile = new File("db.properties"); props.load(new FileInputStream(propFile)); BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName(props.getProperty("db.driver")); ds.setUsername(props.getProperty("db.username")); ds.setPassword(props.getProperty("db.password")); ds.setUrl(props.getProperty("db.url")); ds.setMaxActive(Integer.parseInt(props.getProperty("db.maxActive", "10"))); ds.setMaxIdle(Integer.parseInt(props.getProperty("db.maxIdle", "5"))); ds.setInitialSize(Integer.parseInt(props.getProperty("db.initialSize", "5"))); ds.setValidationQuery(props.getProperty("db.validationQuery", "SELECT 1")); dataSource = ds; } }
  • 9. DI – More Code public ProfileNotificationSender() throws NamingException { super(); InitialContext ctx = new InitialContext(); factory = (ConnectionFactory) ctx.lookup("java:comp/env/jms/queue/ConnectionFactory"); destination = (Destination) ctx.lookup("java:comp/env/jms/queue/Profile"); }
  • 10. DI – Spring Beans from XML <context:property-placeholder location="db.properties" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${db.driver}" /> <property name="url" value="${db.url}" /> <property name="username" value="${db.username}" /> <property name="password" value="${db.password}" /> </bean> <bean id="profileDAC" class="org.springframework.di.demo.ProfileDataAccessComponent"> <constructor-arg ref="dataSource" /> </bean>
  • 11. DI – Spring Beans from XML <jee:jndi-lookup id="destination" jndi-name="jms/queue/Profile" expected-type="javax.jms.Destination" /> <jee:jndi-lookup id="connectionFactory" jndi-name="jms/queue/ConnectionFactory" expected-type="javax.jms.ConnectionFactory" /> <bean id="notificationSender" class="org.springframework.di.demo.ProfileNotificationSender"> <constructor-arg ref="connectionFactory" /> <constructor-arg ref="destination" /> </bean> <bean id="profileService" class="org.springframework.di.demo.ProfileService"> <constructor-arg ref="profileDAC" /> <constructor-arg ref="notificationSender" /> </bean>
  • 12. DI – Spring Annotations Config <context:property-placeholder location="db.properties" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${db.driver}" /> <property name="url" value="${db.url}" /> <property name="username" value="${db.username}" /> <property name="password" value="${db.password}" /> </bean> <jee:jndi-lookup id="destination" jndi-name="jms/queue/Profile" expected-type="javax.jms.Destination" /> <jee:jndi-lookup id="connectionFactory" jndi-name="jms/queue/ConnectionFactory" expected-type="javax.jms.ConnectionFactory" /> <context:component-scan base-package="org.springframework.di.autowire" />
  • 13. DI – Spring Annotations Code @Autowired public ProfileNotificationSender(ConnectionFactory factory, Destination destination) { super(); this.factory = factory; this.destination = destination; } @Service("profileService") public class ProfileService implements ProfileInteface { private ProfileDataAccessInterface dataAccess; private ProfileNotificationInterface notification; @Autowired public ProfileService(ProfileDataAccessInterface dataAccess, ProfileNotificationInterface notification) { super(); this.dataAccess = dataAccess; this.notification = notification; }
  • 14. DI – Spring Java Config @Configuration public class AppConfig { @Autowired Environment env; @Bean public Destination destination() { JndiObjectFactoryBean bean = new JndiObjectFactoryBean(); bean.setJndiName("jms/queue/Profile"); bean.setExpectedType(Destination.class); return (Destination) bean.getObject(); } @Bean public ConnectionFactory connectionFactory() { JndiObjectFactoryBean bean = new JndiObjectFactoryBean(); bean.setJndiName("jms/queue/ConnectionFactory"); bean.setExpectedType(ConnectionFactory.class); return (ConnectionFactory) bean.getObject(); } }
  • 15. DI – Spring Java Config @Configuration @PropertySource("db.properties") public class AppConfig { @Autowired Environment env; @Bean public DataSource dataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(env.getProperty("db.driver")); dataSource.setUrl(env.getProperty("db.url")); dataSource.setUsername(env.getProperty("db.username")); dataSource.setPassword(env.getProperty("db.password")); return dataSource; } }
  • 16. Questions ● Demo code at https://github.com/corneil/demos/tree/master/spring-di-sample ● Discuss on JUG Facebook https://www.facebook.com/groups/jozijug ● Discuss on JUG Meetup