Dependency Injection in Spring in 10min

download Dependency Injection in Spring in 10min

If you can't read please download the document

Transcript of 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 11. DI Spring Beans from XML 12. DI Spring Annotations Config 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