Spring core

spring is a alternative of EJB (enterprise java beans), with lighter & leaner programming models

a spring bean refers to a spring application component; refer to any POJO (plain old java obj)

*Spring simplifies java development*

non-invasive: Spring never force you to use Spring-specific classes/interfaces, thus you can transplant spring projects to non-spring platform.

dependency injection: class A depends on B, B depends on C, if we define C inside B & B inside A, then when we want to change C, we’ll have to change corresponding B, A; To avoid this, we create separate spring bean for A, B, C, and use spring application context to create A/B/C objects and wire them together, thus, A is created without knowing detailed implementation of B & C.

AOP (Aspect Oriented Programming): suppose in a system, class A object first call A’s method, and will trigger class B call B’s method. If we write & call B’s method inside A bean, then B has to be injected into A, and A must maintain B all the time. To avoid this, we turn B into a aspect in Spring application context, and define B’s method to be called after A’s method is called, but not inject B into A.

AOP – declare B as an aspect: use Spring’s <aop:config> to declare B as an aspect, and first declare B as a bean, then refer to that bean in <aop:aspect> . Since B’s method will be triggered after A’s method is called, so inside B’s aspect, define A’s method as pointcut, and refer B’s method to that pointcut. Thus, B’s method applied to A without A needing to call on it inside A.

Spring template: eliminate boilerplate code. If you want to query an employee by his id in a Java JDBC API, besides the core query algorithm, JDBC requires that you must write create a connection, create a statement, and catch SQL exceptions…etc. But Spring provides template for such JDBC requirements, and let you focus only on the core query algorithm.

*spring container*

spring container: it creates objects, inject DI into them, and wire them together, configures them, and mange their lifecycle from begin to end.

two-kind container implementation: bean factory & application context.

an application context: create it by loading application context from XML/Java config file, from filesystem/classpath. (what is classpath? see: https://stackoverflow.com/questions/2396493/what-is-a-classpath)

application context retrieve beans: use the context’s getBean() method.

bean’s lifecycle: see 1.2.2

*Spring Landscape*

JAR file: a JAR (java archive) file is a package file format used to aggregate many java class files and associate metadata and resources (image, sound, etc.) into one file for distribution.

spring lib folder: 20 distinct modules, with 3 JAR files for each one (the binary class library, the source JAR file, and a javadoc JAR file). 20 JAR files can be arranged into 6 categories of functionality – Figure 1.7.

*3 spring wiring mechanisms*

**automatic wiring**

component scanning: originally, in spring configuration file, we should define class A, B as beans and wire them together inside a configuration class; now we annotate the config class with @ComponentScan, and annotate A, B with @Component, then @ComponentScan will  by default scan the config class’s package and any subpackages underneath, look for classes that are annotated with @Component (A, B), and create beans for them in spring.

name a component-scanned bean: all beans in spring config file are given an ID. If class A is annotated with only @Component, then it is implicitly given an ID by lowercasing the first letter of the class name; you can also self-define the ID and annotate class A with @Component(“xxx”) or @Named(“xxx”) since @Named is alternative to @Component in most cases.

set base packages for component scanning: if the config class is annotated with @ComponentScan, spring will by default take the config class’s package as the base package and scan for components; if @ComponentScan(basePackageClasses={K.class, M.class}), spring will take whatever packages that class K&M are in as base packages.

autowiring: if class A is annotated with @Autowired/@Inject, then when spring creates A bean, it will automatically satisfy bean A’s dependencies by finding other beans in the application context that are a match to bean A’s needs.

@Autowired: it can be used in any methods in class A. if no matching beans found for a DI, spring will throw an exception as the application context is created; if multiple matching beans found for a DI, spring will throw an exception indicating ambiguity with auto-wiring.


structs: early time web mvc framework.

dao: is an object that provides an abstract interface to some type of database or other persistence mechanism. By mapping application calls to the persistence layer (hibernate), the DAO provides some specific data operations without exposing details of the database.

hibernate: It provides a framework for mapping an object-oriented domain model to a relational database, it can replace SQL to retrieving data from database.


Leave a Reply

Your email address will not be published. Required fields are marked *