diff --git a/spring-core-2/README.md b/spring-core-2/README.md index 10d3080b45..3c6bd05876 100644 --- a/spring-core-2/README.md +++ b/spring-core-2/README.md @@ -6,13 +6,9 @@ This module contains articles about core Spring functionality - [Guide to Spring @Autowired](http://www.baeldung.com/spring-autowire) - [Spring Profiles](http://www.baeldung.com/spring-profiles) -- [A Spring Custom Annotation for a Better DAO](http://www.baeldung.com/spring-annotation-bean-pre-processor) - [Quick Guide to Spring Bean Scopes](http://www.baeldung.com/spring-bean-scopes) -- [Custom Scope in Spring](http://www.baeldung.com/spring-custom-scope) - [@Order in Spring](http://www.baeldung.com/spring-order) - [Spring @Primary Annotation](http://www.baeldung.com/spring-primary) - [Spring Events](https://www.baeldung.com/spring-events) - [Spring Null-Safety Annotations](https://www.baeldung.com/spring-null-safety-annotations) -- [Using @Autowired in Abstract Classes](https://www.baeldung.com/spring-autowired-abstract-class) -- [Running Setup Data on Startup in Spring](https://www.baeldung.com/running-setup-logic-on-startup-in-spring) - More articles: [[<-- prev]](/spring-core)[[next -->]](/spring-core-3) diff --git a/spring-core-2/src/main/java/com/baeldung/customannotation/CustomAnnotationConfiguration.java b/spring-core-2/src/main/java/com/baeldung/customannotation/CustomAnnotationConfiguration.java deleted file mode 100644 index 2e42a3f744..0000000000 --- a/spring-core-2/src/main/java/com/baeldung/customannotation/CustomAnnotationConfiguration.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.baeldung.customannotation; - -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; - -@Configuration -@ComponentScan("com.baeldung.customannotation") -public class CustomAnnotationConfiguration { -} diff --git a/spring-core-2/src/main/java/com/baeldung/customannotation/DataAccess.java b/spring-core-2/src/main/java/com/baeldung/customannotation/DataAccess.java deleted file mode 100644 index 4160bad16d..0000000000 --- a/spring-core-2/src/main/java/com/baeldung/customannotation/DataAccess.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.baeldung.customannotation; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD }) -@Documented -public @interface DataAccess { - Class entity(); -} diff --git a/spring-core-2/src/main/java/com/baeldung/customannotation/DataAccessAnnotationProcessor.java b/spring-core-2/src/main/java/com/baeldung/customannotation/DataAccessAnnotationProcessor.java deleted file mode 100644 index 27008176a8..0000000000 --- a/spring-core-2/src/main/java/com/baeldung/customannotation/DataAccessAnnotationProcessor.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.baeldung.customannotation; - -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.config.BeanPostProcessor; -import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; -import org.springframework.stereotype.Component; -import org.springframework.util.ReflectionUtils; -import org.springframework.util.ReflectionUtils.FieldCallback; - -@Component -public class DataAccessAnnotationProcessor implements BeanPostProcessor { - - private ConfigurableListableBeanFactory configurableListableBeanFactory; - - @Autowired - public DataAccessAnnotationProcessor(ConfigurableListableBeanFactory bf) { - configurableListableBeanFactory = bf; - } - - @Override - public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { - scanDataAccessAnnotation(bean, beanName); - return bean; - } - - @Override - public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { - return bean; - } - - protected void scanDataAccessAnnotation(Object bean, String beanName) { - Class managedBeanClass = bean.getClass(); - FieldCallback fcb = new DataAccessFieldCallback(configurableListableBeanFactory, bean); - ReflectionUtils.doWithFields(managedBeanClass, fcb); - } -} diff --git a/spring-core-2/src/main/java/com/baeldung/customannotation/DataAccessFieldCallback.java b/spring-core-2/src/main/java/com/baeldung/customannotation/DataAccessFieldCallback.java deleted file mode 100644 index 07b5298ea9..0000000000 --- a/spring-core-2/src/main/java/com/baeldung/customannotation/DataAccessFieldCallback.java +++ /dev/null @@ -1,96 +0,0 @@ -package com.baeldung.customannotation; - -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.config.AutowireCapableBeanFactory; -import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; -import org.springframework.util.ReflectionUtils; -import org.springframework.util.ReflectionUtils.FieldCallback; - -public final class DataAccessFieldCallback implements FieldCallback { - - private static Logger logger = LoggerFactory.getLogger(DataAccessFieldCallback.class); - private static int AUTOWIRE_MODE = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME; - - private static String ERROR_ENTITY_VALUE_NOT_SAME = "@DataAccess(entity) " + "value should have same type with injected generic type."; - private static String WARN_NON_GENERIC_VALUE = "@DataAccess annotation assigned " + "to raw (non-generic) declaration. This will make your code less type-safe."; - private static String ERROR_CREATE_INSTANCE = "Cannot create instance of " + "type '{}' or instance creation is failed because: {}"; - - private ConfigurableListableBeanFactory configurableListableBeanFactory; - private Object bean; - - public DataAccessFieldCallback(final ConfigurableListableBeanFactory bf, final Object bean) { - configurableListableBeanFactory = bf; - this.bean = bean; - } - - @Override - public void doWith(final Field field) throws IllegalArgumentException, IllegalAccessException { - if (!field.isAnnotationPresent(DataAccess.class)) { - return; - } - ReflectionUtils.makeAccessible(field); - final Type fieldGenericType = field.getGenericType(); - // In this example, get actual "GenericDAO' type. - final Class generic = field.getType(); - final Class classValue = field.getDeclaredAnnotation(DataAccess.class).entity(); - - if (genericTypeIsValid(classValue, fieldGenericType)) { - final String beanName = classValue.getSimpleName() + generic.getSimpleName(); - final Object beanInstance = getBeanInstance(beanName, generic, classValue); - field.set(bean, beanInstance); - } else { - throw new IllegalArgumentException(ERROR_ENTITY_VALUE_NOT_SAME); - } - } - - /** - * For example, if user write: - *
-     * @DataAccess(entity=Person.class) 
-     * private GenericDAO<Account> personGenericDAO;
-     * 
- * then this is should be failed. - */ - public boolean genericTypeIsValid(final Class clazz, final Type field) { - if (field instanceof ParameterizedType) { - final ParameterizedType parameterizedType = (ParameterizedType) field; - final Type type = parameterizedType.getActualTypeArguments()[0]; - - return type.equals(clazz); - } else { - logger.warn(WARN_NON_GENERIC_VALUE); - return true; - } - } - - public final Object getBeanInstance(final String beanName, final Class genericClass, final Class paramClass) { - Object daoInstance = null; - if (!configurableListableBeanFactory.containsBean(beanName)) { - logger.info("Creating new DataAccess bean named '{}'.", beanName); - - Object toRegister = null; - try { - final Constructor ctr = genericClass.getConstructor(Class.class); - toRegister = ctr.newInstance(paramClass); - } catch (final Exception e) { - logger.error(ERROR_CREATE_INSTANCE, genericClass.getTypeName(), e); - throw new RuntimeException(e); - } - - daoInstance = configurableListableBeanFactory.initializeBean(toRegister, beanName); - configurableListableBeanFactory.autowireBeanProperties(daoInstance, AUTOWIRE_MODE, true); - configurableListableBeanFactory.registerSingleton(beanName, daoInstance); - logger.info("Bean named '{}' created successfully.", beanName); - } else { - daoInstance = configurableListableBeanFactory.getBean(beanName); - logger.info("Bean named '{}' already exist used as current bean reference.", beanName); - } - return daoInstance; - } -} diff --git a/spring-core-2/src/main/java/com/baeldung/customannotation/GenericDAO.java b/spring-core-2/src/main/java/com/baeldung/customannotation/GenericDAO.java deleted file mode 100644 index 0edd33b049..0000000000 --- a/spring-core-2/src/main/java/com/baeldung/customannotation/GenericDAO.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.baeldung.customannotation; - -import java.util.Collections; -import java.util.List; -import java.util.Optional; - -public class GenericDAO { - - private Class entityClass; - private String message; - - public GenericDAO(Class entityClass) { - this.entityClass = entityClass; - } - - public List findAll() { - message = "Would create findAll query from " + entityClass.getSimpleName(); - return Collections.emptyList(); - } - - public Optional persist(E toPersist) { - message = "Would create persist query from " + toPersist.getClass().getSimpleName(); - return Optional.empty(); - } - - /** Only used for unit-testing. */ - public final String getMessage() { - return message; - } -} diff --git a/spring-core-2/src/main/java/com/baeldung/customscope/TenantBean.java b/spring-core-2/src/main/java/com/baeldung/customscope/TenantBean.java deleted file mode 100644 index e892ae9d9b..0000000000 --- a/spring-core-2/src/main/java/com/baeldung/customscope/TenantBean.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.baeldung.customscope; - -public class TenantBean { - - private final String name; - - public TenantBean(String name) { - this.name = name; - } - - public void sayHello() { - System.out.println(String.format("Hello from %s of type %s", this.name, this.getClass().getName())); - } -} diff --git a/spring-core-2/src/main/java/com/baeldung/customscope/TenantBeanFactoryPostProcessor.java b/spring-core-2/src/main/java/com/baeldung/customscope/TenantBeanFactoryPostProcessor.java deleted file mode 100644 index 84ed0b46d7..0000000000 --- a/spring-core-2/src/main/java/com/baeldung/customscope/TenantBeanFactoryPostProcessor.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.baeldung.customscope; - -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.config.BeanFactoryPostProcessor; -import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; - -public class TenantBeanFactoryPostProcessor implements BeanFactoryPostProcessor { - - @Override - public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) throws BeansException { - factory.registerScope("tenant", new TenantScope()); - } -} diff --git a/spring-core-2/src/main/java/com/baeldung/customscope/TenantBeansConfig.java b/spring-core-2/src/main/java/com/baeldung/customscope/TenantBeansConfig.java deleted file mode 100644 index c219000fe6..0000000000 --- a/spring-core-2/src/main/java/com/baeldung/customscope/TenantBeansConfig.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.customscope; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Scope; - -@Configuration -public class TenantBeansConfig { - - @Scope(scopeName = "tenant") - @Bean - public TenantBean foo() { - return new TenantBean("foo"); - } - - @Scope(scopeName = "tenant") - @Bean - public TenantBean bar() { - return new TenantBean("bar"); - } -} diff --git a/spring-core-2/src/main/java/com/baeldung/customscope/TenantScope.java b/spring-core-2/src/main/java/com/baeldung/customscope/TenantScope.java deleted file mode 100644 index f3077bc4c2..0000000000 --- a/spring-core-2/src/main/java/com/baeldung/customscope/TenantScope.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.baeldung.customscope; - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -import org.springframework.beans.factory.ObjectFactory; -import org.springframework.beans.factory.config.Scope; - -public class TenantScope implements Scope { - - private Map scopedObjects = Collections.synchronizedMap(new HashMap()); - private Map destructionCallbacks = Collections.synchronizedMap(new HashMap()); - - @Override - public Object get(String name, ObjectFactory objectFactory) { - if (!scopedObjects.containsKey(name)) { - scopedObjects.put(name, objectFactory.getObject()); - } - return scopedObjects.get(name); - } - - @Override - public Object remove(String name) { - destructionCallbacks.remove(name); - return scopedObjects.remove(name); - } - - @Override - public void registerDestructionCallback(String name, Runnable callback) { - destructionCallbacks.put(name, callback); - } - - @Override - public Object resolveContextualObject(String key) { - return null; - } - - @Override - public String getConversationId() { - return "tenant"; - } -} diff --git a/spring-core-2/src/main/java/com/baeldung/customscope/TenantScopeConfig.java b/spring-core-2/src/main/java/com/baeldung/customscope/TenantScopeConfig.java deleted file mode 100644 index 1829e1e8c4..0000000000 --- a/spring-core-2/src/main/java/com/baeldung/customscope/TenantScopeConfig.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.baeldung.customscope; - -import org.springframework.beans.factory.config.BeanFactoryPostProcessor; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class TenantScopeConfig { - - @Bean - public static BeanFactoryPostProcessor beanFactoryPostProcessor() { - return new TenantBeanFactoryPostProcessor(); - } -} diff --git a/spring-core-2/src/main/java/com/baeldung/sampleabstract/BallService.java b/spring-core-2/src/main/java/com/baeldung/sampleabstract/BallService.java deleted file mode 100644 index 0d951aac8b..0000000000 --- a/spring-core-2/src/main/java/com/baeldung/sampleabstract/BallService.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.sampleabstract; - -import org.springframework.beans.factory.annotation.Autowired; - -import javax.annotation.PostConstruct; - -public abstract class BallService { - - private RuleRepository ruleRepository; - - private LogRepository logRepository; - - public BallService(RuleRepository ruleRepository) { - this.ruleRepository = ruleRepository; - } - - @Autowired - public final void setLogRepository(LogRepository logRepository) { - this.logRepository = logRepository; - } - - @PostConstruct - public void afterInitialize() { - - System.out.println(ruleRepository.toString()); - System.out.println(logRepository.toString()); - } -} diff --git a/spring-core-2/src/main/java/com/baeldung/sampleabstract/BasketballService.java b/spring-core-2/src/main/java/com/baeldung/sampleabstract/BasketballService.java deleted file mode 100644 index 4d6345b069..0000000000 --- a/spring-core-2/src/main/java/com/baeldung/sampleabstract/BasketballService.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.baeldung.sampleabstract; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -@Component -public class BasketballService extends BallService { - - @Autowired - public BasketballService(RuleRepository ruleRepository) { - super(ruleRepository); - } -} diff --git a/spring-core-2/src/main/java/com/baeldung/sampleabstract/DemoApp.java b/spring-core-2/src/main/java/com/baeldung/sampleabstract/DemoApp.java deleted file mode 100644 index 5a308b2671..0000000000 --- a/spring-core-2/src/main/java/com/baeldung/sampleabstract/DemoApp.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.baeldung.sampleabstract; - -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; - -@Configuration -@ComponentScan(basePackages = "com.baeldung.sampleabstract") -public class DemoApp { - - - public static void main(String[] args) { - - ApplicationContext applicationContext = new AnnotationConfigApplicationContext(DemoApp.class); - } - -} diff --git a/spring-core-2/src/main/java/com/baeldung/sampleabstract/LogRepository.java b/spring-core-2/src/main/java/com/baeldung/sampleabstract/LogRepository.java deleted file mode 100644 index 84979768b5..0000000000 --- a/spring-core-2/src/main/java/com/baeldung/sampleabstract/LogRepository.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.baeldung.sampleabstract; - -import org.springframework.stereotype.Component; - -@Component -public class LogRepository { - - @Override - public String toString() { - return "logRepository"; - } -} diff --git a/spring-core-2/src/main/java/com/baeldung/sampleabstract/RuleRepository.java b/spring-core-2/src/main/java/com/baeldung/sampleabstract/RuleRepository.java deleted file mode 100644 index a1c5b5067f..0000000000 --- a/spring-core-2/src/main/java/com/baeldung/sampleabstract/RuleRepository.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.baeldung.sampleabstract; - -import org.springframework.stereotype.Component; - -@Component -public class RuleRepository { - - @Override - public String toString() { - return "ruleRepository"; - } -} diff --git a/spring-core-2/src/main/java/com/baeldung/startup/AllStrategiesExampleBean.java b/spring-core-2/src/main/java/com/baeldung/startup/AllStrategiesExampleBean.java deleted file mode 100644 index e08309d474..0000000000 --- a/spring-core-2/src/main/java/com/baeldung/startup/AllStrategiesExampleBean.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.baeldung.startup; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import org.springframework.beans.factory.InitializingBean; -import org.springframework.context.annotation.Scope; -import org.springframework.stereotype.Component; - -import javax.annotation.PostConstruct; - -@Component -@Scope(value = "prototype") -public class AllStrategiesExampleBean implements InitializingBean { - - private static final Logger LOG = LoggerFactory.getLogger(AllStrategiesExampleBean.class); - - public AllStrategiesExampleBean() { - LOG.info("Constructor"); - } - - @Override - public void afterPropertiesSet() throws Exception { - LOG.info("InitializingBean"); - } - - @PostConstruct - public void postConstruct() { - LOG.info("PostConstruct"); - } - - public void init() { - LOG.info("init-method"); - } -} diff --git a/spring-core-2/src/main/java/com/baeldung/startup/EventListenerExampleBean.java b/spring-core-2/src/main/java/com/baeldung/startup/EventListenerExampleBean.java deleted file mode 100644 index a76fc6a2b2..0000000000 --- a/spring-core-2/src/main/java/com/baeldung/startup/EventListenerExampleBean.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.startup; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import org.springframework.context.event.ContextRefreshedEvent; -import org.springframework.context.event.EventListener; -import org.springframework.stereotype.Component; - -@Component -public class EventListenerExampleBean { - private static final Logger LOG = LoggerFactory.getLogger(EventListenerExampleBean.class); - - public static int counter; - - @EventListener - public void onApplicationEvent(ContextRefreshedEvent event) { - LOG.info("Increment counter"); - counter++; - } -} diff --git a/spring-core-2/src/main/java/com/baeldung/startup/InitMethodExampleBean.java b/spring-core-2/src/main/java/com/baeldung/startup/InitMethodExampleBean.java deleted file mode 100644 index a3b12028d1..0000000000 --- a/spring-core-2/src/main/java/com/baeldung/startup/InitMethodExampleBean.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.baeldung.startup; - -import java.util.Arrays; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Scope; -import org.springframework.core.env.Environment; -import org.springframework.stereotype.Component; - -@Component -@Scope(value = "prototype") -public class InitMethodExampleBean { - - private static final Logger LOG = LoggerFactory.getLogger(InitMethodExampleBean.class); - - @Autowired - private Environment environment; - - public void init() { - LOG.info("Env Default Profiles", Arrays.asList(environment.getDefaultProfiles())); - } -} diff --git a/spring-core-2/src/main/java/com/baeldung/startup/InitializingBeanExampleBean.java b/spring-core-2/src/main/java/com/baeldung/startup/InitializingBeanExampleBean.java deleted file mode 100644 index c625a172fd..0000000000 --- a/spring-core-2/src/main/java/com/baeldung/startup/InitializingBeanExampleBean.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.baeldung.startup; - -import java.util.Arrays; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Scope; -import org.springframework.core.env.Environment; -import org.springframework.stereotype.Component; - -@Component -@Scope(value = "prototype") -public class InitializingBeanExampleBean implements InitializingBean { - - private static final Logger LOG = LoggerFactory.getLogger(InitializingBeanExampleBean.class); - - @Autowired - private Environment environment; - - @Override - public void afterPropertiesSet() throws Exception { - LOG.info("Env Default Profiles", Arrays.asList(environment.getDefaultProfiles())); - } -} diff --git a/spring-core-2/src/main/java/com/baeldung/startup/InvalidInitExampleBean.java b/spring-core-2/src/main/java/com/baeldung/startup/InvalidInitExampleBean.java deleted file mode 100644 index d31aee8acd..0000000000 --- a/spring-core-2/src/main/java/com/baeldung/startup/InvalidInitExampleBean.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.baeldung.startup; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Scope; -import org.springframework.core.env.Environment; -import org.springframework.stereotype.Component; - -@Component -@Scope("prototype") -public class InvalidInitExampleBean { - - @Autowired - private Environment environment; - - public InvalidInitExampleBean() { - environment.getActiveProfiles(); - } -} diff --git a/spring-core-2/src/main/java/com/baeldung/startup/LogicInConstructorExampleBean.java b/spring-core-2/src/main/java/com/baeldung/startup/LogicInConstructorExampleBean.java deleted file mode 100644 index ade7573bbe..0000000000 --- a/spring-core-2/src/main/java/com/baeldung/startup/LogicInConstructorExampleBean.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.baeldung.startup; - -import java.util.Arrays; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Scope; -import org.springframework.core.env.Environment; -import org.springframework.stereotype.Component; - -@Component -@Scope(value = "prototype") -public class LogicInConstructorExampleBean { - - private static final Logger LOG = LoggerFactory.getLogger(LogicInConstructorExampleBean.class); - - @Autowired - public LogicInConstructorExampleBean(Environment environment) { - LOG.info("Env Default Profiles", Arrays.asList(environment.getDefaultProfiles())); - } -} diff --git a/spring-core-2/src/main/java/com/baeldung/startup/PostConstructExampleBean.java b/spring-core-2/src/main/java/com/baeldung/startup/PostConstructExampleBean.java deleted file mode 100644 index 1001043d86..0000000000 --- a/spring-core-2/src/main/java/com/baeldung/startup/PostConstructExampleBean.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.baeldung.startup; - -import java.util.Arrays; - -import javax.annotation.PostConstruct; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Scope; -import org.springframework.core.env.Environment; -import org.springframework.stereotype.Component; - -@Component -@Scope(value = "prototype") -public class PostConstructExampleBean { - - private static final Logger LOG = LoggerFactory.getLogger(PostConstructExampleBean.class); - - @Autowired - private Environment environment; - - @PostConstruct - public void init() { - LOG.info("Env Default Profiles", Arrays.asList(environment.getDefaultProfiles())); - } -} diff --git a/spring-core-2/src/main/java/com/baeldung/startup/SpringStartupConfig.java b/spring-core-2/src/main/java/com/baeldung/startup/SpringStartupConfig.java deleted file mode 100644 index ad6492dadc..0000000000 --- a/spring-core-2/src/main/java/com/baeldung/startup/SpringStartupConfig.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.baeldung.startup; - -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; - -@Configuration -@ComponentScan("com.baeldung.startup") -public class SpringStartupConfig { -} \ No newline at end of file diff --git a/spring-core-2/src/main/java/com/baeldung/startup/StartupApplicationListenerExample.java b/spring-core-2/src/main/java/com/baeldung/startup/StartupApplicationListenerExample.java deleted file mode 100644 index 2cc5e6abcb..0000000000 --- a/spring-core-2/src/main/java/com/baeldung/startup/StartupApplicationListenerExample.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.baeldung.startup; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import org.springframework.context.ApplicationListener; -import org.springframework.context.event.ContextRefreshedEvent; -import org.springframework.stereotype.Component; - -@Component -public class StartupApplicationListenerExample implements ApplicationListener { - - private static final Logger LOG = LoggerFactory.getLogger(StartupApplicationListenerExample.class); - - public static int counter; - - @Override - public void onApplicationEvent(ContextRefreshedEvent event) { - LOG.info("Increment counter"); - counter++; - } -} diff --git a/spring-core-2/src/main/resources/startupConfig.xml b/spring-core-2/src/main/resources/startupConfig.xml deleted file mode 100644 index d42e0f6c2b..0000000000 --- a/spring-core-2/src/main/resources/startupConfig.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/spring-core-2/src/test/java/com/baeldung/customannotation/Account.java b/spring-core-2/src/test/java/com/baeldung/customannotation/Account.java deleted file mode 100644 index cfdd8815e4..0000000000 --- a/spring-core-2/src/test/java/com/baeldung/customannotation/Account.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.baeldung.customannotation; - -import java.io.Serializable; - -public class Account implements Serializable { - - private static final long serialVersionUID = 7857541629844398625L; - - private Long id; - private String email; - private Person person; - - public Account() { - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getEmail() { - return email; - } - - public void setEmail(String email) { - this.email = email; - } - - public Person getPerson() { - return person; - } - - public void setPerson(Person person) { - this.person = person; - } - -} diff --git a/spring-core-2/src/test/java/com/baeldung/customannotation/BeanWithGenericDAO.java b/spring-core-2/src/test/java/com/baeldung/customannotation/BeanWithGenericDAO.java deleted file mode 100644 index a0707f263b..0000000000 --- a/spring-core-2/src/test/java/com/baeldung/customannotation/BeanWithGenericDAO.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.baeldung.customannotation; - -import org.springframework.stereotype.Repository; - -@Repository -public class BeanWithGenericDAO { - - @DataAccess(entity = Person.class) - private GenericDAO personGenericDAO; - - public BeanWithGenericDAO() { - } - - public GenericDAO getPersonGenericDAO() { - return personGenericDAO; - } - -} diff --git a/spring-core-2/src/test/java/com/baeldung/customannotation/DataAccessAnnotationIntegrationTest.java b/spring-core-2/src/test/java/com/baeldung/customannotation/DataAccessAnnotationIntegrationTest.java deleted file mode 100644 index 1baea4505b..0000000000 --- a/spring-core-2/src/test/java/com/baeldung/customannotation/DataAccessAnnotationIntegrationTest.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.baeldung.customannotation; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.not; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.CoreMatchers.sameInstance; -import static org.junit.Assert.assertThat; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = { CustomAnnotationConfiguration.class }) -public class DataAccessAnnotationIntegrationTest { - - @DataAccess(entity = Person.class) - private GenericDAO personGenericDAO; - @DataAccess(entity = Account.class) - private GenericDAO accountGenericDAO; - @DataAccess(entity = Person.class) - private GenericDAO anotherPersonGenericDAO; - - @Test - public void whenGenericDAOInitialized_thenNotNull() { - assertThat(personGenericDAO, is(notNullValue())); - assertThat(accountGenericDAO, is(notNullValue())); - } - - @Test - public void whenGenericDAOInjected_thenItIsSingleton() { - assertThat(personGenericDAO, not(sameInstance(accountGenericDAO))); - assertThat(personGenericDAO, not(equalTo(accountGenericDAO))); - - assertThat(personGenericDAO, sameInstance(anotherPersonGenericDAO)); - } - - @Test - public void whenFindAll_thenMessagesIsCorrect() { - personGenericDAO.findAll(); - assertThat(personGenericDAO.getMessage(), is("Would create findAll query from Person")); - - accountGenericDAO.findAll(); - assertThat(accountGenericDAO.getMessage(), is("Would create findAll query from Account")); - } - - @Test - public void whenPersist_thenMakeSureThatMessagesIsCorrect() { - personGenericDAO.persist(new Person()); - assertThat(personGenericDAO.getMessage(), is("Would create persist query from Person")); - - accountGenericDAO.persist(new Account()); - assertThat(accountGenericDAO.getMessage(), is("Would create persist query from Account")); - } -} diff --git a/spring-core-2/src/test/java/com/baeldung/customannotation/DataAccessFieldCallbackIntegrationTest.java b/spring-core-2/src/test/java/com/baeldung/customannotation/DataAccessFieldCallbackIntegrationTest.java deleted file mode 100644 index bc7a5f7246..0000000000 --- a/spring-core-2/src/test/java/com/baeldung/customannotation/DataAccessFieldCallbackIntegrationTest.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.baeldung.customannotation; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.junit.Assert.assertThat; - -import java.lang.reflect.Type; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = { CustomAnnotationConfiguration.class }) -public class DataAccessFieldCallbackIntegrationTest { - - @Autowired - private ConfigurableListableBeanFactory configurableListableBeanFactory; - - @Autowired - private BeanWithGenericDAO beanWithGenericDAO; - - @Rule - public ExpectedException ex = ExpectedException.none(); - - @Test - public void whenObjectCreated_thenObjectCreationIsSuccessful() { - final DataAccessFieldCallback dataAccessFieldCallback = new DataAccessFieldCallback(configurableListableBeanFactory, beanWithGenericDAO); - assertThat(dataAccessFieldCallback, is(notNullValue())); - } - - @Test - public void whenMethodGenericTypeIsValidCalled_thenReturnCorrectValue() throws NoSuchFieldException, SecurityException { - final DataAccessFieldCallback callback = new DataAccessFieldCallback(configurableListableBeanFactory, beanWithGenericDAO); - final Type fieldType = BeanWithGenericDAO.class.getDeclaredField("personGenericDAO").getGenericType(); - final boolean result = callback.genericTypeIsValid(Person.class, fieldType); - assertThat(result, is(true)); - } - - @Test - public void whenMethodGetBeanInstanceCalled_thenReturnCorrectInstance() { - final DataAccessFieldCallback callback = new DataAccessFieldCallback(configurableListableBeanFactory, beanWithGenericDAO); - final Object result = callback.getBeanInstance("personGenericDAO", GenericDAO.class, Person.class); - assertThat((result instanceof GenericDAO), is(true)); - } -} diff --git a/spring-core-2/src/test/java/com/baeldung/customannotation/Person.java b/spring-core-2/src/test/java/com/baeldung/customannotation/Person.java deleted file mode 100644 index 4fa70e51af..0000000000 --- a/spring-core-2/src/test/java/com/baeldung/customannotation/Person.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.baeldung.customannotation; - -import java.io.Serializable; - -public class Person implements Serializable { - - private static final long serialVersionUID = 9005331414216374586L; - - private Long id; - private String name; - - public Person() { - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - -} diff --git a/spring-core-2/src/test/java/com/baeldung/customscope/TenantScopeIntegrationTest.java b/spring-core-2/src/test/java/com/baeldung/customscope/TenantScopeIntegrationTest.java deleted file mode 100644 index 1cd7357a09..0000000000 --- a/spring-core-2/src/test/java/com/baeldung/customscope/TenantScopeIntegrationTest.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.baeldung.customscope; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.not; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -import java.util.Map; - -import org.junit.Test; -import org.springframework.beans.factory.config.BeanDefinition; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; - -public class TenantScopeIntegrationTest { - - @Test - public final void whenRegisterScopeAndBeans_thenContextContainsFooAndBar() { - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - try { - ctx.register(TenantScopeConfig.class); - ctx.register(TenantBeansConfig.class); - ctx.refresh(); - - TenantBean foo = (TenantBean) ctx.getBean("foo", TenantBean.class); - foo.sayHello(); - TenantBean bar = (TenantBean) ctx.getBean("bar", TenantBean.class); - bar.sayHello(); - Map foos = ctx.getBeansOfType(TenantBean.class); - - assertThat(foo, not(equalTo(bar))); - assertThat(foos.size(), equalTo(2)); - assertTrue(foos.containsValue(foo)); - assertTrue(foos.containsValue(bar)); - - BeanDefinition fooDefinition = ctx.getBeanDefinition("foo"); - BeanDefinition barDefinition = ctx.getBeanDefinition("bar"); - - assertThat(fooDefinition.getScope(), equalTo("tenant")); - assertThat(barDefinition.getScope(), equalTo("tenant")); - } finally { - ctx.close(); - } - } - - @Test - public final void whenComponentScan_thenContextContainsFooAndBar() { - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - try { - ctx.scan("com.baeldung.customscope"); - ctx.refresh(); - - TenantBean foo = (TenantBean) ctx.getBean("foo", TenantBean.class); - foo.sayHello(); - TenantBean bar = (TenantBean) ctx.getBean("bar", TenantBean.class); - bar.sayHello(); - Map foos = ctx.getBeansOfType(TenantBean.class); - - assertThat(foo, not(equalTo(bar))); - assertThat(foos.size(), equalTo(2)); - assertTrue(foos.containsValue(foo)); - assertTrue(foos.containsValue(bar)); - - BeanDefinition fooDefinition = ctx.getBeanDefinition("foo"); - BeanDefinition barDefinition = ctx.getBeanDefinition("bar"); - - assertThat(fooDefinition.getScope(), equalTo("tenant")); - assertThat(barDefinition.getScope(), equalTo("tenant")); - } finally { - ctx.close(); - } - } -} diff --git a/spring-core-2/src/test/java/com/baeldung/startup/SpringStartupIntegrationTest.java b/spring-core-2/src/test/java/com/baeldung/startup/SpringStartupIntegrationTest.java deleted file mode 100644 index b58c093c31..0000000000 --- a/spring-core-2/src/test/java/com/baeldung/startup/SpringStartupIntegrationTest.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.baeldung.startup; - -import org.assertj.core.api.Assertions; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.BeanCreationException; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ApplicationContext; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.support.AnnotationConfigContextLoader; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = { SpringStartupConfig.class }, loader = AnnotationConfigContextLoader.class) -public class SpringStartupIntegrationTest { - - @Autowired - private ApplicationContext ctx; - - @Test(expected = BeanCreationException.class) - public void whenInstantiating_shouldThrowBCE() throws Exception { - ctx.getBean(InvalidInitExampleBean.class); - } - - @Test - public void whenPostConstruct_shouldLogEnv() throws Exception { - ctx.getBean(PostConstructExampleBean.class); - } - - @Test - public void whenConstructorInjection_shouldLogEnv() throws Exception { - ctx.getBean(LogicInConstructorExampleBean.class); - } - - @Test - public void whenInitializingBean_shouldLogEnv() throws Exception { - ctx.getBean(InitializingBeanExampleBean.class); - } - - @Test - public void whenApplicationListener_shouldRunOnce() throws Exception { - Assertions.assertThat(StartupApplicationListenerExample.counter).isEqualTo(1); - } -} \ No newline at end of file diff --git a/spring-core-2/src/test/java/com/baeldung/startup/SpringStartupXMLConfigIntegrationTest.java b/spring-core-2/src/test/java/com/baeldung/startup/SpringStartupXMLConfigIntegrationTest.java deleted file mode 100644 index 3dfd4835df..0000000000 --- a/spring-core-2/src/test/java/com/baeldung/startup/SpringStartupXMLConfigIntegrationTest.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.baeldung.startup; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ApplicationContext; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration("classpath:startupConfig.xml") -public class SpringStartupXMLConfigIntegrationTest { - - @Autowired - private ApplicationContext ctx; - - @Test - public void whenPostConstruct_shouldLogEnv() throws Exception { - ctx.getBean(InitMethodExampleBean.class); - } - - @Test - public void whenAllStrategies_shouldLogOrder() throws Exception { - ctx.getBean(AllStrategiesExampleBean.class); - } -}