- 빈 팩토리 등록 방법
오브젝트를 생성하고 초기화해준다 => @bean
@configuration으로 등록한
/**
* 팩토리 클래스 UserDao을 생성 책임을 맡았다.
*/
@Configuration // p 96 빈 팩토리를 위한 오브젝트 설정을 담당하는 클래스라고 인식.
public class DaoFactory {
@Bean
public DataSource dataSource() {
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
dataSource.setDriverClass(com.mysql.jdbc.Driver.class);
dataSource.setUrl(DbConstant.JDBC_URL.getStr());
dataSource.setUsername(DbConstant.ID.getStr());
dataSource.setPassword(DbConstant.PASSWORD.getStr());
return dataSource;
}
@Bean
public UserDao userDao() {
UserDao userDao = new UserDao();
userDao.setDataSource(dataSource());
return userDao;
}
}
- xml 설정방법
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/springbook?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="qkrtmdcks1!"/>
</bean>
<bean id="userDao" class="springbook.user.dao.UserDao">
<property name="dataSource" ref="dataSource"/>
</bean>
GenericXmlApplicationContext context =
new GenericXmlApplicationContext("xml/applicationContext.xml");
UserDao dao = context.getBean("userDao", UserDao.class);
User user = new User("dmdrk1414", "박승찬", "1234");
new GenericXmlApplicationContext("xml/applicationContext.xml");
UserDao dao = context.getBean("userDao", UserDao.class);
User user = new User("dmdrk1414", "박승찬", "1234");