if (str.length() == 0) return true;
str = str.toUpperCase();
// str을 char형 배열로 만들어줌.
char[] arr = str.toCharArray();
// 향상된 for문
for (char ch : arr) {
if (str.indexOf(ch) != str.lastIndexOf(ch)) {
return false;
}
}
return true;
ApplicationContext
(인터페이스로 구현되어져있음)
- 스프링 컨테이너는 Configuration Metadata를 사용
- 파라미터로 넘어온 설정 클래스 정보를 사용해서 스프링 빈 등록
- new AnnotationConfigApplicationContext(구성정보.class)로 스프링에 있는 @Bean의 메서드 등록
- 애너테이션 기반 컨테이너 구성 / XML기반
//Spring Container 생성 ApplicationContext applicationContext = new AnnotationConfigApplicationContext(DependencyConfig.class);
- BeanFactory
스프링 컨테이너의 최상위 인터페이스
빈을 등록하고 생성 조회 등 관리 역할
getBean() 메서드를 통해 빈을 인스턴스화 할 수 있다.
@Bean이 붙은 메서드의 명을 스프링 빈의 이름으로 사용해 빈 등록을 함.- ApplicationContext
BeanFactory의 기능을 상속받아 제공
빈을 관리하고 검색하는 기능을 BeanFactory가 제공하고 그 외 부가기능 제공
- 부가기능
MessageSource
: 메세지 다국화를 위한 인터페이스
EnvironmentCapable
: 개발, 운영 등 환경변수 등으로 나눠 처리하고 애플리케이션 구동 시 필요한 정보들을 관리
ApplicationEvenPublisher
: 이벤트 관련 기능을 제공하는 인터페이스
ResourceLoader
: 파일, 클래스패스, 외부 등 리소스를 편리하게 조회
- bean 접근 방법
ApplicationContext 사용해서 bean 정의를 읽고 액세스 할 수 있다.// create and configure beans ApplicationContext context= new ClassPathXmlApplicationContext("service.xml", "daos.xml"); //retrive configured instance PetStoreService service = context.getBean("memberRepository", memberRepository.class); //use configured instance List<String> userList = service.getUsernameList();
실제로는 getBean()메서드로 호출하여 사용 x
클래스의 인스턴스가 딱 1개만 생성되는 것을 보장하는 디자인 패턴
단일 인스턴스는 싱글톤 빈의 캐시에 저장
이름이 정해진 빈에 대한 모든 요청과 참조는 캐시된 개체 반환
싱글톤 패턴의 문제를 싱글톤 컨테이너가 해결
// DependencyConfig 클래스
컨텍스트를 인스턴스화 할 때
@Configuration
public class DependencyConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}
<bean />
에서 제공하는 일부 속성 지원해당 객체가 bean definitions의 소스임을 나타내는 애너테이션
빈 사이에 의존성 주입
@Configuration
public class DependencyConfig {
@Bean
public BeanOne beanOne() {
return new BeanOne(beanTwo());
}
@Bean
public BeanTwo beanTwo() {
return new BeanTwo();
}
}
@Import
애너테이션