Monday, October 5, 2015

Spring Core -- Interesting Observations



  1. Spring Framework -> It's programing model. 
  2. When doing Unit Test of Spring Application Try not to use Spring. Dependencies can easily be stubbed out for unit Testing.
  3. Used Java based configuration  as its more intuitive and easy to comprehend by Java programmer.
  4. If a Class want to have Fields which are mandatory for its creation/working make sure your use constructor for it. Try not to Auto-wire the private fields, It will be hard to test it.
  5. It is good practice to have a Integration Test for configuration to validate that your Spring Context only has bean which you intend to have.
  6. Try not to have schema version number in name space of your xml files, as correct version is automatically picked by Spring. Also it made easy for upgrade .  But if you add it  it does not add any value, but make your code hard to  upgrade.
  7. Try to have multiple configuration files one for each different aspect of your application.
  8. e.g application bean configuration. infrastructure configuration, rest configuration, soap configuration etc.
  9. User Spring Profile for managing your application based on different env.   Component which does not have any Profile annotation on it is always available along with the currently active profile components.
  10. Never rely on the Spring generated Names when using @Component can lead to run time issues.
  11. Java Configuration is arguably better than using @Component.


Transaction

Transaction will not work if you break the thread boundary. Transaction is Spring are stored in thread-local which are not  passed between threads.

So if a thread begins a transaction and in between your code you create a new thread then most probably your transaction wont work. as the newly created thread wont have the transaction element.


Constructor vs Setter Dependency when to use which?

Constructor :

  1. Mandatory fields
  2. Immutable dependencies
  3. Concise (pass several parameter at once).


Setter Dependency :

  1. Option /Changeable dependency
  2. Circular dependency
  3. Inherited automatically.












No comments:

Post a Comment