application.yml 과 application.properties
application.yml 과 application properties 둘다 서버 정보나
스프링부트 외적인 시스템과 연동할때 필요한 profile 정보를 정의하고
프로그램이 실행되는데 필요한 속성들을 정의할때 application.yml 또는 application.properties를 사용한다.


application.yml

.yml 파일은 .properties 파일과 다르게 계층적 구조를 사용 할 수 있다. 아래 내용을 보면 이해가 편할거다.

#DB
spring:
	datasource:
		driver-class-name: oracle.jdbc.driver.OracleDriver
		url: jdbc:oracle:thin:@localhost:8080:orcl
		username: username
		password: password


위 설정을 살펴보면,
db 세팅을 할때 datasource 라는 공통 구조는 한번만 작성하고 하위에 속하는 drvier-class-name, url, password 등의 구조를 선언하여 작성 할 수 있다.


.yml 파일에는 여러개의 profile을 정리하여 작성할 수 있다.


application.properties

.properties 파일은 key = value 형식으로 서술되며 문자열만 사용이 가능하다.
이 또한 아래 예시로 된 설정 파일을 살펴보자

#DB
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:8080:orcl
spring.datasource.username=username
spring.datasource.password=password


.yml에서 설정한 DB 정보를 그대로 .properties 에 설정한 내용이다.

하나의 .properties 파일은 하나의 profile만 가지고 있어야 하며 여러개의 properties 파일을 생성하여 사용할 수 있다.

application.properties 와 application.yml 파일에서 우선순위는 .properties 파일이 우선순위로 .yml 파일에서 설정한 내용이 덮어씌워질 수 있는 점은 주의하자.


이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.

반응형

'프로그래밍 > Java' 카테고리의 다른 글

[Spring] 스프링 AOP  (0) 2024.02.21

LifeCycle Hooks에는 크게 Creation(생성), Mounting(장착), Updating(변경), Destruction(소멸) 이렇게 4단계가 있으며 이번에는 장착 단계에서도 onMounted를 중점으로 Mounting(장착) 단계에 대해 알아보려한다.

 

 

Mounting(장착) 단계는 DOM에 컴포넌트를 삽입하는 단계이며, onBeforeMount와 onMounted가 있다.

  • 서버 렌더링에서 지원하지 않는다.
  • 초기 렌더링 직전에 돔을 변경하고자 한다면 이 단계에서 활용할 수 있다.

onBeforeMount

: 컴포넌트가 마운트되기 직전에 호출된다

 

onMounted

: 컴포넌트가 마운트된 후에 호출되며 DOM에 접근할 수 있는 단계이다. 또한, 모든 자식 컴포넌트가 마운트된 상태를 의미한다.

 

위에서 정의내린 onBeforeMount와 onMounted 단계를 아래 코드를 통해 차이를 알아보면

onMounted는 컴포넌트가 mounted 된 이후이기 때문에 접근이 가능하지만 beforeMount 에서는 mount가 완료되지 않아 null 값이 출력 되는 것을 볼 수 있다.

 

<template>
    <div>
        <input ref="inputRef" type="text">
    </div>
</template>

<script>
import { onBeforeMount, onMounted, ref } from 'vue';

export default {
    setup() {
        console.log('setup')
        const inputRef = ref(null)
        onBeforeMount(() => {
            console.log('onBeforeMount', inputRef.value);
        });
        onMounted(() => {
            console.log('onMounted', inputRef.value);
        });
        return { inputRef }
    }
}
</script>


추가적으로, 자식 컴포넌트를 하나 추가하여 자식 컴포넌트에서 Mounting 순서를 같이 출력해보면 부모 컴포넌트에서 먼저 setup 과 onBeforeMount 작업이 실행되고 자식 컴포넌트에서 mounted가 마무리 된 이후에 부모 컴포넌트 mounted 작업이 완료되는 것을 볼 수 있다.

 

Child Components

<template>
    <div>

    </div>
</template>

<script>
import { onBeforeMount, onMounted } from 'vue'
export default {
    setup() {
        console.log('[Child] setup')
        onBeforeMount(() => {
            console.log('[Child] onBeforeMount')
        })
        onMounted(() => {
            console.log('[Child] onMounted')
        })

        return {}
    }
}
</script>

 

Parent Components

<template>
    <div>
        <input ref="inputRef" type="text">
        <LifeCycleChildVue></LifeCycleChildVue>
    </div>
</template>

<script>
import { onBeforeMount, onMounted, ref } from 'vue';
import LifeCycleChildVue from './components/LifeCycleChild.vue';

export default {
    components: {
        LifeCycleChildVue,
    },
    setup() {
        console.log('setup')
        const inputRef = ref(null)
        onBeforeMount(() => {
            console.log('onBeforeMount', inputRef.value);
        });
        onMounted(() => {
            console.log('onMounted', inputRef.value);
        });
        return { inputRef }
    }
}
</script>

<style lang="scss" scoped></style>

 

부모/자식 컴포넌트 Mounting 순서 출력 결과

 

 

이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.

 

 

반응형

'프로그래밍 > Vue' 카테고리의 다른 글

[Vue3] Computed  (0) 2024.04.26
Vue.js 2.0 라이프 사이클  (0) 2024.02.21
[Vue.js] Props 란  (0) 2022.06.30
구구단 게임  (0) 2020.07.13

 

🤔회사에서 Vue 개발을 하면서 computed를 사용하는 경우가 종종 있어 computed 에 대해 알아보려 한다.

 (학습을 위한 정리로 틀린 정보가 있다면 말씀 주시면 감사하겠습니다.)

 

Computed 사용 및 함수와 차이점

<template> 내 삼항 연산자처리

<template>
  <div>
    <h2>책 제목이 5글자 이상인 경우인가요?</h2>
    <h3>{{ book.title.length > 5 ? '예' : '아니오' }}</h3>
  </div>
</template>

<script>
import { reactive } from 'vue'
export default {
  setup() {
    const book = reactive({
      title: 'vue 학습 책입니다.',
      author: 'test'
    })
    return {
      book
    }
  }
}
</script>

 

위 코드를 보면 ‘책 제목이 5글자 이상’인 경우를 <template> 안에서 삼항 연산자를 통해 체크하여 ‘예’ / ‘아니오’ 로 출력해주고 있다. 하지만, 이와 같은 코드는 가독성이 떨어지기 때문에 computed 또는 함수를 사용하여 기능을 처리하는 것이 효율적인 코드이다.

 

computed 사용

<template>
  <div>
    <h2>책 제목이 5글자 이상인 경우인가요?</h2>
    <h3>{{ checkBookTitle}}</h3>
  </div>
</template>

<script>
import { computed, reactive } from 'vue'
export default {
  setup() {
    const book = reactive({
      title: 'vue 학습 책입니다.',
      author: 'test'
    })

    const checkBookTitle= computed(() => {
      return book.title.length > 5 ? '예' : '아니오'
    })

    return {
      book,
      checkBookTitle
    }
  }
}
</script>

 

method 사용

<template>
  <div>
    <h2>책 제목이 5글자 이상인 경우인가요?</h2>
    <h3>{{ checkBookTitle }}</h3>
    <h3>{{ methodBookTitle() }}</h3>
  </div>
</template>

<script>
import { computed, reactive } from 'vue'
export default {
  setup() {
    const book = reactive({
      title: 'vue 학습 책입니다.',
      author: 'test'
    })

    const checkBookTitle = computed(() => {
      return book.title.length > 5 ? '예' : '아니오'
    })

    const methodBookTitle = () => {
      return book.title.length > 5 ? '예' : '아니오'
    }

    return {
      book,
      checkBookTitle,
      methodBookTitle
    }
  }
}
</script>

 

위 코드와 같이 methodBookTitle() 함수와, checkBookTitle computed를 사용하여 함수명을 통해 어떤 것을 출력하고 싶은지 예측도 가능하게 된다.

함수와, computed 호출 결과 두 코드는 모두 ‘예’ 로 같은 결과를 보여준다.

그렇다면, 함수와 computed의 차이점을 확인해보면 computed는 cache 기능을 갖고 있다

 

<template>
  <div>
    <h2>책 제목이 5글자 이상인 경우인가요?</h2>
    <h3>{{ checkBookTitle }}</h3>
    <h3>{{ checkBookTitle }}</h3>
    <h3>{{ methodBookTitle() }}</h3>
    <h3>{{ methodBookTitle() }}</h3>
  </div>
</template>

<script>
import { computed, reactive } from 'vue'
export default {
  setup() {
    const book = reactive({
      title: 'vue 학습 책입니다.',
      author: 'test'
    })

    const checkBookTitle = computed(() => {
      console.log('computed')
      return book.title.length > 5 ? '예' : '아니오'
    })

    const methodBookTitle = () => {
      console.log('methods')
      return book.title.length > 5 ? '예' : '아니오'
    }

    return {
      book,
      checkBookTitle,
      methodBookTitle
    }
  }
}
</script>

 

위 코드에서 computed인 checkBookTitle 와 함수인 methodsBookTitle() 을 각각 2번씩 호출한 결과

console.log 가 어떻게 찍히는지 확인해보면, ‘computed’ 는 한 번 그리고 methods는 두번 출력된 것을 볼 수 있다.

computed는 내부 데이터 값이 변경되지 않을 경우 cache된 데이터를 가져온다.

Computed get, set

computed의 경우 기본적으로 readonly 속성을 갖고 있어 새로운 값을 할당하려 하면 readonly warning이 뜬다. 이를 해결하기 위해 getter, setter 를 같이 활용해서 사용한다.

아래 소스는 readonly인 computed 값을 변경하려 할때 나오는 warning 예제와 이를 해결하기 위해 사용된 getter / setter 코드이다.

 

<template>
  <div>
    <h2>책 제목이 5글자 이상인 경우인가요?</h2>
    <h3>{{ checkBookTitle }}</h3>
    <h3>{{ checkBookTitle }}</h3>
    <h3>{{ methodBookTitle() }}</h3>
    <h3>{{ methodBookTitle() }}</h3>
    <h2>author</h2>
    <h3>{{ authorName }}</h3>
  </div>
</template>

<script>
import { computed, reactive, ref } from 'vue'
export default {
  setup() {
    const book = reactive({
      title: 'vue 학습 책입니다.',
      author: 'test'
    })

    const checkBookTitle = computed(() => {
      console.log('computed')
      return book.title.length > 5 ? '예' : '아니오'
    })

    const methodBookTitle = () => {
      console.log('methods')
      return book.title.length > 5 ? '예' : '아니오'
    }

    const authorName = ref('락토')
    const changeName = computed(() => authorName.value + '핏')

    console.log(changeName)
    changeName.value = '지은 이'

    return {
      book,
      checkBookTitle,
      methodBookTitle,
      authorName,
      changeName
    }
  }
}
</script>

 

 

Computed getter / setter 사용하여 처리

<template>
  <div>
    <h2>책 제목이 5글자 이상인 경우인가요?</h2>
    <h3>{{ checkBookTitle }}</h3>
    <h3>{{ checkBookTitle }}</h3>
    <h3>{{ methodBookTitle() }}</h3>
    <h3>{{ methodBookTitle() }}</h3>
    <br />
    <h2>author</h2>
    <h3>{{ authorName }}</h3>
  </div>
</template>

<script>
import { computed, reactive, ref } from 'vue'
export default {
  setup() {
    const book = reactive({
      title: 'vue 학습 책입니다.',
      author: 'test'
    })

    const checkBookTitle = computed(() => {
      console.log('computed')
      return book.title.length > 5 ? '예' : '아니오'
    })

    const methodBookTitle = () => {
      console.log('methods')
      return book.title.length > 5 ? '예' : '아니오'
    }

    const authorName = ref('락토')
    const changeName = computed({
      get() {
        console.log('get')
        return authorName.value
      },
      set(value) {
        console.log('set')
        authorName.value = value
      }
    })

    console.log(changeName.value)
    changeName.value = '지은 이'

    return {
      book,
      checkBookTitle,
      methodBookTitle,
      authorName,
      changeName
    }
  }
}
</script>

 

※ Reference 📑
 Vue Computed 공식 문서 :  https://ko.vuejs.org/guide/essentials/computed

 

Vue.js

Vue.js - The Progressive JavaScript Framework

vuejs.org

 

이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.

반응형

'프로그래밍 > Vue' 카테고리의 다른 글

[Vue3] onMounted  (0) 2024.05.14
Vue.js 2.0 라이프 사이클  (0) 2024.02.21
[Vue.js] Props 란  (0) 2022.06.30
구구단 게임  (0) 2020.07.13

반응형

'알고리즘 > Baekjoon' 카테고리의 다른 글

[Baekjoon] #2839 설탕배달  (0) 2024.04.18
[Baekjoon] #4485 녹색 옷 입은 애가 젤다지?  (1) 2024.04.18
[Baekjoon] #4781 사탕 가게  (0) 2024.04.18
[Baekjoon] #5054 주차의 신  (0) 2024.04.18
[Baekjoon] #5567 결혼식  (0) 2024.04.18

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner sc= new Scanner(System.in);
		
		int n = sc.nextInt();
		
		if (n == 4 || n == 7) {
			System.out.println(-1);
		}
		else if (n % 5 == 0) {
			System.out.println(n / 5);
		}
		else if (n % 5 == 1 || n % 5 == 3) {
			System.out.println((n / 5) + 1);
		}
		else if (n % 5 == 2 || n % 5 == 4) {
			System.out.println((n / 5) + 2);
		}
		
	}

}
반응형

'알고리즘 > Baekjoon' 카테고리의 다른 글

[Baekjoon] #2805 나무 자르기  (0) 2024.04.18
[Baekjoon] #4485 녹색 옷 입은 애가 젤다지?  (1) 2024.04.18
[Baekjoon] #4781 사탕 가게  (0) 2024.04.18
[Baekjoon] #5054 주차의 신  (0) 2024.04.18
[Baekjoon] #5567 결혼식  (0) 2024.04.18

+ Recent posts