Spring Core - Java

  • Coupling
    • Tight => The two classes often change together
      • Composition
    • Loose => Means the two classes are mostly independent
      • Example => Code is still tightly coupled to the object creation logic
            public class Employee {
                private String name;
                private Address address;
                public Employee (Address address) {
                    this.address=address;
                }
            }
        
  • Association
    • Association in Java defines the connection between two classes that are set up through their objects
    • Relationships
      • one-to-one
      • one-to-many
      • many-to-one
      • many-to-many
    • Types
      • is-a => Inheritance
      • has-a
        • Aggregation
          • If two entities are in the aggregation composition, and one entity fails due to some error, it will not affect the other entity
        • Composition
          • A restricted form of the Aggregation where the entities are strongly dependent on each other
          • If an object contains the other object and the contained object cannot exist without the existence of that object, then it is called composition
          • The composition is achieved by using an instance variable that refers to other objects
          • Java doesn’t support multiple inheritances but by using composition we can achieve it
  • IoC (Inversion of Control)
    • Normal flow of control is achieved by JVM
    • Java follows container component modal, all objects acts as component
    • Runtime capable of creating object and priving it back to the code
    • Frameworks
      • Spring
        • Dependency Injection
          • Spring Container => File xml/java
            • BeanFactory
            • ApplicationContext
          • Setter Injection
            • If happened through Setter methods
            • XML shortcut with p-namespace
              • Enables you to use the bean element attributes, instead of nested <property/> elements
          • Container Injection
            • Its dependencies are Mandatory dependencies
            • Order should be same, Can use type or index or name attribute instead to order
            • XML shortcut with c-namespace
              • Usage of inline attributes for configuring the constructor arguments
          • Mixed dependency injection
            • Constructor based used for immutable dependencies, and Setter based for optional dependencies
          • Inner Bean
            • Bean inside a bean, can't be used outside the bean it is defined
          • Bean Scopes
            • Singleton
            • Prototype
              • Create a new bean instance each time a bean is requested
          • Injection using Factory Bean
        • Auto-wiring
          • Automatic Dependency injection
          • Eliminates the need to specify bean references via property & contructor-org element
          • Modes
            • byName, byType, Constructor
      • Guice
      • Pico
  • Annotations
    • Will not work with static fields and methods, done before XML injection
    • Types
      • Spring Standard Annotations
        • @Value("#{nameObj}") => Injects value from the properties file
        • @Autowired => AUtomatically wires Spring beans into other beans
      • JSR 250
        • @Resource
      • JSR 330
        • @Inject
        • @Named
        • @Singleton
      • Stero type
        • @Component => Used to mark any class as Spring managed component or bean
        • @Repository => Indicates class is a DAO that interacts with the database
        • @Service => Indicates class is service in service layer of the application
        • @Controller => Indicates that class is responsible for hadling web req & res
    • Java Configurations
      • @Configuration => Used to declare a XML file as Spring configuration
      • @Bean
      • @ComponentScan => Used to scan stereotype annotations
  • Spring Expression Language (SpEL)
    • Quering & Manipulating an object at runtime
    • Expression => #{expression}
    • Operators
  • Spring Test
    • Frameworks
      • junit
      • testng
    • Annotations
      • @ContextConfigurations => Defines how to load and configure ApplicationContext for JUnit test, can point to both XML and java files
        • @RunWith(SprintJunit4ClassRunner.class)
      • @DirtiesContext => Indicates the ApplicationContext is dirty
        • Provies methodMode & classMode
      • @ActiveProfiles => Activate one or more profiles for the test cases
Share: