DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Why I Started Using Dependency Injection in Python
  • How Spring and Hibernate Simplify Web and Database Management
  • Functional Endpoints: Alternative to Controllers in WebFlux
  • Graceful Shutdown: Spring Framework vs Golang Web Services

Trending

  • Developers Beware: Slopsquatting and Vibe Coding Can Increase Risk of AI-Powered Attacks
  • The Full-Stack Developer's Blind Spot: Why Data Cleansing Shouldn't Be an Afterthought
  • Integrating Model Context Protocol (MCP) With Microsoft Copilot Studio AI Agents
  • Metrics at a Glance for Production Clusters
  1. DZone
  2. Coding
  3. Frameworks
  4. Logger Injection With Spring’s InjectionPoint

Logger Injection With Spring’s InjectionPoint

If you have a tendency to use Logger instances, see how you can set up a way to inject them into your features at runtime.

By 
Fatih Dirlikli user avatar
Fatih Dirlikli
·
Nov. 27, 16 · Tutorial
Likes (8)
Comment
Save
Tweet
Share
27.9K Views

Join the DZone community and get the full member experience.

Join For Free

You should all be familiar with the boiler plate code you include on top of your classes to get a Logger instance for use in your classes at runtime.

Logger logger = LoggerFactory.getLogger(WorkOrderController.class);

This is the classic way of obtaining a logger instance at runtime with the classname that you want to be displayed in your log messages like below.

2016-11-22 14:48:37.025 ERROR --- org.orwashere.WorkOrderController : My Service got the request!!

The fully qualified class name org.orwashere.WorkOrderController is retrieved and logged from the class parameter you pass to LoggerFactories getLogger class.

With Spring Framework’s new InjectionPoint feature you can get rid of these boiler plate code in your classes. All you need to do is declaring a prototype scoped Logger bean in one of your configuration classes like below. And from that point, you have access to the class’s properties that the Logger bean is being injected to. As we should be creating a new Logger instance for each Spring Bean that we want to enable logging, it is important to define the Logger bean as prototype scoped.

@Bean
@Scope("prototype")
Logger logger(InjectionPoint injectionPoint){
    return LoggerFactory.getLogger(injectionPoint.getMethodParameter().getContainingClass());
}

In your class target bean in which the logger will actually be used all you need to do is injecting the Logger bean as a classic Spring Bean and voila your logger is configured and ready to be used in your class like below.

@RepositoryRestController
public class WorkOrderController {

    private static Logger logger;

    public WorkOrderController(Logger logger) {
        this.logger = logger;
    }

    public void myControllerMethod() {
        logger.error("My service got the request!");
    }   
}

Even though we are talking about Logger injection in this post, with InjectionPoint support many other opportunities arise before injecting any bean into your class. Basically you can make any kind of configuration on your injected bean depending on the target bean.

Hope it helps you in your future development!

Happy coding!

Spring Framework Injection

Published at DZone with permission of Fatih Dirlikli. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Why I Started Using Dependency Injection in Python
  • How Spring and Hibernate Simplify Web and Database Management
  • Functional Endpoints: Alternative to Controllers in WebFlux
  • Graceful Shutdown: Spring Framework vs Golang Web Services

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!