Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add spring boot to existing project without changing the main(args[]) method

currently I am trying to add spring boot to an existing project.
This project is a standalone jar that successfully uses spring jpa and hibernate. But because the application must be able to receive and send json files, I thought that spring boot would be the best option.

Therefore I added the following dependency to my pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>1.4.0.RELEASE</version>
</dependency>

And set up a little RequestMapping as a test

@Controller
public class RequestMappingController {
    @RequestMapping("/")
    @ResponseBody
    public String myAction() {
        return "Hello World !";
    }
}

Now I have the problem, that all my sources (this, this and this) are telling me to set up the main method next.
Something like

public static void main(String[] args) {
    SpringApplication.run(Main.class, args);
}

But I am not able to change that method. My architecture uses a main jar file that loads components and plugins as jar files. And the main jar file is downloaded and is not programmed by me.

main.jar          (downloaded)
|- plugin1.jar    (programmed by me)
|- plugin2.jar    (programmed by me)
|- ...

I am not using any xml files (except the pom.xml file of course) to configure spring. In myplugin1.jarthere is a JpaConfig Class with all the important spring enabling annotations (@EnableScheduling,@EnableAsync, transactionmanagement, component scan,@Configuration, jpa repository enabling, ...)
I recognices that there is also a annotation
@SpringBootConfiguration` and because my plugin1 is able to activate all current implemented spring components in this Configuration class, maybe I can set up spring boot there as well? (is there a way? or some other way with a second configuration class?)

And that is my question: how can I enable spring boot within this existing project structure without changing the main(String[] args) method?

like image 406
christopher2007 Avatar asked Oct 27 '25 20:10

christopher2007


1 Answers

add this to your pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.2.RELEASE</version>
</parent>

Edit: the POM:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.2.RELEASE</version>
    </parent>

    <dependencies>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

That is you need to give spring boot a connector and also a application.properties on connection information of the database. For example:

# database
spring.datasource.url= jdbc:mysql://****
spring.datasource.username=****
spring.datasource.password=****

#ddl generation
spring.jpa.hibernate.ddl-auto=update

# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.testOnBorrow=true
spring.datasource.timeBetweenEvictionRunsMillis = 3600000
spring.datasource.validationQuery = SELECT 1

# Number of ms to wait before throwing an exception if no connection is available.
spring.datasource.max-wait=10000

# Maximum number of active connections that can be allocated from this pool at the same time.
spring.datasource.max-active=50

# Show or not log for each sql query
spring.jpa.show-sql = true

# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager)

# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
like image 109
dumb_terminal Avatar answered Oct 30 '25 12:10

dumb_terminal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!