Spring Boot external configuration in Groovy - groovy

How to get Spring boot to load external properties for Groovy?
Need something similar to java mechanism (application.properties in resources and ConfigBean with #Value annotations)?
When trying to use the same mechanism as with java, I don't know how to annotate the ConfigBean
#Component
public class ConfigBean {
#Value("${seleniumAddress}")
private String seleniumAddress; ...
and then in application.properties
seleniumAddress=http://localhost:4444/wd/hub
but with groovy I cannot annotate the field with #Value("${seleniumAddress}"
It throws an error complaining about "${}" - this is a special sequence in groovy.
So what mechanism should I use here?
Thank you

If you use "${}" for Spring placeholders in Groovy you have to make sure it's a String (not a GString): i.e. use '${}' (single quotes).

Related

Groovy test class using #ContextHierarchy

I am using Groovy for my Spring application and I try to use multiple XML beans configurations inside my test. I tried using #ContextHierarchy but following usage example didn't work:
#RunWith(SpringRunner)
#SpringBootTest
#ContextHierarchy({#ContextConfiguration("a.xml"), ContextConfiguration("b.xml")})
public class MyTest {
...
}
I have also tried:
#ContextConfiguration(locations={"a.xml", "b.xml"})
but it didn't work as well.
As I understand Groovy doesn't like the "{" "}", because it has different mean to it....?
How can I write the Testclass in groovy with two conifiguration xmls defined?
You can define multiple XML configuration sources with #ContextConfiguration annotation. Let's say I have 2 XML config files located in src/main/resources - beans1.xml and beans2.xml. I can use them in my test with:
#ContextConfiguration(locations = ['classpath:beans1.xml', 'classpath:beans2.xml'])
The main difference comparing to Java is that Groovy uses [] for arrays instead of Java's {}, because {} represents Groovy's closure.

How do I know at runtime which implementing classes are set for my Spring beans?

In hybris, is there an easy way to know which implementing class is being used for a certain Spring bean?
I mean, I can override a Bean by doing something like this:
<alias name="myCheckoutFacade" alias="checkoutFacade"/>
<bean id="myCheckoutFacade" class="com.pedra.facades.checkout.impl.MyCheckoutFacadeImpl" scope="tenant" parent="defaultCheckoutFacade">
<property name="commerceCheckoutService" ref="myCommerceCheckoutService"/>
</bean>
... so now when Spring needs to create a bean with the alias checkoutFacade the implementing class will be MyCheckoutFacadeImpl as opposed to the overridden defaultCheckoutFacade which was defined in some other xml configuration file.
So is there a way to know at runtime which implementing class is being used for a certain Spring bean definition? Without having to debug the code, I mean.
Beanshell or Groovy :-)
Checking the implementing class of a bean is just one of the many cool things you can do at runtime with Beanshell or Groovy.
Disclaimer: Be careful running Beanshell or Groovy code on a production machine!
Log in to the HAC and go to Console > Beanshell or Groovy
Execute the following code in either Beanshell or Groovy to get your implementing class:
de.hybris.platform.core.Registry.getApplicationContext().getBean("checkoutFacade");
Both consoles will show the result of the last expression in the Result tab.
In the Groovy console for Hybris 5.x, simple execute the following:
checkoutFacade
As you can see, each bean is automatically def-ed into each Groovy script.
As for Beanshell, you could create a bean function in Beanshell:
import de.hybris.platform.core.Registry;
import de.hybris.platform.commercefacades.order.CheckoutFacade;
Object bean(String beanName)
{
return Registry.getApplicationContext().getBean(beanName);
}
CheckoutFacade checkoutFacade = (CheckoutFacade) bean("checkoutFacade");
print(checkoutFacade);
I ended up using Beanshell so much that I created my own wrapper application that allows me to develop Beanshell in Eclipse, and use Eclipse as the Beanshell console. But that's a whole other post!
Resources:
Beanshell User Manual
Beanshell Commands Documentation (Built-in functions like print())

Add constraints to properties of Groovy class (not Grails domain class!)

How can we add some common constraints (i.e. maxLength, nullable) to a property of a Groovy class? I know we can do it at Grails domain class, but is it possible if that is a Groovy class (I use it as a DTO class for my Grails project)?
Thank you so much!
You can add constraints to command classes. If a command class is in the same .groovy file as a controller (in Groovy you can have more than one public class in each .groovy file), you don't need to do anything special for Grails to recongise it as a command class.
However, if your command class is somewhere else (e.g. under src/groovy), you need to annotate it with #Validateable and add the package name to the grails.validateable.packages parameter in Config.groovy. Here's an example of a command that's not in the same file as a controller
pacakge com.example.command
#Validateable
class Person {
Integer age
String name
static constraints = {
name(blank: false)
age(size 0..100)
}
}
Add the following to Config.groovy
grails.validateable.packages = ['com.example.command']
Command classes have a validate() method added by Grails. After this method is called, any errors will be available in the errors property (as per domain classes).
Using a grails Command Object is probably your best bet. It has constraints and validation, but no database backing. It's normally a value object that controllers use, but you could instantiate one outside of a controller without any problems.
Not sure if this is relevant to your use (I am not familiar with DTOs), but in the current version (2.3.8), you can also add Grails constraints to an abstract class, and they will be inherited by the domains that extend it. Your IDE might not like it though ;)

In J2ME ,how to invoke/call a method by its name?

Is it possible in J2ME to call/invoke a method by its name.just like we have getDeclaredMethod in java .
The java.lang.reflect package is available only in CDC 1.1.2. If you're not on such configuration, you're out of luck. There is no way to invoke a method by name without reflection.
A workaround would be to create a map from strings (method names) to appropriate classes on which you can invoke the methods.
Closest you can get is to instantiate a class by name using Class.forName("com.class.ClassName").newInstance() -- that will execute a parameterless constructor.

Inspect Groovy object properties with Java reflection

I have an Expando class which I need to inspect its properties from Java.
In Groovy:
def worker = new Expando()
worker.name = "John"
worker.surname = "Doe"
In Java:
Introspector.getBeanInfo(groovyObject.getClass())
Is it possible to compile at runtime the class from the object in Groovy?
The Expando is completely dynamic. It does not generate any bytecode getters or setters and therefore cannot be used as a JavaBean. What do you need to use the bean introspector for? You may be able to implement that logic using the expando directly if you write it in Groovy.
You might try the JSR 223 / Script engine with Groovy (example here) if you are using Java 6. It allows you to evaluate Groovy code from Java.
Depending on the location/definition of the Expando, you might be able to get its properties by evaluating getProperties() (as of Groovy 1.7).

Resources