JCart: Initial Code SetUp

Let us create a root pom type maven project with 3 sub-modules jcart-core, jcart-admin and jcart-site.

jcart-core module will contain all the core logic excluding web related stuff.

jcart-admin module will contain all the administration related web functionality like Controllers, Security, Validators etc.

jcart-site module will contain all the shoppingcart related web functionality like Controllers, Security, Validators etc.

All these modules use SpringBoot, but as of now STS/IntellijIdea are not providing option to create multi-module SpringBoot application, we will be creating Maven modules and then configure SpringBoot dependencies manually.

jcart/pom.xml


	4.0.0
	com.sivalabs
	jcart
	1.0
	pom
	
	
		UTF-8
		1.8
		1.8	
	
	
	
		jcart-core
		jcart-admin
		jcart-site	
	

	
		
			
				org.springframework.boot
				spring-boot-dependencies
				1.3.0.RELEASE
				pom
				import
				
		
	    

jcart/jcart-core/pom.xml


	4.0.0
	
		com.sivalabs
		jcart
		1.0
	
	jcart-core

		
		
			org.springframework.boot
			spring-boot-starter-data-jpa
			
		
			mysql
			mysql-connector-java
		
		
			org.hibernate
			hibernate-validator
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

jcart/jcart-admin/pom.xml


	4.0.0
	
		com.sivalabs
		jcart
		1.0
	
	jcart-admin

	
	
		
			org.springframework.boot
			spring-boot-maven-plugin
		
	
	

	
		
			com.sivalabs
			jcart-core
			${project.version}
		
		
			org.springframework.boot
			spring-boot-starter-web
			
		
			org.springframework.boot
			spring-boot-starter-thymeleaf
			
		
			org.springframework.boot
			spring-boot-starter-test
			test
			
		
			org.springframework.boot
			spring-boot-devtools
			true
			
	

jcart/jcart-site/pom.xml


	4.0.0
	
		com.sivalabs
		jcart
		1.0
	
	jcart-site

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

	
		
			com.sivalabs
			jcart-core
			${project.version}
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.boot
			spring-boot-starter-thymeleaf
			
		
			org.springframework.boot
			spring-boot-starter-test
			test
			
		
			org.springframework.boot
			spring-boot-devtools
			true
			
	

We are going to use MySQL database instead of an InMemory database like H2 or HSQL, so we will configure our datasource properties in application.properties.

As we are using multi-module project structure, we will have a jcart-core/src/main/resources/application.properties file to put the common properties used by jcart-core module. Properties related to jcart-admin and jcart-site will be in jcart-admin/src/main/resources/application-default.properties and jcart-site/src/main/resources/application-default.properties respectively.

For more information on using properties files refer https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-1.3-Release-Notes#default-profile-applicationproperties

jcart-core/src/main/resources/application.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/jcart
spring.datasource.username=root
spring.datasource.password=admin

spring.jpa.hibernate.ddl-auto=update

The jcart-core module will be used along with jcart-admin and jcart-site modules, not as a stand-alone module. But in order to test the jcart-core module functionality let us create main entry point class JCartCoreApplication in src/test/java folder.

package com.sivalabs.jcart;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

}
package com.sivalabs.jcart;

import java.sql.SQLException;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = JCartCoreApplication.class)
public class JCartCoreApplicationTest
{
	@Autowired DataSource dataSource;
	
	@Test
	public void testDummy() throws SQLException
	{
	String schema = dataSource.getConnection().getCatalog();
	assertEquals("jcart", schema);
	}	
}

If this test case passed we can assume we have base setup ready.

3 thoughts to “JCart: Initial Code SetUp”

  1. hi siva,
    please I just want to ask you to help me on how can I use your application but with XML based configuration. I do not know exactly where to put the main application-context.xml and so on for the application modules..etc

  2. Hi, am new to Spring and am trying to create a jcart multi-module project similar to this but i dont know where to start. Please share the project setup instructions.

Leave a Reply

Your email address will not be published. Required fields are marked *