'Spring'에 해당되는 글 49건

  1. 2019.09.20 [Spring] Springboot + Thymeleaf 설정 시 주의.
  2. 2019.09.18 [SpringBoot] Project Starter
  3. 2019.09.16 [SpringBoot] @PropertySource 로 yaml 파일 읽기
  4. 2019.09.09 [Spring] Could not find method annotationProcessor()..
  5. 2018.05.02 [Spring Framework] SpringMVC + Gradle 템플릿 + Tomcat 연동
  6. 2017.07.11 [Spring] Spring @Autowired Annotation
  7. 2016.09.19 [Tomcat] Spring framework 사용 시 중복 실행 이슈
  8. 2014.02.12 [eclipse] tomcat 실행 시 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener 에러 1
  9. 2013.08.13 [Spring] properties 파일 읽기.
  10. 2013.08.13 [Spring] Controller Content-Type 설정. (ajax)

[Spring] Springboot + Thymeleaf 설정 시 주의.

ITWeb/개발일반 2019. 9. 20. 13:58

springboot 에서 thymeleaf 사용 시 applicaiton.properties 내 주의 사항 정도 입니다.

 

기본 설정은 아래와 같습니다.

 

spring.thymeleaf.cache=true
spring.thymeleaf.check-template=true
spring.thymeleaf.check-template-location=true
spring.thymeleaf.enabled=true
spring.thymeleaf.enable-spring-el-compiler=false
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.excluded-view-names=
spring.thymeleaf.mode=HTML
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.reactive.chunked-mode-view-names=
spring.thymeleaf.reactive.full-mode-view-names=
spring.thymeleaf.reactive.max-chunk-size=0B
spring.thymeleaf.reactive.media-types=
spring.thymeleaf.render-hidden-markers-before-checkboxes=false
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.servlet.produce-partial-output-while-processing=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.template-resolver-order=0
spring.thymeleaf.view-names=

 

이 설정 값들을 넣어 주지 않으셔도 기본 동작에는 문제가 없으나 설정 최적화와 이해를 위해서는 각각의 설정이 어떤 의미를 가지는지 아는게 중요 합니다.

 

viewResolver 관련해서 주의 점은!!

spring.thymeleaf.view-names# Comma-separated list of view names (patterns allowed) that can be resolved.

이 설정을 제거 하시거나 등록을 해주셔야 한다는 것입니다.

 

그냥 저렇게 empty value 로 놔두시면 viewResolver 오류가 발생을 하게 됩니다.

주석에 나와 있는 설명 처럼 목록으로 넣어 주시거나 패턴으로 잡아 주시면 되겠습니다.

:

[SpringBoot] Project Starter

ITWeb/개발일반 2019. 9. 18. 10:10

시작 하기에 앞서 한번 읽어 보고 하면 좋은 링크 스크랩.

 

https://start.spring.io
https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/
https://docs.gradle.org/current/userguide/java_library_plugin.html

 

저 문서들을 읽고 나니 왜 스프링부트가 대세가 되었는지 알겠내요.

:

[SpringBoot] @PropertySource 로 yaml 파일 읽기

ITWeb/개발일반 2019. 9. 16. 13:34

아래 문서에 보면 설명이 잘 나와 있습니다.

기록 하는 차원에서 남겨 봅니다.

 

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-yaml-shortcomings

 

24. Externalized Configuration

Getters and setters are usually mandatory, since binding is through standard Java Beans property descriptors, just like in Spring MVC. A setter may be omitted in the following cases:Maps, as long as they are initialized, need a getter but not necessarily a

docs.spring.io

1. If you use “Starters”, SnakeYAML is automatically provided by spring-boot-starter.
2. The YamlPropertySourceLoader class can be used to expose YAML as a PropertySource in the Spring Environment. Doing so lets you use the @Value annotation with placeholders syntax to access YAML properties.

 

SnakeYAML 이 기본 탑재

@Value 로 접근 가능

 

@PropertySource("classpath:/application.yml")
...
@Value("${spring.profiles.active}")
private String profile;

@PropertySource("classpath:/application.yml")
...
@Value("${spring.profiles.active}")
private String profile;

 

:

[Spring] Could not find method annotationProcessor()..

ITWeb/개발일반 2019. 9. 9. 18:23

Intellij 에서 lombok 설정 관련 오류가 난 케이스 입니다.

 

Could not find method annotationProcessor() 이 에러는 gradle 버전 4.6 이상을 사용 하시면 발생 하지 않게 됩니다.

저는 4.4 버전으로 기본 구성을 예전에 해 놓다 보니 이번에 오류가 발생을 했었내요. 

그래서 기록해 둡니다.

 

그 이외는 intellij 설정에서 enable annotation processing 설정을 해주시면 됩니다.

:

[Spring Framework] SpringMVC + Gradle 템플릿 + Tomcat 연동

ITWeb/개발일반 2018. 5. 2. 18:33

매니저 역할을 오래 하다 보면 개발 감각이 떨어 지는건 어쩔수 없는 일인 것 같습니다.

SpringMVC 를 이용한 기본 웹 개발도 이제는 거북이 수준이 되어 가고 있는 것 같아 잊지 않기 위해 기록해 봅니다.


요즘 워낙 좋은 Tool, IDE 들이 많이 나와 있고. Git 검색만 해봐도 찾던 소스가 너무나도 많이 나옵니다.

아래는 Intellij community 버전을 이용해서 웹프로젝트 템플릿을 생성하는 예제 입니다.


각 단계별 코드별 설명은 작성 하지 않았습니다.

이유는 여긴 제가 그냥 혼자 기록하고 공부하는 공간이고, 누구를 가르치기에 너무 많은 무림고수 들이 계셔서 어쭙잖은 설명은 그냥 아래 레퍼런스로 대신 합니다.


References)

https://github.com/HowookJeong/springmvc-gradle-template

https://tomcat.apache.org/tomcat-9.0-doc/config/context.html

https://docs.spring.io/spring/docs/4.3.16.RELEASE/spring-framework-reference/htmlsingle/#spring-web


Step 1)

Intellij 를 실행 하고 매뉴에서 아래와 같이 선택을 합니다.


File -> New -> Project -> Gradle -> Java -> Next


GroupId -> org.project.web

ArtifactId -> springmvc-gradle-template

Next


Step 2)

SpringMVC Framework 틀을 만들기 위해 필요한 폴더와 파일들을 아래와 같이 생성을 합니다.


2-1) Community 버전으로는 gradle webapp 추가가 안되기 때문에 별도로 생성 합니다.

Make webapp directory

src/main/webapp


2-2) Make WEB-INF directory

src/main/webapps/WEB-INF

web.xml

dispatcher-servlet.xml


2-3) jsp template 사용을 위해서 WEB-INF 아래 jsp 폴더를 생성 합니다.

Make jsp directory

src/main/webapps/WEB-INF/jsp


2-4) Resource 구성을 합니다.

Make resources directory

src/main/resources/beans

bo-bean.xml

dao-bean.xml

src/main/resources/context

applicationContext.xml

applicationContext-mybatis.xml

src/main/resources/mybatis

config.xml

jdbc.properties

src/main/resources/sql

example.xml

log4j.xml


2-5) Package 구성을 합니다.

Make packages

org.project.web.bo

org.project.web.controllor

org.project.web.dao

org.project.web.model


여기까지 하셨으면 기본 empty project 생성은 끝났습니다.

이제 필요한 코드와 설정을 해보도록 하겠습니다.


Step 3)

이제 필요한 코드들을 생성해 보도록 하겠습니다.


3-1) web.xml 설정하기



3-2) dispatcher-servlet.xml 설정하기



3-3) log4j.xml 설정하기



3-4) mybatis/config.xml 설정하기



3-5) mybatis/jdbc.properties 설정하기



3-6) build.gradle 설정하기



이제 application 구현을 위한 코드 생성을 해보겠습니다.


3-7) controller/ExampleController


3-8) bo/ExampleBO


3-9) bo/ExampleBOImpl


3-10) dao/ExampleDAO


3-11dao/ExampleDAOImpl


3-12) model/ExampleModel



코드 구현을 했으니 이제 resources 설정을 하도록 하겠습니다.


3-13) beans/bo-bean.xml


3-14) beans/dao-bean.xml


3-15) context/applicationContext.xml


3-16) context/applicationContext-mybatis.xml


3-17) sql/example.xml


화면 구성을 위한 데코레이션으로 jsp 코드를 작성해 보겠습니다.


3-18) WEB-INF/jsp/exampleView.jsp


:

[Spring] Spring @Autowired Annotation

ITWeb/개발일반 2017. 7. 11. 10:56

참고문서)

https://www.tutorialspoint.com/spring/spring_autowired_annotation.htm


@Autowired 를 사용하는 방법에 대해서 기술 되어 있습니다.


1. @Autowired on Setter Methods

2. @Autowired on Properties

3. @Autowired on Constructors


3가지 방법 모두 같은 내용이기 때문에 사용하기 제일 편하신 걸로 사용하시면 되겠습니다.

저는 습관적으로 그냥 2번으로 사용하는 것 같습니다.


누구라도 이해하기 쉬운 예를 들자면.


public class HelloWorldModel {

...

}


-----------------------------------------------

// HelloWorldModel 이라는 클래스가 있다고 가정하고.


public class HelloWorldApp {

  private HelloWorldModel helloWorldModel;


  public HelloWorldApp() {

    this.helloWorldModel = new HelloWorldModel();

  }

...

}


// 위와 같이 선언한 부분이 아래와 같이 변경이 됩니다.


public class HelloWorldApp {

  @Autowired

  private HelloWorldModel helloWorldModel;

...

}


그냥 아는 것과 설명을 해줘야 할 때는 눈 높이를 맞춰야 하기 때문에 참 어렵내요.

:

[Tomcat] Spring framework 사용 시 중복 실행 이슈

ITWeb/개발일반 2016. 9. 19. 11:08

tomcat + spring 을 사용하면서 startup.sh 를 이용해서 실행 후 로그를 확인해 보면 두 번 실행(로딩) 되는 모습을 확인 할 때가 있습니다.


이렇게 두 번 실행이 될 경우 예상하지 못했던 문제를 가끔 경험 하게 되는데요.

아래와 같이 tomcat 의 server.xml 수정을 통해서 한 번만 실행 되도록 처리 할 수 있습니다.


기존 두 번 실행 되던 server.xml)

    <Engine name="Catalina" defaultHost="localhost">

      <Host name="localhost"  appBase="webapps"

            unpackWARs="false" autoDeploy="false">

        <Context docBase="web-service" path="/" reloadable="false" />

      </Host>

    </Engine>



수정된 server.xml)

    <Engine name="Catalina" defaultHost="localhost">

      <Host name="localhost"  appBase="webapps/web-service"

            unpackWARs="false" autoDeploy="false">

        <Context docBase="" path="/" reloadable="false" />

      </Host>

    </Engine>


그냥 별건 아니지만 scheduler 같은 컴포넌트를 사용하시는 경우 중복 실행이 될 수 있기 때문에 공유해 봅니다.

:

[eclipse] tomcat 실행 시 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener 에러

ITWeb/개발일반 2014. 2. 12. 11:14

eclipse 에서 spring mvc 프로젝트 생성하고 tomcat 등록 후 바로 실행 하면 아래와 같은 에러가 발생 할 때가 있습니다.

뭐 다들 아시는 내용이겠지만 기억력을 돕기 위해 기록을 남겨 보도록 하겠습니다.


1. 생성한 프로젝트의 properties  창을엽니다. ( alt + f + enter )

2. Deployment Assembly 를 선택 합니다.

3. 오른쪽 위 Add 버튼을 클릭 합니다.

4. Java Build Path Entries  선택 후 Next 버튼 클릭 합니다.

5. Maven Dependencies  선택 후 Finish 버튼 클릭 합니다.

6. Properties 창 오른쪽 하단의 Apply 버튼 클릭 합니다.

7. Properties 창 오른쪽 하단의 OK 버튼 클릭 후 tomcat 재실행을 해봅니다.



이렇게 하시면 정상적으로 동작 하게 됩니다.

뭐 원인은 빤하죠.

tomcat 에 해당 spring mvc 실행을 위한 class 가 포함이 안되서 그런 것이니 이와 같이 설정만 해주면 끝....


:

[Spring] properties 파일 읽기.

ITWeb/개발일반 2013. 8. 13. 18:35

해당 프로젝트에 conf 디렉토리 하나 만드시고 거기에 profile 별 디렉토리도 만드시고.. 

아래 같이 해주면 됩니다. (뭐 나만 알아 볼라고 대충 등록함.)



[pom.xml]

<resource>

<directory>${basedir}/conf/${env}</directory>

<targetPath>${project.build.directory}/conf</targetPath>

<filtering>false</filtering>

</resource>


[class]

resource = new ClassPathResource(propsFile);

props = PropertiesLoaderUtils.loadProperties(resource);


clusterName = props.getProperty("cluster.name");


[properties]

cluster.name=es-cluster

cluster.node.list=192.168.56.104:9300,192.168.56.104:9301


:

[Spring] Controller Content-Type 설정. (ajax)

ITWeb/개발일반 2013. 8. 13. 18:23

그냥 개발 하다 자꾸 까먹는 것들... 정리 차원에서.. ^^;

- spring controller 클래스에서 호출 method 에 설정

@RequestMapping(value = "/api", method = RequestMethod.POST, produces = "application/json; charset=utf-8")


- serve.xml 에서 URIEncoding 설정

http connector 에 설정

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>

: