What If I do not add #Document annotation - spring

I forgot to add #Document annotation in one of class of spring-mongo application.
Currently the code is working fine. Is there anything I need to worry about.

Related

What class implements the spring framework Autowired

I downloaded the spring-framework project, because I want to see how #Autowired is implemented.
So, I got to this file, which is an interface.
But when I want in Intellij to go to its implementation, no implementations are found.
So is this interface not implemented?
Then where is the code for #Autowired?
Well, this is not an interface it is actually an annotation.
In java #inteface is used to create an annotation.
Once the annotation is created, you can use that annotation on fields, classes, methods (based on what is specified in #Target of the annotation definition.
Spring does package scanning and finds all the things which are using a particular annotation and does the required processing.
Use this article to undestand more in How an annotation is created, used and the how the annotation processor finds and processes the annotation.
#Autowired doesn't really have much code, so to speak. It's just an annotation which is a Java type of interface that provides instructions to other parts of the codebase.
#Autowired is only an annotation or you can say a "marker". Spring use reflection to identify annotation and do something about that annotated thing. For example with #Autowired, when spring found it, spring will inject the annotated property with eligible bean.

#ContextConfiguration and Security Chain Filter

I'm trying to implement integration testing in my app and have test class like that:
#ExtendWith(value={SpringExtension.class})
#WebAppConfiguration
#ContextConfiguration(classes={AppConfiguration.class,WebMvcConfiguration.class})
public class TestClass{ ... }
inside class i have:
mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(SecurityMockMvcConfigurers.springSecurity()).build();
Sadly #Autowired cannot find my Security Spring Chain Filter (or DelegatingFilterProxy) and without it I feel like I won't be able to achievie full functionality of integration testing in spring (espescially the spring-security-test part). In my opinion it's because I initialize my whole app by creating a class that extends AbstractHttpSessionApplicationInitializer. I also feel like adding some sort of custom initializer or loader classes into #ContextConfiguration would help, but acutally have no idea how to do it. Do you guys think it will help and/or can you guys steer me into creating my own loader/initializer?

What is a spring service annotation? [duplicate]

This question already has answers here:
What's the difference between #Component, #Repository & #Service annotations in Spring?
(31 answers)
Closed 5 years ago.
I understand that #Service is used to mark a class as a business logic class. Why is it useful for spring? I can already use #Component to mark a class as a bean. I understand that #Service is more specific than #Component, but how?
Consider #Component annotation as a Swiss Knife. It can act as cutting knife, as an opener, as a scissor, etc.
Similarly, your Component can act as a Repository, as Business Logic class or as a controller.
Now, #Service is a just one of the versions of #Component, say a knife.
Spring process #Service similar to #Component, since #Service interface itself is annotated with #Component.
From Spring docs.:
#Target(value=TYPE)
#Retention(value=RUNTIME)
#Documented
#Component
public #interface Service
Indicates that an annotated class is a "Service" (e.g. a business
service facade).
Why to differentiate both of them?
To apply the basic rule of programming: Your code should be easily readable.
Yes, you can use #Component annotation everywhere and it will work fine but for the better understanding of the code, it is preferred to use the respective special types of annotations like #Service in our case.
The other benefit is ease of debugging. Once you know the error, you need not hope from one component class to another, checking it time whether that class is service, repository or controller.
#Service, #Controller, #Repository = {#Component + some more special functionality}
Click the below link for more details
What's the difference between #Component, #Repository & #Service annotations in Spring?
The #Component annotation marks a java class as a bean so the
component-scanning mechanism of spring can pick it up and pull it into
the application context. The #Service annotation is also a
specialization of the component annotation. It doesn’t currently
provide any additional behavior over the #Component annotation, but
it’s a good idea to use #Service over #Component in service-layer
classes because it specifies intent better. Additionally, tool support
and additional behavior might rely on it in the future.
The #Service annotation is a specialization of the component annotation. It doesn’t currently provide any additional behavior over the #Component annotation, but it’s a good idea to use #Service over #Component in service-layer classes because it specifies intent better. Additionally, tool support and additional behavior might rely on it in the future.

where to place componentscan

I have spring mvc application with complete annotations. Where is the best place to add #componentScan? Let me know any of these recommended
class that extends AbstractAnnotationConfigDispatcherServletInitializer?
class that extend WebMvcConfigurationSupport
class that extend WebSecurityConfigurerAdapter
I placed in 2 without security and workign fine. When I added security, I got problem with security config not able to find userdetails service. Then I moved to 3.
I found other issues with security and put code to just reuturn null instead of
securityconfig object from getRootConfigClasses(). Then I got issues of controllers not found. I am able to fix it to put componenentscan in 2.
I just want to know any links and how it works. Is it ok to put #componentscan in all of these 3? Appreciate your help.
It depends on your project's package tree and what you want to scan. If you want to scan all annoted classes with such annotations like : #Configuration, #Component, #Repository, etc... put #ComponentScan at the top of your package tree.
You can also use the basePackages attribute to specify where to start the scanning.
Say you have an application packages organized like this :
com.app.config,com.app.config.web, com.app.services, com.app.web.controllers
If you want to scan all annoted classes, put the class annoted with #ComponentScan in com.app package.
If you want to scan only controller, add #ComponentScan(basePackages="com.app.web.controllers")
It's up to you to decide.

When to use Spring #Autowire annotation

Recently I had discussion with my friend regarding usage of Spring #Autowire annotation on entity(JPA) classes.
In our project we are using #Autowire annotaion to inject Entity but my friend suggesting not to use #Autowire annotaions on entity classes. When I asked why? He dont have the proper answer for that. So i just wanted to know are there any disadvantages using #Autowire annotaion on entity classes.
Also please explain when to go for #Autowire annotaion or not with example.
Thank in advance.
#Entity and #Autowire are not interchangeable.
#Entity annotation indicates that the JavaBean is a persistent entity.This is actually a JPA annotation and not a Spring Annotation.
#Entity will be used in the sessionFactory by the packagesToScan poroerty.
#Autowired: inject a resource by-type, i.e. by the class or by the interface of the annotated field or contractor. See my answer Inject and Resource and Autowired annotations
#Autowired is used to inject dependencies as an alternative to setting it via xml configurations
Maybe this answer will help you understand
Hibernate - spring annotated entities not scanned from within jar
UPDATE:
Following the comment bellow:
Company is your domain object, so you don't need to use spring in this case.
<bean id="company" class="xxx.Company"/>
The above will return the same instance with #autowire.
Even if you switch to scope="prototype" I don't see any reason to use spring for that.
You should have a service that will be used to CRUD company e.g.
CompanyService, this service will be a single tone so you will use #Autowire to inject it to the controller and it will use your JPA framework to implement CRUD's
To create a new company you will use:
Company c = new Company //this probably will be binded from your ui form
companyServic.saveOrUpdate(c);
See the following answer spring rest service - hibernate dao - annotations - pojo - namedqueries.
For common practice of DAO and services.
#Autowire is an annotation used to perform a dependency injection, its almost similar to the standard #Inject you can take a look at the spring reference manual to see the difference between those two annotations.
#Entity is a part of the jpa framework its used to mark a class as persistent, spring does not implement an equivalent annotation.

Resources