Java tutorial
/** * Copyright 2016 REPLACE ME OWNER (REPLACE ME YEAR) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.homiefund.config; import org.dozer.DozerBeanMapper; import org.dozer.Mapper; import org.homiefund.api.support.HomeAccessVoter; import org.homiefund.api.support.HomeAccessVoterImpl; import org.homiefund.api.support.MailService; import org.homiefund.api.support.MailServiceImpl; import org.homiefund.api.support.UserPreferencesBean; import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Description; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySources; import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.ScopedProxyMode; import org.springframework.mail.MailSender; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.scheduling.annotation.AsyncConfigurer; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.transaction.annotation.EnableTransactionManagement; import java.util.Arrays; import java.util.concurrent.Executor; /** * Created by Dominik Szalai - emptulik at gmail.com on 20.9.2016. */ @Configuration @EnableAsync @EnableTransactionManagement @EnableAspectJAutoProxy(proxyTargetClass = true) @ComponentScan({ "org.homiefund.api" }) @PropertySources({ @PropertySource("classpath:config/datasource.properties"), @PropertySource("classpath:config/hibernate.properties") }) @Import({ DatasourceConfiguration.class, HibernateConfiguration.class }) public class ApplicationConfiguration implements AsyncConfigurer { private LocalContainerEntityManagerFactoryBean emf; @Autowired @Qualifier("localContainerEntityManagerFactoryBean") public void setEmf(LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean) { this.emf = localContainerEntityManagerFactoryBean; } @Bean @Description("JPA Transaction Manager used for controlling transaction using @Transactional annotation.") public JpaTransactionManager jpaTransactionManager() { return new JpaTransactionManager(emf.getObject()); } @Bean @Description("Service used for converting object of type A into object of type B.") public Mapper mapper() { //todo rework into factory bean and move values to application config DozerBeanMapper mapper = new DozerBeanMapper(); mapper.setMappingFiles(Arrays.asList("classpath:config/dozer.xml")); return mapper; } @Bean @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS) @Description("Holder bean containing \"Cached\" values for various parts of application.") public UserPreferencesBean pageModel() { return new UserPreferencesBean(); } @Bean @Description("Wrapper service responsible for sending emails.") public MailService mailService() { return new MailServiceImpl(mailSender(), simpleMailMessage()); } @Bean(name = "homeVoter") public HomeAccessVoter homeAccessVoter() { return new HomeAccessVoterImpl(); } @Bean @Description("Service responsible for sending emails.") public MailSender mailSender() { // todo move to application config JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); mailSender.setHost("localhost"); mailSender.setPort(20025); return mailSender; } @Bean(name = "templateMessage") @Description("Email template for invitation email.") public SimpleMailMessage simpleMailMessage() { //todo create custom config for email templates SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); simpleMailMessage.setFrom("demo@localhost"); simpleMailMessage.setSubject("Invitiation to homiefund"); return simpleMailMessage; } @Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(4); executor.setMaxPoolSize(42); executor.setQueueCapacity(12); executor.setThreadNamePrefix("HomieExecutor-"); executor.initialize(); return executor; } @Override public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { return new SimpleAsyncUncaughtExceptionHandler(); } }