Implementing functions like #LocalPort in Spring Boot - spring

I am contributing some framework and I am integrating it with Spring Boot. I want to create an annotation like #LocalPort that I made to use in testing. 😎
I found #LocalPort to be #Value in the end, but I don't think I can't use it because the annotation I'm making needs to be passed another value. In the end I think I should inherit ContextAnnotationAutowireCandidateResolver and either reimplement the getSuggestedValue(...) method or find another way. But I don't think this is the right way.
Is there another way to implement this? 🤔
For example,
#LocalSomePort(Protocol.HTTP) // Protocol.HTTPS

It was late, but I solved it by adding a custom BeanPostProcessor.

Related

Is there a way to define routes in a YAML file for a Spring project?

I've been working a lot with Symfony recently, and I really like the ability to define routes in a routing.yml file. I was looking into Spring's routing system and I couldn't find any options other than placing routes in annotations on controller methods. Is it possible to accomplish something like this in Spring?
My first thought was creating an abstract controller that grabs the routes from a .yml file, but that seemed a bit hacky.
EDIT:
For some added context, I am looking to build a simple Database API with Spring. After some digging it looks like the routing.yml file is best suited for working with server-rendered pages, which is not what I aim to do with my Spring project.
Symfony and Spring are different framework. You are used to one use and want to use same it in another entirely different system. It will not work. You have to adapt to frameworks.
Spring will scan your project and collect your specific annotation like Controller/Component/Configuration/... and configures itself. Therefore, there is no need predefined project structure, unlike, for example, Laravel. So, you can define this structure if you want. Or every class can be in one package, just not beautiful.
Back to routing. You can configure them by the value of annotations only. This is interpreted at compile time. (Ofc, there are runtime annotations, but I focused parameters of annotation.) So, you can not use configuration from the file because it is already runtime. So, you should use constants or hardcode.
Or, you can use an alternative: Annotate the interfaces, then the controllers will be the implementations.
Alternative #2: If you use Spring with Kotlin, In Kotlin, you can have several classes or interfaces in one file.

#WithMockUser and kotlin test

I'm a big fan of Kotlintest syntax and I'd like to know if it's possible to make it work with WebMvcTest. More particularly, I don't manage to annotate the test methods with #WithMockUser.
Does somebody know if it's feasible?
Thanks beforehand.
It's not something that is currently built into KotlinTest but there is a Spring module, so it should be fairly easy to create another extension that provides this.
This is the source for the spring module.
https://github.com/kotlintest/kotlintest/tree/master/kotlintest-samples/kotlintest-samples-spring

Spring-Boot Custom Repository

I have been reading about Spring Boot custom repository. I have dozens of blogs explaining how to implement those but none of them explained scenario when we actually need it?
I mean one example where we cannot live without custom repo. I mean if there is case of complex query, we can anyhow achieve it using #Query.
Please explain.
Lets say I want strongly typed query instead of #Query. I would create a custom repo, autowire EntityManager and use QueryDSL with it so I can use strongly typed references.
You can use it to extend the repository with other libraries that aren't part of Spring.
I find them useful when working with a program generator like jHipster. They keep your code separate from the generated code.
The xxxRepositoryCustom.java xxxRepositoryImpl will not be overwritten when the entities are re-generated by a dumb programmer (me.) The queries themselves have some complex logic that can not be expressed in a simple #Query

Adding behaviour to single repositories which uses base repository methods

I want to add custom behavior to single repository as described in 1.4.1 Adding behaviour to single repositories. In this custom behavior I want to use method already present in my base repository (like save() or findOne() method). Ho can I achieve this in Spring data?
I tried extending my UserRepositoryImpl with SimpleJpaRepository to have basic SimpleJpaRepository methods available. But this way I got instantiation problems.
Also using aproach described in 1.4.2 Adding custom behaviour to all repositories doesn't seem like good solution, because it's way too much code for adding one simple method. In this case Spring Data seems more like burden.
Or is it antipatern to add to my repository such simple method which depends on other methods from base repository? Should I rather move this method to service/business layer?
The same question came up a few days ago in this post. As I wrote there as well, I strongly believe this is an antipattern, so my answer to your last question would be a definite yes.

Seems like struts validate method doesn't work anymore when using spring

I am now moving to Struts for my presentation layer. I have done a simple example using Struts alone (not really, actually, I test an example inside a simple maven project). From now on, I am likely to use Struts with Spring so I try to migrate my simple application, my goal was to use service layer to deal with specific operation.
In my simple test, I extend action from Struts action, now that I am using Spring, I extend it from ActionSupport so that I can fully use spring injection. Now it seems like even though I override validate method, it is no longer called. Is that the right behaviour, if so, where should perform operations like checking if my mandatory fields are populated (inside action or service ?)
Thanks for your answer !

Resources