Dependency Injection in Spring in 10min

920 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
920
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Dependency Injection in Spring in 10min

  1. 1. Dependency Injection with Spring in 10 minutes Corneil du Plessis corneil.duplessis@gmail.com @corneil
  2. 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. 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. 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. 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. 6. DI – Sample Classes
  7. 7. DI – Sample Objects
  8. 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. 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. 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. 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. 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. 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. 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. 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. 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

×