Here's where you're at: you've got an annotation in your Spring Boot code that accepts a parameter. But you don't want to hardcode the value.

Instead, you'd rather read the value from

Well you're in the right place.

I'll show you how to do that in this guide.

The Same Thing You Do With @Value

You might already be familiar with this type of notation:

    private String mongoConnection;

That code reads the value of the mongo.db property from and sets it as the value of the mongoConnection field in that class.

Note the notation next to the @Value annotation: ("${mongo.db}"). It seems awkward and cumbersome but that's what you gotta do if you want to read the property.

Anyhoo, you might already know about that @Value thing. Or you might have just learned about it right now.

But here's the important point: you can do that with other annotations as well.

That notation isn't confined to only the @Value annotation.

Let's look at an example. Here's how I'm referencing an value in one my Java classes:

public class AnalyticsController {

Pay attention to that @RequestMapping annotation. And look at what you see right next to it.

Yep. It's the same thing you saw with @Value.

And guess what? It works!

In fact, it's working in the very application that powers this website. It's the controller I use to handle endpoints that give me details about how many of you wonderful people are reading my articles.

That annotation sets the root path for all the endpoints in the controller.

So if admin.root is set to /admin in, then all the endpoints in that controller start with /admin.

Another Way

That's not the only way you can do it, though. If the notation above isn't awkward enough for you, here's a notation that's even more awkward:

@Document(collection = "#{@environment.getProperty('mongo.blogpost.collection')}")
public class BlogPost {

Again, that's from a class that's used on this very website.

But take a look at how the collection property is set. That's some really bizarre structuring.

It works though.

By the way, that class represents a document in a MongoDB collection. The name of the collection is identified in by the value of mongo.blogpost.collection.

If you'd like to use that pattern in your application, just change the name of the property but keep everything else the same.

Make sure you pay close attention to the punctuation, though. That's a tricky one.

Wrapping It Up

Now you know a couple of different ways to reference an value in your Spring Boot annotations.

Feel free to take what you've learned here and include it in your own outstanding development efforts.

Have fun!