Spring Boot how to create Auto configuration class [closed] - java

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 5 years ago.
Improve this question
I am beginner for Spring Boot. When I am using any dependencies in Spring Boot, they have the auto configuration default.
My questions are:
What is actually auto configuration class?
How does auto configuration work?
How to make out own auto configuration?
Please suggest me any blog that describes easy manner or please provide me any code snippet for better understanding.

The Spring Boot core package spring-boot-starter contains the spring-boot-autoconfigure package.
What does it do? (from the JavaDoc)
Enable auto-configuration of the Spring Application Context,
attempting to guess and configure beans that you are likely to need.
Auto-configuration classes are usually applied based on your classpath
and what beans you have defined. For example, If you have
tomcat-embedded.jar on your classpath you are likely to want a
TomcatEmbeddedServletContainerFactory (unless you have defined your
own EmbeddedServletContainerFactory bean).
Auto-configuration tries to be as intelligent as possible and will
back-away as you define more of your own configuration. You can always
manually exclude() any configuration that you never want to apply (use
excludeName() if you don't have access to them). You can also exclude
them via the spring.autoconfigure.exclude property. Auto-configuration
is always applied after user-defined beans have been registered.
So each jar in your classpath that Spring can autoconfigure, Spring will autoconfigure for you to use in your application. Think about Hibernate, ThymeLeaf, Jackson etc.
How do you use it?
Simply add the #EnableAutoConfiguration in your application to make Spring autoconfigure your application (you possibly also need #SpringBootConfiguration).
#SpringBootConfiguration
#EnableAutoConfiguration
// Or just #SpringBootApplication instead of the 2 above
#Import(AppConfig.class)
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class);
}
}
And your good to go.
What can it configure for you?
All of these tools below (got this from looking at the org.springframework.boot.autoconfigure package)
admin
amqp
aop
batch
cache
cassandra
cloud
condition
context
couchbase
dao
data
analyzer
domain
jest
flyway
freemarker
template
gson
h2
hateoas
hazelcast
info
integration
jackson
jdbc
jersey
jms
jmx
jooq
kafka
ldap
liquibase
logging
mail
mobile
mongo
mustache
jpa
reactor
security
sendgrid
session
social
solr
template
thymeleaf
transaction
validation
web
webservices
websocket
How to create your own configuration?
Don't know, never needed to do this. But this blog is a good starting point.

Related

Spring data JPA WITHOUT an entire Spring Boot application

I am creating an internal CLI that is communicating with a PostgreSQL database and the easiness to create a no-code repository is one of the features that convince me to choose Spring data JPA.
However, I am not able to find some tutorial or GitHub repository to set up a Spring data JPA project without an entire spring boot application.
On the project https://github.com/spring-projects/spring-data-book/tree/master/jpa there is no main entry point, so the code is not runnable and by the way, it was updated 8 years ago ...
This other StackOverflow thread Spring Data JPA without Spring Boot does not help me because the guy could run his spring application on Google Cloud Platform finally (that was the cause of why he ask how to setup sping data jpa without spring boot).
I don't know how to start, if you have any ideas I will be happy to discuss with someone who is more experienced than me.
Thank you.
This might help if no more complete tutorial turns up https://docs.spring.io/spring-data/jpa/docs/1.5.0.RELEASE/reference/html/repositories.html
Look for this section
Standalone usage
You can also use the repository infrastructure outside of a Spring container, e.g. in CDI environments. You still need some Spring libraries in your classpath, but generally you can set up repositories programmatically as well. The Spring Data modules that provide repository support ship a persistence technology-specific RepositoryFactory that you can use as follows
In particular it says you can use a factory to generate repositories:
RepositoryFactorySupport factory = … // Instantiate factory here
UserRepository repository = factory.getRepository(UserRepository.class);
So adding the spring parts that contain the spring data classes may be enough for this level and if you want to have DI, too, you likely need to combine them with the respective spring dependencies and configure a regular spring application.

How to automatically test that Ehcache is used in Spring Boot project

I started using Ehcache in my Spring Boot project. How can I prove that the Ehcache is being used instead of the default ConcurrentHashMap, which is provided by Spring Boot by default? How can I prove it automatically in the integration tests?
If I understand the question correctly, you are trying to validate your spring configuration.
A one time thing regarding Ehcache is to check logs - you should see the information about it being started and configured.
For automated testing, the easiest way is going to be to have the test configured to be injected with the cacheManager bean and then making sure it is of the right type.
This should give you the confidence that your setup is correct.

#EnableIntegration annotation purpose

I migrate project from XML Spring Integration configuration to Java DSL. I prepared some integration tests beforehand. So I can do the migration safely step-by-step.
At some point after moving this XML connector definition
<int:publish-subscribe-channel id="upstreamAckChannel" />
to Java Spring Configuration
#Bean
public PublishSubscribeChannel upstreamAckChannel() {
return MessageChannels.publishSubscribe().get();
}
my integration flow stopped resend test messages to my tests.
After some time and experiments I realized that my Spring Java configuration must have #EnableIntegration annotation together with usual Spring #Configuration annotation for properly work.
The question is what is #EnableIntegration annotation semantic? When I can not use it and when I must use?
I could find only this small Configuration paragraph in official reference manual. Unfortunately, description isn't clear.
The PublishSubscribeChannel class exists inside of the Spring Integration project. The #EnableIntegration annotation is used to adopt a default configuration for Spring Integration, so typically when using Spring Integration you'll want to add it (unless you're using a piece of Spring Integration that doesn't require a context--unlikely). The only time you might want to forego it is if you want to do your own configuration from scratch.

Do I need to configure hibernate before configuring spring-data?

I am a little bit lost here, my main goal is to create a MVC pattern with Spring MVC and include Spring-security with user/passwords from the database.
So far, I have Spring security and MVC running well, but I dont know how to include the database (I must use spring data in some point).
I've read the tutorials and info of the site, and it says its a layer that works with other ORM (such as hibernate). So my question is, Should I configure hibernate before Spring data?
Is there any guide on how to do it (where they use annotations only?).
The answer to the question "do I need hibernate configured to use Spring Data JPA" is "yes" if you want Hibernate to be your JPA implentation.
So here's what the stack looks like for a typical Spring MVC + Spring Data JPA application:
#Controller class with #RequestMapping
calls
#Service class
calls
#Repository class (this annotation is optional, extend CrudRepository interface)
The repository you write uses an #Entity (JPA) class to access the database through Hibernate as described in this tutorial. Spring does a great job hiding most of the setup details of Hibernate from you (you don't need a persistence.xml if you do it right).

Ultimate Aim of Spring-Boot [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
as you may know Spring4 comes with new features, and one of the most important feature among them is Spring-boot.
I am following the links below
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-documentation
https://github.com/spring-projects/spring-boot
Spring-boot feature comes with new class files in org.springframework.boot.* to start the Spring application. There are two question comes in mind that
1- for JavaSE, I can start-up the spring application with previoues spring versions easly, is the new feature spring-boot is just for simple boot
2- for JavaEE, as far as I know Spring-boot is not just for javaSE project, it can start-up web projects as well. So in the future spring-boot works as Application-server (like Glassfish)
Although Spring Boot only works with Spring 4+, it is technically a different project. What this means is that you can use Spring 4 without any Spring Boot code.
The aim of Spring Boot is to provide an easy way to configure a Spring application by providing sensible defaults and easy configuration options for stuff that is commonly used (and you otherwise have to implemented) over and over again in our applications.
As far as starting up a Java SE application, Spring Boot will easily start the application just like any other Java SE, with the main method, and looks something like this:
#Configuration
#EnableAutoConfiguration
//whatever other annotations
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
//do whatever
}
}
In order to use a web environment, Spring Boot uses an embedded servlet container (Tomcat by default, but Jetty is also available). That means that code like:
#ComponentScan
#EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
#RestController
public class HelloController {
#RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
is enough to get everything started (providing that all the required dependencies are on the classpath).
Seeing working Spring code that is so light, is a breath of fresh air! You no longer need to loads of XML or Java config files, the defaults work great!
Also you can start and stop the whole application from the main method inside your IDE! Sweet!

Categories

Resources