Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
[ch07] Replace XML application context using @configuration class.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 authored and Dongho Sim committed Oct 28, 2017
1 parent e9b5636 commit 99a6aca
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 140 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<cglib.version>3.1</cglib.version>
<junit.version>4.12</junit.version>
<hsqldb.version>2.3.5</hsqldb.version>
<h2.version>1.4.196</h2.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -101,6 +102,12 @@
<version>${hsqldb.version}</version>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>

</dependencies>

<build>
Expand Down
128 changes: 0 additions & 128 deletions src/main/resources/applicationContext.xml

This file was deleted.

2 changes: 1 addition & 1 deletion src/test/java/ch01/springbook/user/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static class TestUserServiceException extends RuntimeException {

}

static class TestUserService extends UserServiceImpl {
public static class TestUserService extends UserServiceImpl {
private String id = "test04";

private TestUserService(String id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

Expand All @@ -25,10 +27,4 @@ public void getMessageFromFactoryBeanTest() {
assertThat(Message.class.isInstance(message), is(true));
assertThat(((Message)message).getText(), is("Factory Bean"));
}

@Test
public void getFactoryBeanTest() {
Object factory = applicationContext.getBean("&message");
assertThat(MessageFactoryBean.class.isInstance(factory), is(true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.junit.Before;
import org.junit.Test;

import ch07.springbook.sql.registry.ConcurrentHashMapSqlRegistry;
import ch07.springbook.sql.registry.SqlNotFoundException;
import ch07.springbook.sql.registry.SqlUpdateFailureException;
import ch07.springbook.sql.registry.UpdatableSqlRegistry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class EmbeddedDbSqlRegistryTest extends AbstractUpdatableSqlRegistryTest

@Override
protected UpdatableSqlRegistry createUpdatableSqlRegistry() {
db = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
db = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
.addScript("classpath:sql/embedded/schema.sql")
.build();

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/ch07/springbook/EmbeddedDbTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class EmbeddedDbTest {
@Before
public void setUp() {
db = new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.HSQL)
.setType(EmbeddedDatabaseType.H2)
.addScript("classpath:sql/embedded/schema.sql")
.addScript("classpath:sql/embedded/data.sql")
.build();
Expand Down
105 changes: 103 additions & 2 deletions src/test/java/test/TestApplicationContext.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,110 @@
package test;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.oxm.Unmarshaller;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.mysql.jdbc.Driver;

import ch01.springbook.user.UserService;
import ch01.springbook.user.UserServiceImpl;
import ch01.springbook.user.UserServiceTest;
import ch01.springbook.user.dao.UserDao;
import ch01.springbook.user.dao.UserDaoJdbc;
import ch06.springbook.factorybean.Message;
import ch06.springbook.factorybean.MessageFactoryBean;
import ch07.springbook.sql.OxmSqlService;
import ch07.springbook.sql.SqlService;
import ch07.springbook.sql.registry.EmbeddedDbSqlRegistry;
import ch07.springbook.sql.registry.SqlRegistry;

@Configuration
@ImportResource(locations = "/applicationContext.xml")
@EnableTransactionManagement
public class TestApplicationContext {

@Bean
public DataSource dataSource() {
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
dataSource.setDriverClass(Driver.class);
dataSource.setUrl("jdbc:mysql://localhost/tobystudy?useUnicode=true&amp;characterEncoding=UTF-8&amp;useSSL=true&amp;verifyServerCertificate=false");
dataSource.setUsername("study");
dataSource.setPassword("study");

return dataSource;
}

@Bean
public PlatformTransactionManager transactionManager() {
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(dataSource());
return transactionManager;
}

@Bean
public UserDao userDao() {
UserDaoJdbc userDaoJdbc = new UserDaoJdbc();
userDaoJdbc.setDataSource(dataSource());
userDaoJdbc.setSqlService(sqlService());
return userDaoJdbc;
}

@Bean
public UserService userService() {
UserServiceImpl userService = new UserServiceImpl();
userService.setUserDao(userDao());
return userService;
}

@Bean
public UserService testUserService() {
UserServiceTest.TestUserService testUserService = new UserServiceTest.TestUserService();
testUserService.setUserDao(userDao());
return testUserService;
}

@Bean
public SqlService sqlService() {
OxmSqlService oxmSqlService = new OxmSqlService();
oxmSqlService.setUnmarshaller(unmarshaller());
oxmSqlService.setSqlRegistry(sqlRegistry());
return oxmSqlService;
}

@Bean
public Unmarshaller unmarshaller() {
Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
jaxb2Marshaller.setContextPath("ch07.springbook.sql.reader.jaxb");
return jaxb2Marshaller;
}

@Bean
public SqlRegistry sqlRegistry() {
EmbeddedDbSqlRegistry embeddedDbSqlRegistry = new EmbeddedDbSqlRegistry();
embeddedDbSqlRegistry.setDataSource(embeddedDatabase());
return embeddedDbSqlRegistry;
}

@Bean
public DataSource embeddedDatabase() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
.addScript("classpath:sql/embedded/schema.sql")
.build();
}

@Bean
public Message message() throws Exception {
MessageFactoryBean messageFactoryBean = new MessageFactoryBean();
messageFactoryBean.setText("Factory Bean");

return messageFactoryBean.getObject();
}
}

0 comments on commit 99a6aca

Please sign in to comment.