فهرست منبع

Refactored config files.

Ant and application config files are now stored seperately.

Config now uses a root class which imports other required classes.
I'm using the root config for all the one-time annotations that seem
like they'll become increasingly common.
Tom Flucke 7 سال پیش
والد
کامیت
9b92769f7a

+ 11 - 7
build.xml

@@ -3,23 +3,24 @@
 <project name="ieat2" basedir="." default="war" xmlns:ivy="antlib:org.apache.ivy.ant">
   
   <property name="app.name" value="ieat"/>
+  <property name="app.conf.dir" value="conf/app/"/>
 
   <property name="ant.lib" value="${user.home}/.ant/lib"/>
-  
-  <property name="conf.dir" value="conf/"/>
+  <property name="ant.conf.dir" value="conf/ant/"/>
+
   <property name="lib.dir" value="lib/"/>
   <property name="web.dir" value="web/"/>
 
-  <property prefix="tomcat" file="${conf.dir}/tomcat.properties"/>
+  <property prefix="tomcat" file="${ant.conf.dir}/tomcat.properties"/>
 
-  <property prefix="build" file="${conf.dir}/build.properties"/>
+  <property prefix="build" file="${ant.conf.dir}/build.properties"/>
   <property name="build.lib" value="${lib.dir}/war/"/>
   <property name="build.warfile" value="${build.dir}/${app.name}-${build.version}.war"/>
   <tstamp>
 	<format property="build.time" pattern="MM/dd/yyyy hh:mm aa z" />
   </tstamp>
 
-  <property prefix="compile" file="${conf.dir}/compile.properties"/>
+  <property prefix="compile" file="${ant.conf.dir}/compile.properties"/>
   <property name="compile.lib" value="${lib.dir}/compile"/>
   <path id="compile.classpath">
 	<pathelement location="${target.dir}"/>
@@ -30,7 +31,7 @@
   <property name="target.dir" value="${build.dir}/${app.name}/WEB-INF/classes"/>
   
   <property name="ivy.jar" value="${ant.lib}/ivy.jar"/>
-  <property name="ivy.dep.file" value="${conf.dir}/ivy.xml" />
+  <property name="ivy.dep.file" value="${ant.conf.dir}/ivy.xml" />
   <available property="ivy.installed" file="${ivy.jar}"/>
   
   <target name="help" description="Display build help">
@@ -88,8 +89,11 @@
   
   <target name="war" depends="compile,ivy.resolve.war" description="Create application WAR">
 	<mkdir dir="${build.dir}"/>
-	<war warfile="${build.warfile}" webxml="${conf.dir}/web.xml">
+	<war warfile="${build.warfile}" webxml="${app.conf.dir}/web.xml">
       <lib dir="${build.lib}"/>
+      <lib dir="${app.conf.dir}">
+        <exclude name="web.xml"/>
+      </lib>
       <fileset dir="web"/>
       <classes dir="${target.dir}"/>
     </war>

+ 0 - 0
conf/build.properties → conf/ant/build.properties


+ 0 - 0
conf/compile.properties → conf/ant/compile.properties


+ 28 - 0
conf/ant/ivy.xml

@@ -0,0 +1,28 @@
+<ivy-module version="2.0">
+  <info organisation="name.tflucke" module="ieat2"/>
+  <configurations defaultconfmapping="default->default">
+    <conf name="default" />
+    <conf name="provided" description="Provided by the environment" />
+    <conf name="compile" extends="default,provided" />
+    <conf name="war" extends="default"/>
+    <conf name="deploy" description="Used by ant to deploy war files." />
+  </configurations>
+  <dependencies>
+    <!-- Spring Framework -->
+    <dependency org="commons-logging" name="commons-logging" rev="1.2"/>
+    <dependency org="org.springframework" name="spring-webmvc" rev="5.0.9.RELEASE"/>
+    <!-- DB Resources -->
+    <dependency org="org.mongodb.morphia" name="morphia" rev="1.3.2"/>
+    <dependency org="org.springframework.boot" name="spring-boot-starter-data-mongodb" rev="2.0.5.RELEASE"/>
+    <!-- UI Resources -->
+    <dependency org="org.webjars" name="bootstrap" rev="4.1.3" conf="war->default"/>
+    <dependency org="org.webjars" name="angular-ui-bootstrap" rev="2.5.0" conf="war->default"/>
+    <dependency org="org.webjars" name="webjars-locator" rev="0.34" conf="war->default"/>
+    <dependency org="javax.servlet" name="jstl" rev="1.2" conf="war->default"/>
+    <dependency org="org.apache.taglibs" name="taglibs-standard-impl" rev="1.2.5" conf="war->default"/>
+    <!-- Tomcat Resources -->
+    <dependency org="javax.servlet" name="javax.servlet-api" rev="4.0.1" conf="provided->default"/>
+    <!-- Ant dependencies -->
+    <dependency org="org.apache.tomcat" name="catalina-ant" rev="6.0.53" conf="deploy->default"/>
+  </dependencies>
+</ivy-module>

+ 0 - 0
conf/tomcat.properties → conf/ant/tomcat.properties


+ 8 - 0
conf/app/application.properties

@@ -0,0 +1,8 @@
+#mongodb
+spring.data.mongodb.host=localhost
+spring.data.mongodb.port=27017
+spring.data.mongodb.database=ieat
+
+#logging
+logging.level.org.springframework.data=debug
+logging.level.=error

+ 0 - 0
conf/web.xml → conf/app/web.xml


+ 0 - 13
conf/ivy.xml

@@ -8,19 +8,6 @@
     <conf name="deploy" description="Used by ant to deploy war files." />
   </configurations>
   <dependencies>
-    <!-- Spring Framework -->
-    <dependency org="commons-logging" name="commons-logging" rev="1.2"/>
-    <dependency org="org.springframework" name="spring-webmvc" rev="5.0.9.RELEASE"/>
-    <!-- UI Resources -->
-    <dependency org="org.webjars" name="bootstrap" rev="4.1.3" conf="war->default"/>
-    <dependency org="org.webjars" name="angular-ui-bootstrap" rev="2.5.0" conf="war->default"/>
-    <dependency org="org.webjars" name="webjars-locator" rev="0.34" conf="war->default"/>
-
-    <dependency org="javax.servlet" name="jstl" rev="1.2" conf="war->default"/>
-    <dependency org="org.apache.taglibs" name="taglibs-standard-impl" rev="1.2.5" conf="war->default"/>
-
-    <!-- Tomcat Resources -->
-    <dependency org="javax.servlet" name="javax.servlet-api" rev="4.0.1" conf="provided->default"/>
     <!-- Ant dependencies -->
     <dependency org="org.apache.tomcat" name="catalina-ant" rev="6.0.53" conf="deploy->default"/>
   </dependencies>

+ 2 - 5
src/name/tflucke/ieat2/AppInitializer.java

@@ -4,6 +4,7 @@ import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatche
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+import name.tflucke.ieat2.configs.RootConfig;
 import name.tflucke.ieat2.controllers.*;
 
 public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@@ -12,11 +13,7 @@ public class AppInitializer extends AbstractAnnotationConfigDispatcherServletIni
 
     @Override
     protected Class<?>[] getRootConfigClasses() {
-        /*  this is where you will return you config class
-         *  your root config class should @Import other configs 
-         *  to make them work without needing them to add there
-         */
-        return new Class[] {WebConfig.class};
+        return new Class[] {RootConfig.class};
     }
 
     @Override

+ 37 - 0
src/name/tflucke/ieat2/configs/DBConfig.java

@@ -0,0 +1,37 @@
+package name.tflucke.ieat2.configs;
+
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.config.BeanDefinition;
+import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.core.type.filter.AnnotationTypeFilter;
+
+import com.mongodb.MongoClient;
+
+import org.mongodb.morphia.Datastore;
+import org.mongodb.morphia.Morphia;
+import org.mongodb.morphia.annotations.Entity;
+
+@Configuration
+@SpringBootApplication
+public class DBConfig {
+    @Autowired
+    private MongoClient mongoClient;
+
+    @Bean
+    public Datastore datastore() {
+        Morphia morphia = new Morphia();
+
+        ClassPathScanningCandidateComponentProvider entityScanner =
+            new ClassPathScanningCandidateComponentProvider(true);
+        entityScanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
+        /*for (BeanDefinition candidate : scanner.findCandidateComponents("name.tflucke.ieat2")) {
+            morphia.map(Class.forName(candidate.getBeanClassName()));
+            }*/
+
+        return morphia.createDatastore(mongoClient, "dataStoreInstanceId");
+    }
+}

+ 15 - 0
src/name/tflucke/ieat2/configs/RootConfig.java

@@ -0,0 +1,15 @@
+package name.tflucke.ieat2.configs;
+
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Import;
+import org.springframework.web.servlet.config.annotation.EnableWebMvc;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@Configuration
+@EnableWebMvc
+@SpringBootApplication
+@ComponentScan(basePackages = "name.tflucke.ieat2")
+@Import({DBConfig.class, WebConfig.class})
+public class RootConfig {
+}

+ 1 - 3
src/name/tflucke/ieat2/WebConfig.java → src/name/tflucke/ieat2/configs/WebConfig.java

@@ -1,4 +1,4 @@
-package name.tflucke.ieat2;
+package name.tflucke.ieat2.configs;
 
 import java.util.concurrent.TimeUnit;
 
@@ -15,8 +15,6 @@ import org.springframework.web.servlet.resource.WebJarsResourceResolver;
 import org.springframework.http.CacheControl;
 
 @Configuration
-@EnableWebMvc
-@ComponentScan(basePackages = "name.tflucke.ieat2")
 public class WebConfig implements WebMvcConfigurer {
     @Bean
     public ViewResolver viewResolver() {

+ 5 - 0
src/name/tflucke/ieat2/models/BasicFood.java

@@ -0,0 +1,5 @@
+package name.tflucke.ieat2.models;
+
+public class BasicFood {
+    public String name;
+}