Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Dependency Injection in Spring in 10min
1. Dependency Injection with Spring
in 10 minutes
Corneil du Plessis
corneil.duplessis@gmail.com
@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.
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