This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ch07] Replace XML application context using @configuration class.
- Loading branch information
Showing
8 changed files
with
115 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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&characterEncoding=UTF-8&useSSL=true&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(); | ||
} | ||
} |