Use #Value on a conditional Bean - spring

I'm providing a value via a conditional Bean. If the Condition is met everything is fine but if the condition is not met (hence the bean is not present) my code fails. is there some way to check if the bean is defined before hand. in SpEL ?
I tried something like
#{someBean? someBean.myValue:null} but it does not work.

See this answer for why this works...
#SpringBootApplication
public class So56189689Application {
public static void main(String[] args) {
SpringApplication.run(So56189689Application.class, args);
}
#Value("#{containsObject('foo') ? getObject('foo').foo : null}")
String foo;
#Bean
public ApplicationRunner runner() {
return args -> System.out.println(foo);
}
// #Bean
// public Foo foo() {
// return new Foo();
// }
public static class Foo {
private String foo = "bar";
public String getFoo() {
return this.foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
}
}
EDIT
The #root object of the SpEL Expression is a BeanExpressionContext, you can invoke containsObject() and getObject() methods on that context.
Here's the code from the BeanExpressionContext:
public boolean containsObject(String key) {
return (this.beanFactory.containsBean(key) ||
(this.scope != null && this.scope.resolveContextualObject(key) != null));
}
public Object getObject(String key) {
if (this.beanFactory.containsBean(key)) {
return this.beanFactory.getBean(key);
}
else if (this.scope != null){
return this.scope.resolveContextualObject(key);
}
else {
return null;
}
}

Related

weird issue with PostConstruct in parent class

I have such a simple example:
public class Base<T> {
private String value;
public Base(String value) {
this.value = value;
}
#PostConstruct
private void init() {
System.out.println(value);
}
}
public class StringTest extends Base<String> {
public StringTest(String value) {
super(value);
}
}
public class IntegerTest extends Base<Integer> {
public IntegerTest(String value) {
super(value);
}
}
#Configuration
public class Configuration {
#bean(name="stringtest1")
public Base<?> getStringTest1() {
return new StringTest("string test 1");
}
#bean(name="stringtest2")
public Base<?> getStringTest2() {
return new StringTest("string test 2");
}
#bean(name="integertest1")
public Base<?> getIntegerTest1() {
return new IntegerTest("integer test 1");
}
#bean(name="integertest2")
public Base<?> getIntegerTest2() {
return new IntegerTest("integer test 2");
}
}
The output is
string test 1
string test 2
PostConstruct is only invoked for beans stringtest1 and stringtest2 but not for integertest1 and integertest2.
If I change the configuration to below (change ? to the concrete type)
#bean(name="stringtest1")
public Base<String> getStringTest1() {
return new StringTest("string test 1");
}
#bean(name="stringtest2")
public Base<String> getStringTest2() {
return new StringTest("string test 2");
}
#bean(name="integertest1")
public Base<Integer> getIntegerTest1() {
return new IntegerTest("integer test 1");
}
#bean(name="integertest2")
public Base<Integer> getIntegerTest2() {
return new IntegerTest("integer test 2");
}
I will get the expected output:
string test 1
string test 2
integer test 1
integer test 2
Any idea why my previous code only have the PostConstruct invoked for the first 2 beans?

How to make Hibernate use setFixedCHAR instead of setString

Can I somehow modify the way Hibernate binds parameters to the query?
For example, I want hibernate to use OracleResultSet.setFixedChar() when executing on an string column, instead of rs.setString() when executing a JPA query via Spring data.
This is how I would do it without Hibernate:
try(PreparedStatement ps = con.executeQuery("...")) {
if(ps.isWrapped(OraclePreparedStatement.class) {
ps.unwrap(OraclePreparedStatement.class).setFixedCHAR(0, myStringField);
} else {
ps.setString(0, myStringField);
}
try(ResultSet rs = ps.getResultSet()) {
while(rs.next()) {
... do stuff ...
}
}
}
Repository method (Spring data JPA):
List<Object> findByMyStringField(String myStringField);
How can I influence how Hibernate binds my variable. With the above example setString is used always.
As background: the problem is that all our Legacy DB's use CHAR columns and not VARCHAR2, so we have to deal with whitespace and setFixedCHAR should do exactly what we would want.
Found a solution by implementing a SqlTypeDescriptor & Custom Dialect:
#Autowired
private DataSource source;
#Bean
public HibernateJpaVendorAdapter getHibernateJPAVendorAdapter() {
return new CustomHibernateJpaVendorAdaptor();
}
private static class CustomHibernateJpaVendorAdaptor extends HibernateJpaVendorAdapter {
#Override
protected Class<?> determineDatabaseDialectClass(Database database) {
// if HSQL is copied from Spring Sourcecode to keep everything the same
if (Database.HSQL.equals(database)) {
return CustomHsqlDialect.class;
}
try {
if (source.isWrapperFor(OracleDataSource.class)) {
return CustomOracleDialect.class;
}
} catch (SQLException e) {
}
return super.determineDatabaseDialectClass(database);
}
private class CustomHsqlDialect extends HSQLDialect {
public CustomHsqlDialect() {
registerColumnType(Types.BOOLEAN, "boolean");
registerHibernateType(Types.BOOLEAN, "boolean");
}
}
}
#NoArgsConstructor
public static class CustomOracleDialect extends Oracle12cDialect {
private static final OracleCharFix INSTANCE = new OracleCharFix();
#Override
protected SqlTypeDescriptor getSqlTypeDescriptorOverride(final int sqlCode) {
switch (sqlCode) {
case Types.VARCHAR:
return INSTANCE;
default:
return super.getSqlTypeDescriptorOverride(sqlCode);
}
}
}
#Slf4j
private static class OracleCharFix extends CharTypeDescriptor {
#Override
public <X> ValueBinder<X> getBinder(final JavaTypeDescriptor<X> javaTypeDescriptor) {
return new BasicBinder<>(javaTypeDescriptor, this) {
#Override
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options)
throws SQLException {
if (st.isWrapperFor(OraclePreparedStatement.class)) {
OraclePreparedStatement ops = st.unwrap(OraclePreparedStatement.class);
if (ops.getParameterMetaData().getParameterType(index) == Types.CHAR) {
ops.setFixedCHAR(index, javaTypeDescriptor.unwrap(value, String.class, options));
} else {
st.setString(index, javaTypeDescriptor.unwrap(value, String.class, options));
}
} else {
st.setString(index, javaTypeDescriptor.unwrap(value, String.class, options));
}
}
#Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
//Is nolonger used by Hibernate in the current Version
st.setString(name, javaTypeDescriptor.unwrap(value, String.class, options));
}
private boolean checkIfCHARByName(ResultSetMetaData metadata, String name)
throws SQLException {
for (int i = 1; i <= metadata.getColumnCount(); i++) {
if (metadata.getColumnType(i) == Types.CHAR && Objects.equals(metadata.getColumnName(i), name)) {
return true;
}
}
return false;
}
};
}

PostSharp - args.ReturnValye = default(T) -> T = method return type, how?

My aspect:
[Serializable]
class FlowController : OnMethodBoundaryAspect
{
[ThreadStatic]
private static bool logging;
public override void OnEntry(MethodExecutionArgs args)
{
if (logging)
return;
try
{
logging = true;
if (ProgramState.State() == false)
{
args.ReturnValue = ""; // WHAT DO I SET HERE?
args.FlowBehavior = FlowBehavior.Return;
}
}
finally
{
logging = false;
}
}
}
Basically the ProgramState.State() method checks if the program is running(true),paused(loops while isPaused == true), stopped(false), this should control the if methods can run or not(basically a start pause/resume stop thing)
But sometimes i get nullreferences when returning from the method.
i am interested in knowing how can i set the return type to the default return type of the method.
It is tested with PostSharp 6.0.29
Before use it please check required null controls.
If method is async Task
public override void OnException(MethodExecutionArgs args)
{
var methodReturnType = ((System.Reflection.MethodInfo)args.Method).ReturnType;
var runtime = methodReturnType.GetRuntimeFields().FirstOrDefault(f => f.Name.Equals("m_result"));
//Only if return type has parameterless constructture (should be check before create)
var returnValue = Activator.CreateInstance(runtime.FieldType);
args.ReturnValue = returnValue;
}
And if method is not async
public override void OnException(MethodExecutionArgs args)
{
var methodReturnType = ((System.Reflection.MethodInfo)args.Method).ReturnType;
//Only if return type has parameterless constructture (should be check before create)
var returnValue = Activator.CreateInstance(methodReturnType);
args.ReturnValue = returnValue;
}
You can make your aspect class generic with the generic parameter representing the method return type. Then you need to create a method-level attribute that is also an aspect provider. The attribute will be applied to the user code and in turn it can provide the correct instance of the generic aspect.
[Serializable]
[MulticastAttributeUsage( MulticastTargets.Method )]
public class FlowControllerAttribute : MethodLevelAspect, IAspectProvider
{
public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
{
MethodInfo method = (MethodInfo) targetElement;
Type returnType = method.ReturnType == typeof(void)
? typeof(object)
: method.ReturnType;
IAspect aspect = (IAspect) Activator.CreateInstance(typeof(FlowControllerAspect<>).MakeGenericType(returnType));
yield return new AspectInstance(targetElement, aspect);
}
}
[Serializable]
public class FlowControllerAspect<T> : IOnMethodBoundaryAspect
{
public void RuntimeInitialize(MethodBase method)
{
}
public void OnEntry(MethodExecutionArgs args)
{
args.ReturnValue = default(T);
args.FlowBehavior = FlowBehavior.Return;
}
public void OnExit(MethodExecutionArgs args)
{
}
public void OnSuccess(MethodExecutionArgs args)
{
}
public void OnException(MethodExecutionArgs args)
{
}
}
// Usage:
[FlowController]
public int Method()
{
// ...
}

Spring Mvc with Thread

Hi My thread class is showing null pointer exception please help me to resolve
#Component
public class AlertsToProfile extends Thread {
public final Map<Integer, List<String>> userMessages = Collections.synchronizedMap(new HashMap<Integer, List<String>>());
#Autowired
ProfileDAO profileDAO;
private String categoryType;
private String dataMessage;
public String getCategoryType() {
return categoryType;
}
public void setCategoryType(String categoryType) {
this.categoryType = categoryType;
}
public String getDataMessage() {
return dataMessage;
}
public void setDataMessage(String dataMessage) {
this.dataMessage = dataMessage;
}
public void run() {
String category=getCategoryType();
String data= getDataMessage();
List<Profile> all = profileDAO.findAll();
if (all != null) {
if (category == "All" || category.equalsIgnoreCase("All")) {
for (Profile profile : all) {
List<String> list = userMessages.get(profile.getId());
if (list == null ) {
ArrayList<String> strings = new ArrayList<String>();
strings.add(data);
userMessages.put(profile.getId(), strings);
} else {
list.add(data);
}
}
}
}
}
and my service method is as follows
#Service
public class NoteManager
{
#Autowired AlertsToProfile alertsToProfile;
public void addNote(String type, String message, String category) {
alertsToProfile.setCategoryType(category);
String data = type + "," + message;
alertsToProfile.setDataMessage(data);
alertsToProfile.start();
System.out.println("addNotes is done");
}
But when i call start() method am getting null pointer exception please help me. I am new to spring with thread concept
It pretty obvious: you instantiate your thread directly, as opposed to letting spring create AlertsToProfile and auto wire your instance.
To fix this, create a Runnable around your run() method and embed that into a method, something like this:
public void startThread() {
new Thread(new Runnable() {
#Override
public void run() {
// your code in here
}}).start();
}
you will want to bind the Thread instance to a field in AlertsToProfile in order to avoid leaks and stop the thread when you're done.

Factory Pattern: Enum Parameter vs Explicit Method Name?

Say you have a factory that returns instances of ILightBulb. Two ways (there may be more) of implementing the factory are as follows:
Option 1 - Passing in an enum type
enum LightBulbType
{
Incandescent,
Halogen,
Led,
}
class ILightBulbFactory
{
public ILightBulb Create(LightBulbType type)
{
switch (type)
{
case LightBulbType.Incandescent:
return new IncandescentBulb();
case LightBulbType.Halogen:
return new HalogenBulb();
case LightBulbType.Led:
return new LedBulb();
}
}
}
Option 2 - Explicit method names
class ILightBulbFactory
{
public ILightBulb CreateIncandescent()
{
return new IncandescentBulb();
}
public ILightBulb CreateHalogen()
{
return new HalogenBulb();
}
public ILightBulb CreateLed()
{
return new LedBulb();
}
}
Which method is most preferable, and why?
Thanks.
In java, you can solve it in the enum, thus making sure that if you add new enums, all your code will remain working, instead of forgetting to add statements to your case.
enum LightBulbType
{
Incandescent{
#Override
public ILightBulb getInstance() {
return new IncandescentBulb();
}
},
Halogen{
#Override
public ILightBulb getInstance() {
return new HalogenBulb();
}
},
Led{
#Override
public ILightBulb getInstance() {
return new LedBulb();
}
};
public abstract ILightBulb getInstance();
}
class ILightBulbFactory
{
public ILightBulb Create(LightBulbType type)
{
return type.getInstance();
}
}
The equivalent for c# would be
public sealed class LightBulbType
{
public static readonly LightBulbType Incandescent = new
LightBulbType("Incandescent", new IncandescentBulb());
public static readonly LightBulbType Halogen = new
LightBulbType("Halogen", new HalogenBulb());
public static readonly LightBulbType LedBulb = new LightBulbType("Led",
new LedBulb());
public static IEnumerable<LightBulbType> Values
{
get
{
yield return Incandescent;
yield return Halogen;
yield return LedBulb;
}
}
private string Name { get; set; }
private ILightBulb Instance { get;}
private LightBulbType(string name, ILightBulb instance)
{
this.Name = name;
this.Instance = instance;
}
public override string ToString() => Name;
public ILightBulb GetInstance()
{
return Instance;
}
}
public interface ILightBulb
{
}
public class IncandescentBulb : ILightBulb
{
}
public class HalogenBulb : ILightBulb
{
}
public class LedBulb : ILightBulb
{
}
public static class LightBulbFactory
{
public static ILightBulb Create(LightBulbType type)
{
return type.GetInstance();
}
}
For call
ILightBulb halogenBulb = new HalogenBulb();
ILightBulb incandescentBulb = new IncandescentBulb();
ILightBulb ledBulb = new LedBulb();
or
ILightBulb halo = LightBulbFactory.Create(LightBulbType.Halogen);
ILightBulb inca = LightBulbFactory.Create(LightBulbType.Incandescent);
ILightBulb led = LightBulbFactory.Create(LightBulbType.LedBulb);

Resources