net.przemkovv.sphinx.config.ApplicationConfig.java Source code

Java tutorial

Introduction

Here is the source code for net.przemkovv.sphinx.config.ApplicationConfig.java

Source

/*
 * The MIT License
 *
 * Copyright 2013 Przemyslaw Walkowiak <przemyslaw.walkowiak@put.poznan.pl>.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package net.przemkovv.sphinx.config;

import java.beans.PropertyEditor;
import java.util.HashMap;
import java.util.Map;
import net.przemkovv.sphinx.web.util.ReloadableMessageSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.CustomEditorConfigurer;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.aspectj.EnableSpringConfigured;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Controller;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

/**
 *
 * @author Przemyslaw Walkowiak <przemyslaw.walkowiak@put.poznan.pl>
 */
@Configuration
@ComponentScan(basePackages = { "net.przemkovv.sphinx" }, useDefaultFilters = true, excludeFilters = {
        @Filter(value = { Controller.class, Configuration.class }) })
@EnableSpringConfigured
@PropertySource("classpath:jdbc.properties")
public class ApplicationConfig {

    private static final Logger logger = LoggerFactory.getLogger(ApplicationConfig.class);

    @Bean(name = "applicationMessageSource")
    //    @Qualifier("applicationMessageSource")
    public MessageSource applicationMessageSource() {
        logger.debug("Bean initialization: ApplicationMessageSource");
        ReloadableMessageSource source = new ReloadableMessageSource();
        source.setCacheSeconds(5);
        source.setDefaultEncoding("UTF-8");
        source.setFallbackToSystemLocale(false);
        source.setUseCodeAsDefaultMessage(true);
        source.setBasenames("classpath:build", "classpath:validation");
        return source;

    }

    @Bean
    public LocalValidatorFactoryBean getValidator() {
        logger.debug("Bean initialization: Validator");
        LocalValidatorFactoryBean factory = new LocalValidatorFactoryBean();
        factory.setValidationMessageSource(applicationMessageSource());
        return factory;

    }

    @Bean
    public CustomEditorConfigurer getCustomEditorConfigurer() {
        logger.debug("Bean initialization: CustomEditorConfigurer");
        CustomEditorConfigurer configurer = new CustomEditorConfigurer();
        Map<Class<?>, Class<? extends PropertyEditor>> customEditors;
        customEditors = new HashMap<>();
        customEditors.put(java.io.Reader.class,
                net.przemkovv.sphinx.web.util.ServletContextResourceReaderPropertyEditor.class);
        configurer.setCustomEditors(customEditors);
        return configurer;
    }

    @Bean
    public JavaMailSender getJavaMailSender() {
        logger.debug("Bean initialization: JavaMailSender");
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost("127.0.0.1");
        return sender;
    }

}