Example usage for org.springframework.beans.factory SmartFactoryBean SmartFactoryBean

List of usage examples for org.springframework.beans.factory SmartFactoryBean SmartFactoryBean

Introduction

In this page you can find the example usage for org.springframework.beans.factory SmartFactoryBean SmartFactoryBean.

Prototype

SmartFactoryBean

Source Link

Usage

From source file:com.fitbur.testify.fixture.PostgresDockerConfig.java

@Bean
FactoryBean<SessionFactory> sessionFactoryImplProvider(DataSource dataSource) {
    return new SmartFactoryBean<SessionFactory>() {
        @Override/*  www. java  2s.  co m*/
        public boolean isPrototype() {
            return false;
        }

        @Override
        public boolean isEagerInit() {
            return true;
        }

        @Override
        public SessionFactory getObject() throws Exception {
            StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
                    .loadProperties("hibernate.properties").applySetting(Environment.DATASOURCE, dataSource)
                    .build();

            Reflections reflections = new Reflections(PostgresDockerConfig.class.getPackage().getName());
            MetadataSources metadataSources = new MetadataSources(registry);

            MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder()
                    .applyPhysicalNamingStrategy(new PhysicalNamingStrategyStandardImpl())
                    .applyImplicitNamingStrategy(new ImplicitNamingStrategyComponentPathImpl());

            reflections.getSubTypesOf(AttributeConverter.class).parallelStream()
                    .forEach(metadataBuilder::applyAttributeConverter);

            reflections.getTypesAnnotatedWith(Entity.class).parallelStream()
                    .forEach(metadataSources::addAnnotatedClass);

            Metadata metadata = metadataBuilder.build();

            return metadata.buildSessionFactory();
        }

        @Override
        public Class<?> getObjectType() {
            return SessionFactory.class;
        }

        @Override
        public boolean isSingleton() {
            return true;
        }
    };

}