basic usage of java Spring Framework

1 minute read

Keywords

  • Object Dependencies
  • Dependency Injection
  • Spring Containers

Features of Spring

Dependency Injection

  • also called Inversion of Control(IoC)
  • Dependency Inversion Principle plus Single Responsibility Principles

### Kinds of DI

  1. Constructor Injection
    <constructor-arg ref="cat"></constructor-arg>
    
  2. Method(Settor) Injection
    <property name="myname" value="poodle"></property>
    
  3. Field Injection (Interface)

Advantages of DI

  1. Reduced Dependencies
  2. More Resuable Code
  3. More Testable Code
  4. More Readable Code

Bean definition for DI

Spring Container

  • manage the bean object

Spring Container Metadata

  1. XML
    • bean definition
    • dependency injection
  2. Java Annotations
  3. Java Code

Spring Container Types

  1. BeanFactory
    • The simplest factory, mainly for DI
    • Used when resources are limited
    • org.springframework.beans.factory.BeanFactory
    • ex) XMLBeanFactory
  2. ApplicationContext
    • The advanced and more complex factory
    • Features

      1) Enterprise-aware functions 2) Publish application events to listeners 3) Wire and dispose beans on request

    • org.springframework.context.ApplicationConText
    • ex) ClassPathXmlApplicationContext
      ApplicationContext context = new ClassPathXmlApplicationContext("spring.di.beans.bean.xml");
      HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");
      

Embeded Redis Server for Spring test

Spring framework web example in Windows 10

1. install java

  • java -version

    openjdk version “11.0.7” 2020-04-14

  • add JAVA_HOME to system variables in Windows or profile in linux

    2. maven

  • download maven : https://maven.apache.org/download.cgi#
  • add MAVEN_HOME to system variable
  • add %MAVEN_HOME%\bin on the PATH
  • mvn -version

    Apache Maven 3.6.3

    3. Apache Tomcat

  • Download : https://tomcat.apache.org/download-80.cgi
  • conf/server.xml
    <!-- default -->
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    
    <!-- add URIEncoding -->
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               URIEncoding="UTF-8" />
    
  • conf/logging.properties
    <!-- default -->
    java.util.logging.ConsoleHandler.encoding = UTF-8
    
    <!-- change encoding if you need -->
    java.util.logging.ConsoleHandler.encoding = EUC-KR
    
  • conf/tomcat-users.xml
    • uncomment and change username and password ```xml <!–

    –>

    ```

  • Run : [cmd] - change to tomcat bin dir and execute [startup.bat]
  • Test : http://localhost:8080

4. Spring Boot

  • Create a Spring Boot Project and download
    • https://start.spring.io/
  • “src” and “pom.xml” are required and others can be deleted

Tags:

Categories:

Updated:

Leave a comment