-->
Showing posts with label Spring Framework. Show all posts
Showing posts with label Spring Framework. Show all posts

Sunday, June 2, 2019

Spring Core Containers and Dependency Injection

Java classes should be as independent as possible from each other.


  • To decouple classes from one another, dependencies should be injected through Constructors and Setters.
  • Classes should not configure themselves. IoC uses dependency injection to: 
    • Configure a class correctlyi from outside the class
    • Wire services or components.
  • Piecing together all the beans in the Spring Container is called Wiring.
    • This can most commonly be don through xml.
    • Various BeanFactories and ApplicationContext objects that support wiring include:
      • XMLBeanFactory
      • ClassPathXMLApplicationContext
      • FileSystemXMLApplicationContext
      • XMLWebApplicationContext

Spring Fundamentals

Originally developed to fight against low level maintenance, Spring framework functionality can be used in any JavaEE server and most of it is also adaptable to non-manage frameworks.

Most JavaEE applications are complex and require a lot of effort to develop.

Spring's BeanFactory enables configuration options from XML by providing dependencies when required, automatically.

Spring is composed of 7 key components. These modules can be chosen freely as needed.

  1. Spring Coreseparates configuration from dependencies
    • Prime Components: BeanFactory; Application contexts.
    • Bean Factory: Provides support for dependency injection.
    • Application Contexts: provides application framework services.
  2. Spring Context
    • A configuration file that provides contextual information to the Spring Framework. JNDI, EJB, email, scheduling and internalization services.
  3. Spring AOP
    • Integrates Aspect Oriented Programming through its configuration management feature, providing transaction management services for objects in any Spring-based application.
  4. Spring DAO
    • Data Access Object abstraction layer offers a meaningful hierarchy for the managing of exceptions and error messages. This greatly reduces the amount of code you need to write, such as opening and closing connections.
  5. Spring ORM
    • Object Relational Map -Object oriented programs and relational databases tend to speak completely different languages. This bridges that gap. This provides support to several ORM solutions, including Hibernate, JDO, JPA, and others.
  6. Spring Web Module
    • Builds on top of the application context module, to provide contexts for Web-based applications. The Web module
  7. Spring MVC
    • A full featured MVC implementation for building Web-applications. Highly configurable via strategy interfaces. It accommodates numerous view technologies including JSP, Velocity, Tile, and so on.

Monday, March 23, 2015

Java Beans and Serialization

A java bean encapsulates the scripting for setting and getting information from a database. You make them Serializable (aka, savable). They help you manage rows of data to update and delete as you adapt your management class to various data structures. Beans are rather simple to correct and modify.

Videos that demonstrate the concept can be shown here.

<iframe width="560" height="315" src="https://www.youtube.com/embed/eMMLaneVkjc" frameborder="0" allowfullscreen></iframe>

JavaBeans are among the prime features of frameworks such as Apache Spring or Java Database Connectivity (JDBC).  An extension of Factory Patterns, they help free developers from relying on too many class dependencies by encapsulating the code that creates objects. This greatly assists maintenance, as per the Dependency Inversion Principle.



Dependency Inversion Principle - depend on abstractions, not concrete classes.

To speak more of Serialization, the concept here is to safe the instance variables (aka the "state") of your class objects to a FileOutputStream (foo for short). Such methods are risky, so you may want to put them into a try-catch block.
If there is an instance variable you don't want saved, mark it as transient.  Among the conveniences of Serialization is that the entire object graph is Serialized. That means any object values referenced by the class is also saved.



For example, say you have a Car Class and it references classes for Engines and Wheels. What you have with these various pointers within the Car Class is a graph, and Serialize will save all this for you.



Serialization can be more convenient then other paradigms for saving information (such as a Comma Separated Value document), but Serialization is limited to Java Applications only. If such information must be cross compatible among programming languages, you wish need to use a FileWriter to save a String that contains the information you wish to save.


~Code Crunch Corner~