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!
Related
In Spring Boot 2.2 there is an annotation #ConditionalOnCloudPlatform which can be used to detect whether the application is running on a Cloud Platform.
I have the reverse issue - I want to not run a bean which is running on a Cloud Platform. For example, I have code which I don't want to run when running on Kubernetes (as the platform where I run Kubernetes already supplies the functions in the given bean).
What's the best approach to do this? I feel like I need a #ConditionalOnCloudPlatform annotation.
Thanks.
Use # ConditionalOnMissingBean with the name of Kubernetes bean of # ConditionalOnMissingClass with one of bean's (or related) classes.
Solution 1
It seems you are using Spring Cloud Kubernetes. If so, when an application runs as a pod inside Kubernetes a Spring profile named 'kubernetes' is automatically get activated.
#Component
#Profile("!kubernetes")
public class MyLocalTestBean {}
Solution 2
Invert condition using NonNestedConditions (the code below is not verified)
class ConditionalOnNotInCloud extends NoneNestedConditions {
ConditionalOnNotInCloud() {
super(ConfigurationPhase.PARSE_CONFIGURATION);
}
#ConditionalOnCloudPlatform
static class OnCloudCondition {
}
}
#Bean
#Conditional(ConditionalOnNotInCloud.class)
public StubBean createStubBean() {}
I have a large Spring Boot monolithic Web Application project. This application is packaged as an executable JAR and serves all kinds of JSON REST endpoints.
Now, I sometimes want to run a piece of Java code to process or import a large file, or clean up certain database tables from the command line.
What would be good way to do this with Spring Boot?
I first looked into CommandLineRunner interface but this seems to serve a completely different use case. This is executed always when running the Spring Boot application, followed by starting the main application.
I would like to have this functionality in the same application as the main web app for various reasons:
Reuse same application configuration (DB credentials, external config files, etc.)
Reuse application context
Shared application logic and code
Difficult to split into smaller (micro) services
If you want to reuse the same jar, you could use a combination of Profiles and CommandLineRunners.
#Configuration
public class BatchConfig {
#Bean
#Profile("import")
public CommandLineRunner import() {
// ...
}
#Bean
#Profile("dbClean")
public CommandLineRunner dbClean() {
// ...
}
}
Then, when you run the jar, pass the desired profile as argument.
java -jar -Dspring.profiles.active=dbClean yourJar.jar
In this way, your command line runners are executed only when the profile matches.
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.
As in spring site isn't allowed to ask question i will write here my question to Spring authors.
I would like to know how supposed to use spring documentation.
I spent a lot of time in Spring boot reference guide. Only for understand how spring boot is working.
There are some examples work principle of which are not explained in documentation. Or explained like this
11.3.2 The #EnableAutoConfiguration annotation
The second class-level annotation is #EnableAutoConfiguration. This annotation tells Spring Boot to “guess” how you will want to configure Spring, based on the jar dependencies that you have added. Since spring-boot-starter-web added Tomcat and Spring MVC, the auto-configuration will assume that you are developing a web application and setup Spring accordingly.
And there are nothing about how it works. Some kind of philosophy.
I read a lot of tutorial from other developer and they give the exactly same information as original documentation. Like nobody knows how it really works and what will happen if you will not use tutorial example)).
Now I have to handle another programmers code on spring boot and I can't find answer to one question. What method will call after this application.main?
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.endpoint.EnvironmentEndpoint;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import javax.inject.Inject;
/**
* Root entry of our service, starting the Spring container and configuring our system.
*/
#SpringBootApplication
#EnableScheduling
public class Application {
#Inject
private EnvironmentEndpoint environmentEndpoint;
protected Application() {
}
/**
* Main method, used to run the application.
*/
public static void main(final String[] args) {
final SpringApplication application = new SpringApplication(Application.class);
application.run(args);
}
}
I'm having troubles in integrating Vaadin and spring-data (JPARepositories in particular).
After following the guidance of Vaadin's team with their last webinar I managed to configure my application using Spring starter (spring boot), adding Vaadin, JPA and a Postgresql database. Using this method was really straightforward and loading entities to a table works out of the box by simply having this
#Autowired private ProjectDAO projects;
// other code here ...
grid.setContainerDataSource(new BeanItemContainer<Project>(Project.class, projects.findAll()));
My repository is just
#Repository
public class ProjectDAO implements JPARepository<Project, Long> {
}
And, as I said, this works flawlessly. The problem is when I try to save something: when trying to save a project via a button click, for example
btnSave.addClickListener(e -> saveCurrent());
private void saveCurrent() {
// ... here I do some filesystem operations, nothing that requires DB connection
setModified();
}
public void setModified() {
project.setLastAccess(new Date());
projects.saveAndFlush(project);
}
I get an exception:
org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress.
I've tried many different things, such as marking the setModified method as #Transactional, or encapsulating the setModified into a service class, but everything always throws this exception.
I find it rather strange that this doesn't work "out of the box", and I'd rather not work with transactions myself, since there are already the instruments to do that.
Any help would be really appreciated
EDIT 12/05/2015
It seems that this problem only appears when using an external server, not the embedded one provided by spring-boot. For test purposes I can manage to use the built-in, but eventually I will have to deploy the application on an existing server, so what could the issue be? Tomcat's configuration?
If anyone needs the answer I'll leave here what I found out after a few days of breaking my head on it.
Before deploying the application I changed some configurations on the main class that was generated by Spring Initializr (The application one) and made it extend SpringBootServletInitializer because I was following the guide I found in the official spring blog. Turns out that this was in fact not necessary because I set up "WAR" in Spring Initializr from the beginning, thus the application already had a class extending that one, and as a result when deploying it on Tomcat, the web application started twice, so the application was configured twice and two persistence units were instantiated. This probably was the error, because after reverting the changes I made to the application everything worked fine.
TL;DR: if you use Spring Initializr to create a WAR application don't touch anything.