Spring Framework
- μλ° νλ«νΌμ μν μ€νμμ€ μ ν리μΌμ΄μ νλ μμν¬λ‘μ μν°νλΌμ΄μ¦κΈ μ ν리μΌμ΄μ μ κ°λ°νκΈ° μν λͺ¨λ κΈ°λ₯μ μ’ ν©μ μΌλ‘ μ 곡νλ κ²½λνλ μ루μ
Springμ μ¬μ© λͺ©μ
1. μν°νλΌμ΄μ¦ κ°λ°μ 볡μ‘ν¨
- κΈ°μ μ μΈ μ μ½μ‘°κ±΄κ³Ό μꡬμ¬νμ΄ λμ΄λ¨, λΉμ¦λμ€ λ‘μ§ λ³΅μ‘λ μ¦κ°
2. 볡μ‘ν¨μ ν΄κ²°νλ €λ λ Έλ ₯
- μ κ±° λ μ μλ κ·Όλ³Έμ μΈ λ³΅μ‘ν¨, μ€ν¨ν ν΄κ²°μ± EJB
3. 볡μ‘ν¨μ μλνλ μ€νλ§ μ λ΅
- μλΉμ€ μΆμν, AOP, κ°μ²΄μ§ν₯, DI
4. κΈ°μ μ μΌλ‘ μ€νλ§μ΄ μ§ν₯νλ λͺ©μ μ μ
- POJOλ₯Ό μ΄μ©ν κ°λ³κ³ λΉμΉ¨ν¬μ κ°λ°
- POJO (Plain Old Java Object) - μ€λλ λ°©μμ μλ° μ€νμ νΈ
Java EE λ±μ μ€λ νλ μμν¬λ€μ μ¬μ©νκ² λλ©΄μ ν΄λΉ νλ μμν¬μ μ’ μλ λ¬΄κ±°μ΄ κ°μ²΄λ₯Ό λ§λ€κ² λ κ²μ λ°λ°ν΄μ μ¬μ©νκ² λ μΈμ΄
- μ§μ ν POJOλ κ°μ²΄μ§ν₯μ μΈ μ리μ μΆ©μ€νλ©΄μ, νκ²½κ³Ό κΈ°μ μ μ’
μλμ§ μκ³ νμμ λ°λΌ μ¬νμ©λ μ μλ λ°©μμΌλ‘ μ€κ³λ μ€λΈμ νΈλ₯Ό λ§νλ€.
μ€νλ§ νλ μμν¬λ POJO λ°©μμ νλ μμν¬ μ΄λ€.
Spring μΈν λ°©λ²
1. κ΅¬κΈ sts κ²μ
2. Spring Tools 4 for Eclipse - 4.10.0 μλμ° 64bit λ²μ Ό λ€μ΄λ‘λ
3. jar νμΌ μ€ν
4. μ€νλ§ μ€ν
5. μ€μ ν μ΄ν΄λ¦½μ€ λ§μΌ μ€ν
6. spring legacy κ²μ ν Spring Tools 3 Add-on for spring tools 4 3.9.16 μ€μΉ
7. help - install new software - work with - latest Eclipse Release μ ν
맨 μλ web,xml, java EE and OSGi ~~ μ ν μ€μΉ
8. web utf-8 μ€μ
Maven
- μλ° νλ‘μ νΈμ λΉλ(Build)λ₯Ό μλν ν΄μ£Όλ λΉλ ν΄
Spring κΈ°λ³Έ μ μ
1. Dynamic Web Project μμ±
2. νλ‘μ νΈ μ€λ₯Έμͺ½ λλ₯΄κ³ λ©μ΄λΈ μ€μ
- λ©μ΄λΈ μ€μ
1. κ΅¬κΈ mavenrepository κ²μ
2. spring mvc κ²μ
3. λ²μ Ό ν΄λ¦ (5.3.3)
4. Maven λμ μλ νκ·Έ 볡μ¬
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.3</version>
</dependency>
5. log4j μ slf4jλ κ²μ νμ¬ μΆκ° (Java Logging System)
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>
6. μ€νλ§ νλ‘μ νΈ νμΌ μ€λ₯Έμͺ½ λ§μ°μ€ - Configure - Convert to Maven Project λλ¬ pom.wml μμ±
7. <project> νκ·Έ μμ <dependencies> νκ·Έ μμ± ν μμ μ νκ·Έ λΆμ¬λ£κΈ°
3. WEB-INF μμ ν΄λ spring, views μμ±
4. spring ν΄λ μμ Spring bean configuration file μμ±
5. μμ±λ xml νμΌμμ νλ¨ Namespaces ν΄λ¦
6. context 체ν¬
7. <beans>νκ·Έ μμ μλ νκ·Έλ€ μΆκ°
<!--spring MVS annotation(μ£Όμλ¬Έ, μ§μλ¬Έ)μ μ¬μ©νκΈ° μν μ€μ -->
<context:annotation-config/>
<!-- view μ€μ -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/> <!-- viewμ κ²½λ‘ -->
<property name="suffix" value=".jsp"/> <!-- νμ₯μλͺ
-->
</bean>
<!-- Java κ³΅ν΅ ν¨ν€μ§ μ€μ -->
<context:component-scan base-package="bit.com.a"/>
8. νλ‘μ νΈ λ§μ°μ€ μ€λ₯Έμͺ½ - Java EE Tools - Generateλ‘ web.xml μμ±
9. νλ¨κ³Ό κ°μ΄ μΈν
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/dispatcherServlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern> <!-- .do λ‘ λλλ λ§ν¬λ§ controller μ΄λ -->
</servlet-mapping>
<!-- filter -->
<!-- νκΈμ€μ -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern> <!-- '/*' λͺ¨λ κ²½λ‘μ μ²΄ν¬ -->
</filter-mapping>
10. src/main/java μ 컨νΈλ‘€λ¬ μμ±( ν¨ν€μ§λͺ μ xxx.xxx.xx μ΄λ° μ)
Spring Frameworkμ Controller
- μ΄λ Έν μ΄μ μ μ¬μ©νμ¬ μ»¨νΈλ‘€λ¬ μ μΈ
@Controller
- μ΄λ Έν μ΄μ μ μ΄μ©ν Mapping
@RequestMapping(value = "home.do", method = RequestMethod.GET)
- Loggerλ₯Ό ν΅ν΄ μ½μ μΆλ ₯
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Controller
public class MyController {
private static Logger logger = LoggerFactory.getLogger(MyController.class);
@RequestMapping("myPage")
public String myPage() {
//System.out.println("MyController myPage λ€μ΄μ΄");
logger.info("MyController myPage() λ€μ΄μ¨ μκ° : " + new Date());
return "myPage";
}
}
- Modelμ ν΅ν addAttribute()
import org.springframework.ui.Model;
@RequestMapping(value = "home.do", method = RequestMethod.GET)
public String home(Model model) {
//System.out.println("MyController newFile λ€μ΄μ΄");
logger.info("MyController home() λ€μ΄μ¨ μκ° : " + new Date());
//setAttribute() μν
String name = "νκΈΈλ";
model.addAttribute("_name", name);
return "home"; // forward νμ
}
- Parameter μλ ν λ³ν
@RequestMapping(value = "world.do", method = RequestMethod.GET)
public String world(String name, int age) { //μ€νλ§ νλ μμ€μ΄ Stringμ intλ‘ μλ λ³νν΄μ€
logger.info("MyController world() λ€μ΄μ¨ μκ° : " + new Date());
System.out.println("name : " + name + " age : " + age);
return "home";
}
- λμ΄μ¨ Parameter κ° μλ Dto μ½μ
β» λ¨ Parameter μ΄λ¦κ³Ό Dto μ΄λ¦μ΄ κ°μμΌ νλ€.
@RequestMapping(value = "world.do", method = RequestMethod.GET)
public String world(Human ha) { //μ€νλ§ νλ μμ€μ΄ dtoμ μμμ μ½μ
ν΄μ€λ€.
logger.info("MyController world() λ€μ΄μ¨ μκ° : " + new Date());
System.out.println(ha.toString());
return "home";
}
'π» κ°λ° > π TIL (Today I Learned)' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
21.04.14 TIL (D+65 Spring, ajax) (0) | 2021.04.14 |
---|---|
21.04.13 TIL (D+64 MyBatis, Spring) (0) | 2021.04.13 |
210323 TIL(JSP EL νκ·Έ, JSTL νκ·Έ, JSP Action νκ·Έ) (0) | 2021.03.23 |
21.03.12 TIL(D+42 μλΈλ¦Ώ Cookie and Session, JSP, Ajax) (0) | 2021.03.12 |
21.03.10(D+40 μλΈλ¦Ώ(Servlet) , WAS) (0) | 2021.03.10 |