|
| 1 | +package modisefileupload.java.config; |
| 2 | + |
| 3 | +import java.util.Properties; |
| 4 | + |
| 5 | +import javax.persistence.EntityManagerFactory; |
| 6 | +import javax.sql.DataSource; |
| 7 | + |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.context.annotation.Bean; |
| 10 | +import org.springframework.context.annotation.Configuration; |
| 11 | +import org.springframework.core.env.Environment; |
| 12 | +import org.springframework.core.io.ClassPathResource; |
| 13 | +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; |
| 14 | +import org.springframework.jdbc.datasource.DriverManagerDataSource; |
| 15 | +import org.springframework.jdbc.datasource.init.DatabasePopulator; |
| 16 | +import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils; |
| 17 | +import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; |
| 18 | +import org.springframework.orm.jpa.JpaTransactionManager; |
| 19 | +import org.springframework.orm.jpa.JpaVendorAdapter; |
| 20 | +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; |
| 21 | +import org.springframework.orm.jpa.vendor.Database; |
| 22 | +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; |
| 23 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 24 | +import org.springframework.web.multipart.MultipartResolver; |
| 25 | +import org.springframework.web.multipart.commons.CommonsMultipartResolver; |
| 26 | + |
| 27 | +import modisefileupload.java.service.UserService; |
| 28 | +import modisefileupload.java.service.impl.UserServiceImpl; |
| 29 | + |
| 30 | +@EnableJpaRepositories(basePackages={"modisefileupload.java.dao"}) |
| 31 | +@EnableTransactionManagement |
| 32 | +@Configuration |
| 33 | +public class ApplicationContext { |
| 34 | + |
| 35 | + @Autowired |
| 36 | + private Environment environment; |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + /** |
| 41 | + * setting up the required Beans |
| 42 | + */ |
| 43 | + |
| 44 | + @Bean |
| 45 | + public UserService userService(){ |
| 46 | + return new UserServiceImpl(); |
| 47 | + } |
| 48 | + |
| 49 | + @Bean |
| 50 | + public DataSource dataSource(){ |
| 51 | + DriverManagerDataSource dataSource = new DriverManagerDataSource(); |
| 52 | + dataSource.setDriverClassName(environment.getProperty("jdbc.driverClass")); |
| 53 | + dataSource.setUrl(environment.getProperty("jdbc.url")); |
| 54 | + dataSource.setUsername(environment.getProperty("jdbc.username")); |
| 55 | + dataSource.setPassword(environment.getProperty("jdbc.password")); |
| 56 | + |
| 57 | + return dataSource; |
| 58 | + } |
| 59 | + |
| 60 | + public DatabasePopulator databasePopulator(){ |
| 61 | + ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(); |
| 62 | + databasePopulator.setContinueOnError(true); |
| 63 | + databasePopulator.addScript(new ClassPathResource("test-data.sql")); |
| 64 | + return databasePopulator; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * we need the entity manager for handling Spring transactions |
| 69 | + * @param entityManagerFactory |
| 70 | + * @return |
| 71 | + */ |
| 72 | + @Bean |
| 73 | + public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory){ |
| 74 | + |
| 75 | + JpaTransactionManager jpaTransactionManager = new JpaTransactionManager(); |
| 76 | + |
| 77 | + jpaTransactionManager.setEntityManagerFactory(entityManagerFactory); |
| 78 | + |
| 79 | + DatabasePopulatorUtils.execute(databasePopulator(), dataSource()); |
| 80 | + |
| 81 | + return jpaTransactionManager; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * We need the multipartResolver for uploading images |
| 86 | + * @return |
| 87 | + */ |
| 88 | + @Bean |
| 89 | + public MultipartResolver multipartResolver(){ |
| 90 | + CommonsMultipartResolver resolver = new CommonsMultipartResolver(); |
| 91 | + resolver.setDefaultEncoding("utf-8"); |
| 92 | + resolver.setMaxUploadSize(1024000); |
| 93 | + return resolver; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Specifying the JpaVendorAdapter implementation for the desired JPA provider. |
| 98 | + * This will initialize appropriate defaults for the given provider, |
| 99 | + * such as persistence provider class and JpaDialect, unless locally overridden in this FactoryBean. |
| 100 | + * @return |
| 101 | + */ |
| 102 | + @Bean |
| 103 | + public JpaVendorAdapter jpaVendorAdapter(){ |
| 104 | + HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter(); |
| 105 | + |
| 106 | + hibernateJpaVendorAdapter.setDatabase(Database.MYSQL); |
| 107 | + |
| 108 | + hibernateJpaVendorAdapter.setShowSql(true); |
| 109 | + |
| 110 | + return hibernateJpaVendorAdapter; |
| 111 | + } |
| 112 | + /** |
| 113 | + * |
| 114 | + * @return |
| 115 | + */ |
| 116 | + @Bean |
| 117 | + public LocalContainerEntityManagerFactoryBean entityManagerFactory(){ |
| 118 | + |
| 119 | + LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean(); |
| 120 | + |
| 121 | + entityManagerFactory.setDataSource(dataSource()); |
| 122 | + |
| 123 | + entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter()); |
| 124 | + |
| 125 | + |
| 126 | + entityManagerFactory.setPackagesToScan("modisefileupload.java.domain"); |
| 127 | + |
| 128 | + Properties jpaProperties = new Properties(); |
| 129 | + |
| 130 | + jpaProperties.setProperty("hibernate.hbm2ddl.auto", "create-drop"); |
| 131 | + |
| 132 | + entityManagerFactory.setJpaProperties(jpaProperties); |
| 133 | + |
| 134 | + return entityManagerFactory; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * end of setting up MySQL |
| 139 | + */ |
| 140 | + |
| 141 | +} |
0 commit comments