IntelliJ Live Template Tip – Autowired Dependencies

Do you type these lines a lot?

@Autowired
private MyAwesomeService myAwesomeService;

Then try adding a Live Template to your IntelliJ settings that helps you reduce the number of keystrokes. Go to your Settings (Cmd+, on Mac or Ctrl+Alt+S on Windows) and find the Live Templates panel. On the right, select the ‘other’-group and click on the plus sign to add a new template.

Give the template a name (I chose auto and a description, then make sure it has a context of Java Declarations and the window looks somewhat like this:

live_template

Click ‘Edit Variables’ and make sure the TYPE variable has the expression set to expectedType() (this will open IntelliJ’s type completion box when the template activates). Then set the expression for VAR_NAME to suggesteVariableName() (this whill cause IntelliJ to suggest a name of the variable when you select the type).

template_vars

Now click Ok and close the Settings dialog. Go to your class file and at the place of an instance variable declaration, start typing auto and wait for the popup to show. Press Enter, type the name of your type and press Enter again, you should have a beautiful declaration.

–JH

Posted in Java, Tips and Tricks, Tools and tagged , , , .