Spring Framework Notes

Spring Framework Lecture Notes

Midterm – October 20th

Spring Framework

  • Levels of Abstraction
    • Specification -> HTTP Protocol Specification (RFC)
    • Implementation -> ServerSocket, Socket
    • URLConnection
    • Servlets -> we were able to create Sessions, tracking, redirection, unit testing
      • We have 2–3 forms each of them go to the same doPost statement…

      *

    • Spring Framework
      • Now we can map methods to URLs, giving us fine grained control of resources
      • It’s an application container for Java that provides many useful features, such as
        • Dependency injection
        • Transaction management
        • Support for developing aspect oriented programs
          • We can do aspect oriented programming using Filters
          • Error handling, authentication, and such
        • Request/Response are abstracted providing method-level mapping for resources
          • logical-core-groupings

Dispatcher Servlet

A place we would trap all of out requests and map them to the actual method in the servlet. While a filter will be placed in front of the servlets we defined. A dispatcher would be more comprehensive, having a broader reach than a filter. The routing happens within the Dispatcher Servlet, something built into the Spring Framework.
It can be viewed as the Central dispatcher for HTTPrequest handlers/ controllers
A we application can define any number of DSs.

    <servlet>
        servlet-name springDispatcher
        servlet-class   org.springframework.web.servbletservletDispatcher
        init-param
            contextConfigLocation
            /WEB-ING/servletContext.xml
        /init-param
        load-on-startup 1 /load-on-startup

        <servlet-mapping>
            <servlet-name> springDispatcher </servlet-name>
            <url-pattern>  / </url-pattern>
        </servlet-mapping>

Bootstrapping and configuration

Just starting up of the framework. XML-based (deployment descriptor) or Java-based (programmatically)
The deployment descriptor requires an instance of the DS to be given a configuration filein the form of a contextConfigLocation ini parameter and instructed to load on startup. (Example above)

    #web.xml
    <context-param>
        <param-name> contextConfigLocation</param-name>
        <param-value>/WEB-INF/rootContext.xml</param-value>
    </context-param>
    This will be independent of any servlet.

    #rootcontext.xm l
    <beans>
        <bean name="greetingServiceImpl" class="GreetingServiceImpl" />
    </beans>
    Contains the bean "stack"

    HelloConteoller.java
    @Controller
    public class HelloController{
    @ResponseBody
    @RequestMapping(value = "/",params = {"name","lastname"}, method=RequestMethod.GET)
    public String helloWorld()
    {
        return "Hello World";
    }
    ....@RequestParam("name")...

    }

    #servletContext.xml
    <beans>
       <bean name="helloController" class="HelloController">
            <property name="greetingsService" ref="greetingServiceImpl" />
       </bean>
                ##### We could also create the bean within this file 
                <bean name="greetingServiceImpl" class="GreetingServiceImpl" />
                #### Scoping option... 
    </beans>

The Spring framework will wire the @ parameters and specifications together for us. There is no constructor where we create the GreetingService and we don’t have any routing logic anywhere in the code. Spring allows us to use these classes as part of our class construction.

The bean element defines a name and class, the same we use in the java file without actually specifying it.
So what we need to do….

    public void setGreetingService(GreetingService greetingService)
    {
        this.greetingService = greetingService;
    }
    // This is where we can configure a fresh bean

We need to show Spring how to wire our classes to the Controller, which happens through the setter service above.

Name of the setter method needs to be set setGreetingService…… needs to follow the naming convention. Java reflection -> find all words with set…

SO WHAT IS A JAVA BEANNN!!!!!

Java class that is built based on the following convention:
Properties are accessible through getter/setter methods….?

Application Context

What is actually going on? At the heart of it… it boils down the to Application Context. Represented by org.springframework.context.ApplicationContext interface
IoC = Inversion of Control
Business Objects (POJOs) -> | |
| The Spring Container |
Configuration Metadata -> | |
Now your program doesn’t have to worry about parameters and methods, it’s just handed a fully functioning system that’s ready for use.

Dependency Injection

IoC is a technique to perform dependency injection
Why IoC?
Instead of our program creating the objects, the container creates them and injects them into our class. So they can be instantiated at run time instead of compile time. We can inject dependencies at run time and at testing time.

DI -> Used interchangeably
Setter injection
    setter methods need to be defined for each dependent variable:
        Example: HelloControlelr
Constructor injection
    Constructor parameters passed in for each dependent variable....


            HelloController1.java
            HelloController1( GreetingService greetingService, URLQueryingService urlQueryingService)
            {
                this.greetingService = greetingService;

            }       

            servletContext.xml
            <bean name="helloController" class="com.wrox.HelloController">
                <property name="greetingsService" ref="greetingServiceImpl" />
                <property name="urlQueryingService" ref="URLQueryingService" />
            </bean>
            <bean name="helloController1" class="com.wrox.HelloController1">
            <constructor-arg name="" ref=""  /> // Same as above just starts with constructor-arg
            <construstor-arg name="" ref=""  />
            </bean>

Dependency injection for Unit Testing

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s