https://velog.io/@yoonee1126/Day68 보충
<property name="setter메서드이름" value="값"/>
<property name="setter메서드이름" ref="빈아이디"/>
public class SampleController{
String[] urls; // 배열
Set<MessageService> messageService; // Set
List<ExceptionHanlder> exceptionHandlers; // List
// setter생략
}
<bean id="sampleController" class="com.sample.SampleController">
<!-- String[] urls -->
<property name="urls">
<array>
<value>http://www.naver.com</value>
<value>http://www.daum.net</value>
</array>
</property>
<!-- Set<MessageService> -->
<property name="messageService">
<set>
<ref bean="smsMessageService"/>
<ref bean="emailMessageService"/>
</set>
</property>
<!-- List<ExceptionHandler> -->
<property name="exceptionHandler">
<list>
<ref bean="runtimeExceptionHandler"/>
<ref bean="DataAccessExceptionHandler"/>
</list>
</property>
</bean>
public class SampleController{
Map<String, MessageService> messageServices;
public void setMessageServices(Map<String, MessageService> messageService){
this.messageServices = messageServices;
}
public void execute(String key, String message){
MessageService service = messageServices.get(key);
service.send(message);
}
}
<bean id="sampleController" class="com.sample.SampleController">
<property name="messageServices>
<map>
<entry key="sms" value-ref="smsMessageServiceImple" />
<entry key="email" value-ref="emailMessageServiceImple" />
<entry key="app" value-ref="appPushMessageServiceImple" />
</map>
</property>
</bean>
<?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">
...
</beans>
1. Service - 업무로직이 구현된 곳
public interface ProductService {
Product getProductByNo(int no);
}
2. Controller
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
public String execute(int userNo) {
User user = userService.getUserByNo(userNo);
System.out.println("사용자번호: "+user.getNo());
System.out.println("사용자명: "+user.getName());
System.out.println("사용자이메일: "+user.getEmail());
System.out.println("사용자비밀번호: "+user.getPassword());
return "detail.jsp";
}
<?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">
1.
<bean id="userServiceImpl" class="com.sample.service.UserServiceImpl"/>
2.
<bean id="userController" class="com.sample.controller.UserController">
<property name="userService" ref="userServiceImpl" />
</bean>
</beans>
<bean id="userServiceImpl" class="com.sample.service.UserServiceImpl"/>
UserServiceImpl userServiceImpl = new UserServiceImpl();
<bean id="userController" class="com.sample.controller.UserController">
<property name="userService" ref="userServiceImpl" />
</bean>
UserController userController = new UserController();
userController.setUserService(userServiceImpl());
xmlns:p = "http://www.springframework.org/schema/p"
<bean id="userController" class="com.sample.controller.UserController">
<property name="userService" ref="userServiceImpl" />
</bean>
아래처럼 변경
<bean id="userController" class="com.sample.controller.UserController"
p:userService-ref="userServiceImpl"/>
1. Service
public class ProductServiceImpl {
public Product getProductByNo(int no) {
return new Product(100,"애플워치","애플",650000,610000);
}
}
2. Controller
public class SampleConstructorController {
private ProductServiceImpl productService;
public SampleConstructorController(ProductServiceImpl productService) {
this.productService = productService;
}
public String execute() {
Product product = productService.getProductByNo(100);
User user = userService.getUserByNo(100);
System.out.println(product);
System.out.println(user);
return null;
}
}
<?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="productServiceImpl" class="com.sample.service.ProductServiceImpl"/>
<bean id="sampleController" class="com.sample.controller.SampleConstructorController">
<constructor-arg name="productService" ref="productServiceImpl"/>
</bean>
</bean>
<bean id="productServiceImpl" class="com.sample.service.ProductServiceImpl"/>
ProductServiceImpl productServiceImpl = new ProductServiceImpl();
<bean id="sampleController" class="com.sample.controller.SampleConstructorController">
<constructor-arg name="productService" ref="productServiceImpl"/>
</bean>
SampleConstructorController sampleController
= new SampleConstructorController(productServiceImpl());
<bean id="sampleController" class="com.sample.controller.SampleConstructorController">
<constructor-arg name="productService" ref="productServiceImpl"/>
</bean>
아래처럼 변경
<bean id="sampleController" class="com.sample.controller.SampleConstructorController" c:productService-ref="productServiceImpl">
@Configuration
public class Config{
@Bean
public Engine{}
@Bean
public Door{}
}
@Component
public class Car{
...
}
<context:component-scan base-package="경로"/>
<context:component-scan base-package="경로">
<context:exclude-filter type="annotation"
expression="경로.제외할어노테이션"/>
<context:include-filter type="regex"
expression="스캔에포함할경로"/>
</context:component-scan>
@Configuration
@ComponentScan
public class ApplicationConfig{}