Scala + Spring boot #Profile annotation - spring-boot

I'm trying to use profiles in my Scala application which is using spring boot framework.
I only added this annotation to my class
#Profile("config.local")
class LocalConfig with Config
However the compiler says it expects an array of String, but trying to make it work with Array, Seq, etc it is saying it expect an String there.
This is the error sbt shows:
Error:(16, 10) type mismatch; found : String("config.local")
required: Array[String] #Profile("config.local")
How can I use spring boot #Profile with Scala classes
Thanks

#Profile(Array("config.local"))
will work for you, but it may cause errors in the IDE although it compiles successfully.

Related

kotlin-reflect with proguard causes reflection errors

I'm using ProGuard for my spring boot application code. After I upgraded to Spring Boot 2, I cannot start my application anymore.
Spring Boot 2 uses kotlin-reflect to create beans, which uses kotlin.Metadata annotation during reflection. This annotation has unobfuscated values and therefore kotlin-reflect is looking for methods with original names. and following exception is thrown:
kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Could not compute caller for function: public constructor ProjectService(...
ProjectService is obfuscated to F, hence no such constructor.
When I keep class names, I have same problem elsewhere:
kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Could not compute caller for function: public open fun addRole(...
Is there a way to fix the obfuscation of kotlin.Metadata annotation parameters? Annotation itself is not obfuscated and it still refers to original class names which are written as string values. I also tried to obfuscate the Metadata annotation to no avail.

Spring AOP aspect doesn't get applied if included from an external jar with different package name

I have a spring boot rest service that included an external project in pom as it's dependency. That external project is basically a jar that has spring AOP code.
The base package in my main application that includes this external jar with spring AOP code is x.y.z
The class in external jar where the #before advice is, is under the package a.b.c
With this class under a.b.c package, it doesn't get recognized by the main application where I want to use the spring aop implementation and apply the aspect. However, when I change it's package from a.b.c to x.y.z (which I really can't do in real life) it works fine.
I know that in spring boot service which happens to be the including service, it scans everything under root package given in the application class, x.y.z in this case and that is why aspect works fine if it's class is under x.y.z.
however, the problem is that this spring app jar will be used across multiple applications. So changing package name like this is not an option.
Is there a way to accomplish this without changing the package name of the class where spring app code is ?
Probably component scan is only activated for your application class packages by default. You can extend it to multiple packages, including the aspect package:
XML style configuration:
<context:component-scan base-package="x.y.z, a.b.c" />
Annotation style configuration:
#ComponentScan(basePackages = {"x.y.z", "a.b.c"})
Disclaimer: I am not a Spring user, only an AspectJ expert. I just knew that you can configure component scan, googled the syntax for you and hope it is correct.
Please define the bean (of jar project )inside main application. Give the #ComponentScan(basePackages = {"x.y.z", "a.b.c"}) as well as #EnableAspectJAutoProxy. Also include below piece of code.
ex:
` #Bean
public LoggingHandler loggingHandler()
{
return new LoggingHandler();
}`
Also annotate external jar code with:
`#Aspect
#Component
public class LoggingHandler {`
What #kriegaex suggests is correct. In addition to that, please make sure you are using #Component along with #Aspect. Since #Aspect is not a Spring annotation, Spring won't recognize it and hence your aspect won't be registered. So, using #Component is mandatory to getting aspects to work in Spring environment.

Error when trying to use both mongo and sql jpa in the same spring app

We have a spring app that works with mongodb.
Now we need it to connect also to mysql.
All the beans are defined in a applicationContext.xml legacy file.
We like that the new mysql configuration will be in java classes.
We created an entity, repository and a configuraion java files.
But it seems that as we try to do so, spring gets confused.
If we try to run the app, it start complaining about the mongo repositories:
Error creating bean with name 'MyMongoRepository': Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities
In the intellij we have the "could not autowire" error only on the sql repository (the first 3 are the mongo repositories):
Is it possible that the #configuration class is clashing with the applicationContext?
Should the #configuraion class be in a certain package/folder to work correctly?
Also, in the #configuration file, as there are green beans on the left side, it seems like intellij is able to understand where is the persistence repository.
Thanks for any help.

What's the syntax for multiple dependency injection using Spring and Scala?

I'm creating a new application using Akka, Scala and Spring. However all the examples I found online show a single dependency being injected. Since the syntax is different from Java, could anyone tell me what'd it look like with multiple DI?
Spring 4 MVC with Scala
akka-scala-spring
Converting a Java Spring application to Scala
Example of single DI:
class HelloWorldController #Autowired() (nameService: Name) {
I would start by simply adding parameters to the autowired argument list:
class HelloWorldController #Autowired() (service1: Foo, service2: Bar)
Unfortunately, I haven't used Spring in Scala yet, but if #Autowired is anything like Google Guice's #Inject - and it looks pretty similar - then this should be all you need in your class file.

WAS 6.1 JDK & Spring Autowiring

By default, autowiring happens by propertyname. It seems if I compile my application with the WAS 6.1 JDK, spring cannot autowire my dependencies by name and then instead reverts to type which is causing an issue because of some ambiguous Validator references in my controllers. The controller properties are all of type Validator and there are 9 validators defined in the context file so spring complains that it finds too many matches. I know one way around this would be to use the actual implementation class as the type for the validator instances in my controller classes, but I want to know if anyone else has encountered this problem with autowiring by name failing when compiled with the WAS 6.1 jdk.

Resources