[#4470] Add <springAnnotations/> code generation configuration to generate Spring annotations on select objects

This commit is contained in:
lukaseder 2015-08-13 13:45:29 +02:00
parent 840d49731c
commit 39452d3680
8 changed files with 44 additions and 13 deletions

View File

@ -65,6 +65,7 @@ abstract class AbstractGenerator implements Generator {
boolean generateDaos = false;
boolean generateJPAAnnotations = false;
boolean generateValidationAnnotations = false;
boolean generateSpringAnnotations = false;
boolean generateGlobalObjectReferences = true;
boolean generateGlobalRoutineReferences = true;
boolean generateGlobalSequenceReferences = true;
@ -223,6 +224,16 @@ abstract class AbstractGenerator implements Generator {
this.generateValidationAnnotations = generateValidationAnnotations;
}
@Override
public boolean generateSpringAnnotations() {
return generateSpringAnnotations;
}
@Override
public void setGenerateSpringAnnotations(boolean generateSpringAnnotations) {
this.generateSpringAnnotations = generateSpringAnnotations;
}
@Override
public boolean generateGlobalObjectReferences() {
return generateGlobalObjectReferences;

View File

@ -396,6 +396,8 @@ public class GenerationTool {
generator.setGenerateJPAAnnotations(g.getGenerate().isJpaAnnotations());
if (g.getGenerate().isValidationAnnotations() != null)
generator.setGenerateValidationAnnotations(g.getGenerate().isValidationAnnotations());
if (g.getGenerate().isSpringAnnotations() != null)
generator.setGenerateSpringAnnotations(g.getGenerate().isSpringAnnotations());
if (g.getGenerate().isGlobalObjectReferences() != null)
generator.setGenerateGlobalObjectReferences(g.getGenerate().isGlobalObjectReferences());
if (g.getGenerate().isGlobalRoutineReferences() != null)

View File

@ -182,6 +182,18 @@ public interface Generator {
*/
void setGenerateValidationAnnotations(boolean generateValidationAnnotations);
/**
* Whether DAOs should be annotated with useful spring annotations such as
* <code>@Repository</code> or <code>@Autowired</code>
*/
boolean generateSpringAnnotations();
/**
* Whether DAOs should be annotated with useful spring annotations such as
* <code>@Repository</code> or <code>@Autowired</code>
*/
void setGenerateSpringAnnotations(boolean generateSpringAnnotations);
/**
* Whether global object references should be generated
*/

View File

@ -2149,6 +2149,9 @@ public class JavaGenerator extends AbstractGenerator {
generateDaoClassJavadoc(table, out);
printClassAnnotations(out, table.getSchema());
if (generateSpringAnnotations())
out.println("@%s", out.ref("org.springframework.stereotype.Repository"));
if (scala)
out.println("class %s(configuration : %s) extends %s[%s, %s, %s](%s, classOf[%s], configuration)[[before= with ][%s]] {",
className, Configuration.class, daoImpl, tableRecord, pType, tType, tableIdentifier, pType, interfaces);
@ -2177,6 +2180,10 @@ public class JavaGenerator extends AbstractGenerator {
}
else {
out.tab(1).javadoc("Create a new %s with an attached configuration", className);
if (generateSpringAnnotations())
out.tab(1).println("@%s", out.ref("org.springframework.beans.factory.annotation.Autowired"));
out.tab(1).println("public %s(%s configuration) {", className, Configuration.class);
out.tab(2).println("super(%s, %s.class, configuration);", tableIdentifier, pType);
out.tab(1).println("}");

View File

@ -19,9 +19,9 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.springframework.version>4.1.2.RELEASE</org.springframework.version>
<org.jooq.version>3.6.2</org.jooq.version>
<org.jooq.version>3.7.0-SNAPSHOT</org.jooq.version>
<org.h2.version>1.4.181</org.h2.version>
<java.version>1.8</java.version>
<java.version>1.8</java.version>
</properties>
<dependencies>
@ -210,6 +210,8 @@
<generate>
<instanceFields>true</instanceFields>
<pojos>true</pojos>
<daos>true</daos>
<springAnnotations>true</springAnnotations>
</generate>
<target>
<packageName>org.jooq.example.db.h2</packageName>

View File

@ -47,7 +47,7 @@
<schemaDirectory>src/main/resources/xsd</schemaDirectory>
<bindingDirectory>src/main/resources/xjb/codegen</bindingDirectory>
<schemaIncludes>
<include>jooq-codegen-3.6.0.xsd</include>
<include>jooq-codegen-3.7.0.xsd</include>
</schemaIncludes>
<generatePackage>org.jooq.util.jaxb</generatePackage>
<args>

View File

@ -11,7 +11,7 @@
<jaxb:globalBindings>
<!-- Force all classes implements Serializable -->
<xjc:serializable uid="360" />
<xjc:serializable uid="370" />
<!-- [#2401] Trim unnecessary whitespace from configuration -->
<xjc:javaType name="java.lang.String" xmlType="xs:string" adapter="org.jooq.util.jaxb.tools.TrimAdapter"/>
@ -19,7 +19,7 @@
<!-- Annotate the following classes with @SuppressWarnings -->
<jaxb:bindings schemaLocation="../../xsd/jooq-codegen-3.6.0.xsd" multiple="true" node="//xs:complexType">
<jaxb:bindings schemaLocation="../../xsd/jooq-codegen-3.7.0.xsd" multiple="true" node="//xs:complexType">
<annox:annotate>
<annox:annotate annox:class="java.lang.SuppressWarnings" value="all" />
</annox:annotate>

View File

@ -687,6 +687,11 @@
-->
<element name="validationAnnotations" type="boolean" default="false" minOccurs="0" maxOccurs="1" />
<!--
Annotate DAOs with useful spring annotations such as @Repository or @Autowired
-->
<element name="springAnnotations" type="boolean" default="false" minOccurs="0" maxOccurs="1" />
<!--
Allow to turn off the generation of ALL global object references, which include
@ -750,14 +755,6 @@
by specifying <fullyQualifiedTypes>java\.lang\..*</fullyQualifiedTypes>
-->
<element name="fullyQualifiedTypes" type="string" default="" minOccurs="0" maxOccurs="1" />
<!--
A flag indicating whether Java 8's java.time types should be used by the
source code generator, rather than JDBC's java.sql types.
This flag is ignored in the commercial Java 6 distribution of jOOQ 3.7+
-->
<element name="javaTimeTypes" type="boolean" default="true" minOccurs="0" maxOccurs="1" />
</all>
</complexType>