Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Is Java pass-by-reference or pass-by-value?
The terms "pass-by-value" and "pass-by-reference" have special, precisely defined meanings in computer science. These meanings differ from the intuition many people have when first hearing the terms. Much of the confusion in this discussion seems to come from this fact. The terms "pass-by-value" andRead more
The terms “pass-by-value” and “pass-by-reference” have special, precisely defined meanings in computer science. These meanings differ from the intuition many people have when first hearing the terms. Much of the confusion in this discussion seems to come from this fact.
The terms “pass-by-value” and “pass-by-reference” are talking about variables. Pass-by-value means that the value of a variable is passed to a function/method. Pass-by-reference means that a reference to that variable is passed to the function. The latter gives the function a way to change the contents of the variable.
By those definitions, Java is always pass-by-value. Unfortunately, when we deal with variables holding objects we are really dealing with object-handles called references which are passed-by-value as well. This terminology and semantics easily confuse many beginners.
It goes like this:
In this example,
aDog.getName()
will still return"Max"
. The valueaDog
withinmain
is not changed in the functionfoo
by creating newDog
with name member variable set to"Fifi"
because the object reference is passed by value. If the object reference was passed by reference, then theaDog.getName()
inmain
would return"Fifi"
after the call tofoo
.Likewise:
In this example,
See lessFifi
is dog’s name after call tofoo(aDog)
because the object’s name was set inside offoo(...)
. Any operations thatfoo
performs ond
are such that, for all practical purposes, they are performed onaDog
, but it is not possible to change the value of the variableaDog
itself.Find all files containing a specific text (string) on Linux?
Do the following: grep -rnw '/path/to/somewhere/' -e 'pattern' -r or -R is recursive, -n is line number, and -w stands for match the whole word. -l (lower-case L) can be added to just give the file name of matching files. -e is the pattern used during the search Along with these, --exclude, --includRead more
Do the following:
-r
or-R
is recursive,-n
is line number, and-w
stands for match the whole word.-l
(lower-case L) can be added to just give the file name of matching files.-e
is the pattern used during the searchAlong with these,
--exclude
,--include
,--exclude-dir
flags could be used for efficient searching:--exclude-dir
parameter. For example, this will exclude the dirsdir1/
,dir2/
and all of them matching*.dst/
:This works very well for me, to achieve almost the same purpose like yours.
See lessHow to remove a specific item from an array in JavaScript?
Find the index of the array element you want to remove using indexOf, and then remove that index with splice. The splice() method changes the contents of an array by removing existing elements and/or adding new elements. const array = [2, 5, 9]; console.log(array); const index = array.indexOf(5); ifRead more
Find the
index
of the array element you want to remove usingindexOf
, and then remove that index withsplice
.The second parameter of
splice
is the number of elements to remove. Note thatsplice
modifies the array in place and returns a new array containing the elements that have been removed.For completeness, here are functions. The first function removes only a single occurrence (e.g., removing the first match of
5
from[2,5,9,1,5,8,5]
), while the second function removes all occurrences:In TypeScript, these functions can stay type-safe with a type parameter:
See lessWhat is difference between CrudRepository and JpaRepository interfaces in Spring Data JPA?
Basics The base interface you choose for your repository has two main purposes. First, you allow the Spring Data repository infrastructure to find your interface and trigger the proxy creation so that you inject instances of the interface into clients. The second purpose is to pull in as much functiRead more
Basics
The base interface you choose for your repository has two main purposes. First, you allow the Spring Data repository infrastructure to find your interface and trigger the proxy creation so that you inject instances of the interface into clients. The second purpose is to pull in as much functionality as needed into the interface without having to declare extra methods.
The common interfaces
The Spring Data core library ships with two base interfaces that expose a dedicated set of functionalities:
CrudRepository
– CRUD methodsPagingAndSortingRepository
– methods for pagination and sorting (extendsCrudRepository
)Store-specific interfaces
The individual store modules (e.g. for JPA or MongoDB) expose store-specific extensions of these base interfaces to allow access to store-specific functionality like flushing or dedicated batching that take some store specifics into account. An example for this is
deleteInBatch(…)
ofJpaRepository
which is different fromdelete(…)
as it uses a query to delete the given entities which is more performant but comes with the side effect of not triggering the JPA-defined cascades (as the spec defines it).We generally recommend not to use these base interfaces as they expose the underlying persistence technology to the clients and thus tighten the coupling between them and the repository. Plus, you get a bit away from the original definition of a repository which is basically “a collection of entities”. So if you can, stay with
PagingAndSortingRepository
.Custom repository base interfaces
The downside of directly depending on one of the provided base interfaces is two-fold. Both of them might be considered as theoretical but I think they’re important to be aware of:
Page
orPageable
in your code anyway. Spring Data is not any different from any other general purpose library like commons-lang or Guava. As long as it provides reasonable benefit, it’s just fine.CrudRepository
, you expose a complete set of persistence method at once. This is probably fine in most circumstances as well but you might run into situations where you’d like to gain more fine-grained control over the methods expose, e.g. to create aReadOnlyRepository
that doesn’t include thesave(…)
anddelete(…)
methods ofCrudRepository
.The solution to both of these downsides is to craft your own base repository interface or even a set of them. In a lot of applications I have seen something like this:
The first repository interface is some general purpose base interface that actually only fixes point 1 but also ties the ID type to be
See lessLong
for consistency. The second interface usually has all thefind…(…)
methods copied fromCrudRepository
andPagingAndSortingRepository
but does not expose the manipulating ones. Read more on that approach in the reference documentation.What’s the difference between @Component, @Repository and @Service annotations in Spring?
We'll here focus on some minor differences among them. First the Similarity First point worth highlighting again is that with respect to scan-auto-detection and dependency injection for BeanDefinition all these annotations (viz., @Component, @Service, @Repository, @Controller) are the same. We can uRead more
We’ll here focus on some minor differences among them.
Differences between @Component, @Repository, @Controller and @Service
This is a general-purpose stereotype annotation indicating that the class is a spring component.
What’s special about @Component
<context:component-scan>
only scans@Component
and does not look for@Controller
,@Service
and@Repository
in general. They are scanned because they themselves are annotated with@Component
.Just take a look at
@Controller
,@Service
and@Repository
annotation definitions:Thus, it’s not wrong to say that
@Controller
,@Service
and@Repository
are special types of@Component
annotation.<context:component-scan>
picks them up and registers their following classes as beans, just as if they were annotated with@Component
.Special type annotations are also scanned, because they themselves are annotated with
@Component
annotation, which means they are also@Component
s. If we define our own custom annotation and annotate it with@Component
, it will also get scanned with<context:component-scan>
This is to indicate that the class defines a data repository.
What’s special about @Repository?
In addition to pointing out, that this is an Annotation based Configuration,
@Repository
’s job is to catch platform specific exceptions and re-throw them as one of Spring’s unified unchecked exception. For this, we’re provided withPersistenceExceptionTranslationPostProcessor
, that we are required to add in our Spring’s application context like this:This bean post processor adds an advisor to any bean that’s annotated with
@Repository
so that any platform-specific exceptions are caught and then re-thrown as one of Spring’s unchecked data access exceptions.The
@Controller
annotation indicates that a particular class serves the role of a controller. The@Controller
annotation acts as a stereotype for the annotated class, indicating its role.What’s special about @Controller?
We cannot switch this annotation with any other like
@Service
or@Repository
, even though they look same. The dispatcher scans the classes annotated with@Controller
and detects methods annotated with@RequestMapping
annotations within them. We can use@RequestMapping
on/in only those methods whose classes are annotated with@Controller
and it will NOT work with@Component
,@Service
,@Repository
etc…Note: If a class is already registered as a bean through any alternate method, like through
@Bean
or through@Component
,@Service
etc… annotations, then@RequestMapping
can be picked if the class is also annotated with@RequestMapping
annotation. But that’s a different scenario.@Service
beans hold the business logic and call methods in the repository layer.What’s special about @Service?
Apart from the fact that it’s used to indicate, that it’s holding the business logic, there’s nothing else noticeable in this annotation; but who knows, Spring may add some additional exceptional in future.
Similar to above, in the future Spring may add special functionalities for
See less@Service
,@Controller
and@Repository
based on their layering conventions. Hence, it’s always a good idea to respect the convention and use it in line with layers.How to split a list into equally-sized chunks in Python?
Here's a generator that yields evenly-sized chunks: def chunks(lst, n): """Yield successive n-sized chunks from lst.""" for i in range(0, len(lst), n): yield lst[i:i + n] import pprint pprint.pprint(list(chunks(range(10, 75), 10))) [[10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25,Read more
Here’s a generator that yields evenly-sized chunks:
For Python 2, using
xrange
instead ofrange
:Below is a list comprehension one-liner. The method above is preferable, though, since using named functions makes code easier to understand. For Python 3:
For Python 2:
See lessWhen and How to use GraphQL with microservice architecture?
Definitely approach #1. Having your clients talk to multiple GraphQL services (as in approach #2) entirely defeats the purpose of using GraphQL in the first place, which is to provide a schema over your entire application data to allow fetching it in a single roundtrip. Having a shared nothing archiRead more
Definitely approach #1.
Having your clients talk to multiple GraphQL services (as in approach #2) entirely defeats the purpose of using GraphQL in the first place, which is to provide a schema over your entire application data to allow fetching it in a single roundtrip.
Having a shared nothing architecture might seem reasonable from the microservices perspective, but for your client-side code it is an absolute nightmare, because every time you change one of your microservices, you have to update all of your clients. You will definitely regret that.
GraphQL and microservices are a perfect fit, because GraphQL hides the fact that you have a microservice architecture from the clients. From a backend perspective, you want to split everything into microservices, but from a frontend perspective, you would like all your data to come from a single API. Using GraphQL is the best way I know of that lets you do both. It lets you split up your backend into microservices, while still providing a single API to all your application, and allowing joins across data from different services.
If you don’t want to use REST for your microservices, you can of course have each of them have its own GraphQL API, but you should still have an API gateway. The reason people use API gateways is to make it more manageable to call microservices from client applications, not because it fits well into the microservices pattern.
See less