
net.sf.jstuff.integration.spring.SpringBeanRef Maven / Gradle / Ivy
/*
* Copyright 2010-2022 by Sebastian Thomschke and contributors.
* SPDX-License-Identifier: EPL-2.0
*/
package net.sf.jstuff.integration.spring;
import static net.sf.jstuff.core.validation.NullAnalysisHelper.*;
import java.io.Serializable;
import org.eclipse.jdt.annotation.Nullable;
import net.sf.jstuff.core.validation.Args;
/**
* A serializable reference to a Spring managed bean. Relies on a configured {@link SpringBeanLocator}.
*
* @author Sebastian Thomschke
*/
public final class SpringBeanRef implements Serializable {
private static final long serialVersionUID = 1L;
public static SpringBeanRef of(final Class beanType) {
return new SpringBeanRef<>(beanType);
}
public static SpringBeanRef of(final String beanName) {
return new SpringBeanRef<>(beanName);
}
private final @Nullable String beanName;
private final @Nullable Class beanType;
private transient @Nullable T springBean;
private SpringBeanRef(final Class beanType) {
Args.notNull("beanType", beanType);
this.beanType = beanType;
beanName = null;
}
private SpringBeanRef(final String beanName) {
Args.notNull("beanName", beanName);
this.beanName = beanName;
beanType = null;
}
@SuppressWarnings({"unchecked"})
public T get() {
if (springBean == null) {
springBean = (T) (beanName == null //
? SpringBeanLocator.get().byClass(asNonNullUnsafe(beanType))
: SpringBeanLocator.get().byName(asNonNullUnsafe(beanName)));
}
return asNonNullUnsafe(springBean);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy