Browse Source

Polishing

pull/1935/head
Juergen Hoeller 8 years ago
parent
commit
73dfa9a968
  1. 39
      spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlListableBeanFactoryTests.java
  2. 28
      spring-context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java
  3. 11
      spring-context/src/main/java/org/springframework/scheduling/annotation/Scheduled.java
  4. 2
      spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java

39
spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlListableBeanFactoryTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -42,30 +42,33 @@ import static org.junit.Assert.*; @@ -42,30 +42,33 @@ import static org.junit.Assert.*;
* @author Juergen Hoeller
* @since 09.11.2003
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@SuppressWarnings({"rawtypes", "unchecked"})
public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTests {
private DefaultListableBeanFactory parent;
private DefaultListableBeanFactory factory;
@Before
public void setUp() {
public void setup() {
parent = new DefaultListableBeanFactory();
Map m = new HashMap();
m.put("name", "Albert");
Map map = new HashMap();
map.put("name", "Albert");
RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
bd1.setPropertyValues(new MutablePropertyValues(m));
bd1.setPropertyValues(new MutablePropertyValues(map));
parent.registerBeanDefinition("father", bd1);
m = new HashMap();
m.put("name", "Roderick");
map = new HashMap();
map.put("name", "Roderick");
RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
bd2.setPropertyValues(new MutablePropertyValues(m));
bd2.setPropertyValues(new MutablePropertyValues(map));
parent.registerBeanDefinition("rod", bd2);
this.factory = new DefaultListableBeanFactory(parent);
new XmlBeanDefinitionReader(this.factory).loadBeanDefinitions(
new ClassPathResource("test.xml", getClass()));
new XmlBeanDefinitionReader(this.factory).loadBeanDefinitions(new ClassPathResource("test.xml", getClass()));
this.factory.addBeanPostProcessor(new BeanPostProcessor() {
@Override
public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
@ -82,9 +85,10 @@ public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTest @@ -82,9 +85,10 @@ public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTest
return bean;
}
});
this.factory.addBeanPostProcessor(new LifecycleBean.PostProcessor());
this.factory.addBeanPostProcessor(new ProtectedLifecycleBean.PostProcessor());
//this.factory.preInstantiateSingletons();
// this.factory.preInstantiateSingletons();
}
@Override
@ -92,6 +96,7 @@ public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTest @@ -92,6 +96,7 @@ public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTest
return factory;
}
@Test
@Override
public void count() {
@ -104,19 +109,19 @@ public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTest @@ -104,19 +109,19 @@ public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTest
}
@Test
public void lifecycleMethods() throws Exception {
public void lifecycleMethods() {
LifecycleBean bean = (LifecycleBean) getBeanFactory().getBean("lifecycle");
bean.businessMethod();
}
@Test
public void protectedLifecycleMethods() throws Exception {
public void protectedLifecycleMethods() {
ProtectedLifecycleBean bean = (ProtectedLifecycleBean) getBeanFactory().getBean("protectedLifecycle");
bean.businessMethod();
}
@Test
public void descriptionButNoProperties() throws Exception {
public void descriptionButNoProperties() {
TestBean validEmpty = (TestBean) getBeanFactory().getBean("validEmptyWithDescription");
assertEquals(0, validEmpty.getAge());
}
@ -125,7 +130,7 @@ public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTest @@ -125,7 +130,7 @@ public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTest
* Test that properties with name as well as id creating an alias up front.
*/
@Test
public void autoAliasing() throws Exception {
public void autoAliasing() {
List beanNames = Arrays.asList(getListableBeanFactory().getBeanDefinitionNames());
TestBean tb1 = (TestBean) getBeanFactory().getBean("aliased");
@ -224,7 +229,7 @@ public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTest @@ -224,7 +229,7 @@ public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTest
}
@Test
public void beanPostProcessor() throws Exception {
public void beanPostProcessor() {
TestBean kerry = (TestBean) getBeanFactory().getBean("kerry");
TestBean kathy = (TestBean) getBeanFactory().getBean("kathy");
DummyFactory factory = (DummyFactory) getBeanFactory().getBean("&singletonFactory");

28
spring-context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java

@ -154,7 +154,7 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor @@ -154,7 +154,7 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
/**
* Start the specified bean as part of the given set of Lifecycle beans,
* making sure that any beans that it depends on are started first.
* @param lifecycleBeans Map with bean name as key and Lifecycle instance as value
* @param lifecycleBeans a Map with bean name as key and Lifecycle instance as value
* @param beanName the name of the bean to start
*/
private void doStart(Map<String, ? extends Lifecycle> lifecycleBeans, String beanName, boolean autoStartupOnly) {
@ -167,7 +167,7 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor @@ -167,7 +167,7 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
if (!bean.isRunning() &&
(!autoStartupOnly || !(bean instanceof SmartLifecycle) || ((SmartLifecycle) bean).isAutoStartup())) {
if (logger.isDebugEnabled()) {
logger.debug("Starting bean '" + beanName + "' of type [" + bean.getClass() + "]");
logger.debug("Starting bean '" + beanName + "' of type [" + bean.getClass().getName() + "]");
}
try {
bean.start();
@ -207,7 +207,7 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor @@ -207,7 +207,7 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
/**
* Stop the specified bean as part of the given set of Lifecycle beans,
* making sure that any beans that depends on it are stopped first.
* @param lifecycleBeans Map with bean name as key and Lifecycle instance as value
* @param lifecycleBeans a Map with bean name as key and Lifecycle instance as value
* @param beanName the name of the bean to stop
*/
private void doStop(Map<String, ? extends Lifecycle> lifecycleBeans, final String beanName,
@ -223,7 +223,8 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor @@ -223,7 +223,8 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
if (bean.isRunning()) {
if (bean instanceof SmartLifecycle) {
if (logger.isDebugEnabled()) {
logger.debug("Asking bean '" + beanName + "' of type [" + bean.getClass() + "] to stop");
logger.debug("Asking bean '" + beanName + "' of type [" +
bean.getClass().getName() + "] to stop");
}
countDownBeanNames.add(beanName);
((SmartLifecycle) bean).stop(new Runnable() {
@ -239,7 +240,8 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor @@ -239,7 +240,8 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
}
else {
if (logger.isDebugEnabled()) {
logger.debug("Stopping bean '" + beanName + "' of type [" + bean.getClass() + "]");
logger.debug("Stopping bean '" + beanName + "' of type [" +
bean.getClass().getName() + "]");
}
bean.stop();
if (logger.isDebugEnabled()) {
@ -248,7 +250,7 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor @@ -248,7 +250,7 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
}
}
else if (bean instanceof SmartLifecycle) {
// don't wait for beans that aren't running
// Don't wait for beans that aren't running...
latch.countDown();
}
}
@ -307,8 +309,6 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor @@ -307,8 +309,6 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
*/
private class LifecycleGroup {
private final List<LifecycleGroupMember> members = new ArrayList<LifecycleGroupMember>();
private final int phase;
private final long timeout;
@ -317,9 +317,13 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor @@ -317,9 +317,13 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
private final boolean autoStartupOnly;
private volatile int smartMemberCount;
private final List<LifecycleGroupMember> members = new ArrayList<LifecycleGroupMember>();
private int smartMemberCount;
public LifecycleGroup(
int phase, long timeout, Map<String, ? extends Lifecycle> lifecycleBeans, boolean autoStartupOnly) {
public LifecycleGroup(int phase, long timeout, Map<String, ? extends Lifecycle> lifecycleBeans, boolean autoStartupOnly) {
this.phase = phase;
this.timeout = timeout;
this.lifecycleBeans = lifecycleBeans;
@ -327,10 +331,10 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor @@ -327,10 +331,10 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
}
public void add(String name, Lifecycle bean) {
this.members.add(new LifecycleGroupMember(name, bean));
if (bean instanceof SmartLifecycle) {
this.smartMemberCount++;
}
this.members.add(new LifecycleGroupMember(name, bean));
}
public void start() {
@ -363,7 +367,7 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor @@ -363,7 +367,7 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor
doStop(this.lifecycleBeans, member.name, latch, countDownBeanNames);
}
else if (member.bean instanceof SmartLifecycle) {
// already removed, must have been a dependent
// Already removed: must have been a dependent bean from another phase
latch.countDown();
}
}

11
spring-context/src/main/java/org/springframework/scheduling/annotation/Scheduled.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -41,6 +41,7 @@ import java.lang.annotation.Target; @@ -41,6 +41,7 @@ import java.lang.annotation.Target;
* <em>composed annotations</em> with attribute overrides.
*
* @author Mark Fisher
* @author Juergen Hoeller
* @author Dave Syer
* @author Chris Beams
* @since 3.0
@ -55,10 +56,10 @@ import java.lang.annotation.Target; @@ -55,10 +56,10 @@ import java.lang.annotation.Target;
public @interface Scheduled {
/**
* A cron-like expression, extending the usual UN*X definition to include
* triggers on the second as well as minute, hour, day of month, month
* and day of week. e.g. {@code "0 * * * * MON-FRI"} means once per minute on
* weekdays (at the top of the minute - the 0th second).
* A cron-like expression, extending the usual UN*X definition to include triggers
* on the second as well as minute, hour, day of month, month and day of week.
* <p>E.g. {@code "0 * * * * MON-FRI"} means once per minute on weekdays
* (at the top of the minute - the 0th second).
* @return an expression that can be parsed to a cron schedule
* @see org.springframework.scheduling.support.CronSequenceGenerator
*/

2
spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java

@ -101,7 +101,7 @@ public class ScheduledAnnotationBeanPostProcessor @@ -101,7 +101,7 @@ public class ScheduledAnnotationBeanPostProcessor
SmartInitializingSingleton, ApplicationListener<ContextRefreshedEvent>, DisposableBean {
/**
* The default name of the {@link TaskScheduler} bean to pick up: "taskScheduler".
* The default name of the {@link TaskScheduler} bean to pick up: {@value}.
* <p>Note that the initial lookup happens by type; this is just the fallback
* in case of multiple scheduler beans found in the context.
* @since 4.2

Loading…
Cancel
Save