xml 파일로 빈을 등록할 수 있다.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="userRepository" class="nextstep.helloworld.config.xmlConfig.UserRepository"/> <bean id="userService" class="nextstep.helloworld.config.xmlConfig.UserService"> <property name="userRepository" ref="userRepository"/> </bean> </beans>
<bean> 태그
빈 객체를 만들 때 사용
<bean id="userRepository" class="nextstep.helloworld.config.xmlConfig.UserRepository"/>
id 속성: 해당 빈 객체를 식별하기 위한 고유 식별자
class 속성: 해당 빈 객체의 클래스 경로

<property> 태그
해당 빈 객체의 프로퍼티를 설정하는 데 사용
<property name="userRepository" ref="userRepository"/>
name 속성: 해당 클래스 내에 선언된 맴버 변수 중, 의존성 주입을 할 변수 이름
ref 속성: 의존하는 빈의 id
public class UserService { private UserRepository userRepository; public void setUserRepository(UserRepository userRepository) { this.userRepository = userRepository; } public String sayHello() { return "hello"; } public String sayHi() { return userRepository.sayHi(); } }
'프로그래밍 > JAVA Spring' 카테고리의 다른 글
[Spring 스프링] 외부 파일을 이용한 Configuration 그런데 이제 @PropertySource와 @Value를 곁들인 (0) | 2023.05.15 |
---|---|
[Spring 스프링] Java기반 Configuration (0) | 2023.05.15 |
[Spring 스프링] HandlerMethodArgumentResolver 알아보기 (0) | 2023.05.08 |
[Spring 스프링] HandlerInterceptor 알아보기 (0) | 2023.05.07 |
[Spring 스프링] HandlerInterceptorAdapter 말고 HandlerInterceptor (0) | 2023.05.07 |