Best way to inject component in Spring with Kotlin [closed] - spring

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
For inject a beans with Spring/Kotlin, I know two ways :
Passing it into the constructor :
#Service
open class MyService #Autowired constructor(
#Autowired
val myRepository: MyRepository
)
Using the 'lateinit' keyword :
#Service
open class MyService {
#Autowired
lateinit var myRepository: MyRepository
}
I know the two works, but i'd like to know which one is the best ? Is there some problem I can encounter with one solution and not the other ?
Thank you !

I prefer constructor. Spring no longer requires The #Autowired annotation if there is only one constructor. This way you don't have to make the class open (you do for some Spring things, like #Scheduled but that's another question) or use a var. It's also pretty easy to read.
This is all you need
#Service
class MyService (private val myRepository: MyRepository)

Related

Spring #Autowire on method [duplicate]

This question already has answers here:
#autowired on method in Spring
(4 answers)
Closed 1 year ago.
I've seen in many example the use of #Autowire in spring on a method.
For example in configuration file:
#Component
public class SomeConfigFile{
#Autowire
public void someMethod(SomeBeanInstance someBean){
//bla bla
}
I guess someBean in my example above injected to the method by Spring but when does this method is called? and how calls it?
Marks a constructor, field, setter method, or config method as to be
autowired by Spring's dependency injection facilities. This is an
alternative to the JSR-330 Inject annotation, adding
required-vs-optional semantics.
see https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/annotation/Autowired.html

Got lateinit property has not been initialized error when using #Autowired and #Component

This AwsSqsUtil class has a Component annotation, when I want to use it in my main function, I got error "kotlin.UninitializedPropertyAccessException: lateinit property awsSqsUtil has not been initialized, how to fix this issue?
"
#Component
class AwsSqsUtil {
fun sendMessageToSqs() {
}
}
#Autowired
private lateinit var awsSqsUtil: AwsSqsUtil
awsSqsUtil.sendMessageToSqs()
Where exactly is your variable declaration? Is it inside a class that is a component/service/controller/etc, or just on top level?
I expect your answer to be top lever or a normal class, since that is when you'd usually encounter this.
And the solution would be to leave the main class alone in a spring boot app. If you want something to run at start-up, you can create a fun (inside a proper spring container) that you annotate with #PostConstruct. This way it will run after the spring app has actually started, and you'll be able to autowire at will.

Spring security filter by authenticated user [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
What is in Spring Boot the best way to filter all queries of entity adding the authenticated user check (using the Authentication Name field)?
Adding a where hibernate on the entity? Is it possible?
Adding Spring Security postFilter? I didn't understand if I should add it to my repository or to my service. Isn't it an inefficient system (than executing a filtered query)?
EDIT
No, I'm not using Spring-Data, but I can add it to my project.
For example, if my repository is:
public interface MyRepository extends JpaRepository<MyEntity, Long>, QuerydslPredicateExecutor<MyEntity>, QuerydslBinderCustomizer<QMyEntity> {
...
}
I have to override all methods of the classes of JpaRepository and QuerydslPredicateExecutor and QuerydslPredicateExecutor that I use in my project? It's correct?
The postFilter is not an efficient option since it would result in fetching many data from the database, but throwing most of them away.
If you're using Spring Data JPA, then you can use the Spring Security data integration and do something like this to include the authentication name in the query (automatically fetched from the SecurityContext).
#Repository
public interface BillingRepository extends JpaRepository<Billing,Long> {
#Query("select b from Billing b where b.userId = ?#{ authentication?.name }")
Ierable<Billing> findBillings();
}

How to upload images to a database with other details using postman [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to upload a image with other details like employee id, employee name etc using spring boot and spring data jpa, trying to send request using postman.I have searched a lot but I cannot find any examples sending both image and other details in one method, as I am a fresher unable to find exact solution and also I don't want to use ObjectMapper to read values.Can some one help me.
Thanks in advance
Check this Example:
#PostMapping("/test")
public Object getConsumedUser(
#Valid User user,
#RequestParam("file") MultipartFile file) {
return file;
}
-----------
class User {
private String name;
private String username;
//Getter and Setter
...
}

Spring #autowired annotation example [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Can someone explain spring #autowired example which is given in the below tutorials point link?
Whatever id I give in the place of bean id which is "spellChecker" it takes that and injects it. for example instead of bean id="spellChecker" if I give bean id="a" in Beans.xml
Spring auto-wired annotation
Here are some point for autowire functionality and how it is works interally,
1) If your application has two bean for same class then it would not works.for that you have to give same bean name as you given in bean.xml file.
like one bean is -> id = "spellChecker1"
second bean is -> id = "spellChecker2"
now you have to autowired like
bean 1 -> #autowired
private SpellChecker spellChecker1;
bean 2 -> #autowired
private SpellChecker spellChecker2;
2) if your application has only one bean for class then it would automatically detect bean and inject.
In your case application has only one bean id="a" so spring automatically detect that bean for class SpellChecker.

Resources