basic usage of java Spring Framework
Keywords
- Object Dependencies
- Dependency Injection
- Spring Containers
Dependency Injection
- also called Inversion of Control(IoC)
- Dependency Inversion Principle plus Single Responsibility Principles
### Kinds of DI
- Constructor Injection
<constructor-arg ref="cat"></constructor-arg>
- Method(Settor) Injection
<property name="myname" value="poodle"></property>
- Field Injection (Interface)
Advantages of DI
- Reduced Dependencies
- More Resuable Code
- More Testable Code
- More Readable Code
Bean definition for DI
Spring Container
- manage the bean object
Spring Container Metadata
- XML
- bean definition
- dependency injection
- Java Annotations
- Java Code
Spring Container Types
- BeanFactory
- The simplest factory, mainly for DI
- Used when resources are limited
- org.springframework.beans.factory.BeanFactory
- ex) XMLBeanFactory
- 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
Leave a comment