简体   繁体   中英

PMD rule for java to avoid using @Autowired annotation on class fields- to not use field injection

I want to have a pmd rule for my java project which will disallow field injections in Spring(want to use injection by constructor instead). is there any pmd rule for it, or can I create some to have it in my code analysis?

To not allow this:

@Autorired
private Object object;

thanks!

Please try this: Change the @autowired annotation to be fellow disable mode

You could create an XPath rule with the following expression:

//Annotation
  [pmd-java:typeIs('org.springframework.beans.factory.annotation.Autowired')]
  [../FieldDeclaration]

To add this to your ruleset XML, see here :

<rule name="todo"
      language="java"
      message="todo."
      class="net.sourceforge.pmd.lang.rule.XPathRule" >
    <description>
TODO
    </description>
    <priority>3</priority>
    <properties>
        <property name="xpath">
            <value>
<![CDATA[
//Annotation
  [pmd-java:typeIs('org.springframework.beans.factory.annotation.Autowired')]
  [../FieldDeclaration]
]]>
            </value>
        </property>
    </properties>
</rule>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM