Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7507761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:33:06+00:00 2026-05-29T22:33:06+00:00

what is the best approach for using velocity component inside spring container. In my

  • 0

what is the best approach for using velocity component inside spring container.

In my velocity util(emailer) class , i have declared the method for sending email as STATIC to avoid creation of emailer object everytime. Also i have declared the setter injections as static.

Is this the correct way ?

Java:

     Class emailer{


    public static Boolean sendEmail(){
     SimpleMailMessage msg = new SimpleMailMessage();  
     String val = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, template, model);
    msg.setText(val);
    mailSender.send(msg);


    }



    private static MailSender mailSender;

@Autowired
public static void setMailSender(MailSender mailsender) {
    LOGGER.debug("MailSender set successfully");
    mailSender = mailsender;
}

private static VelocityEngine velocityEngine;

@Autowired
public static void setVelocityEngine(VelocityEngine velocityengine) {
    LOGGER.debug("Velocity Engine set successfully");
    velocityEngine = velocityengine;
}
    }

xml changes:
localhost

<bean id="emailHelper" class="emailer">
  <property name="mailSender" ref="mailSender"/>
  <property name="velocityEngine" ref="velocityEngine"/>

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
 <property name="velocityProperties">
 <props>
  <prop key="resource.loader">class</prop>
  <prop key="class.resource.loader.class">
            org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
  </prop>


  </props>
   </property>
   </bean>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:33:07+00:00Added an answer on May 29, 2026 at 10:33 pm

    You shouldn’t use static methods here. Spring can’t autowire static methods (see Can you use @Autowired with static fields?)

    But even if you could autowire static methods, it wouldn’t be any better than using instance methods. That’s because Spring creates beans as singletons by default, which means it creates only one instance and it uses the same one for each @Autowired property. So it would not create a new instance of your emailer object or your mailsender object every time, it would keep using the same one.

    The Spring documentation explains it in more detail.

    You should only use instance properties and variables to make it work — in other words, just remove the word static from each one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best approach in using a timer. Use a System.Timer.Timer class or
Setting up an integration server, I’m in doubt about the best approach regarding using
What is the best approach to managing NHibernate transaction using Autofac within web application?
With an existing n-tier web application using ASP.net WebForms what is the best approach
what is the best approach to using javascript to asking input username & password
What's the best approach to execute the following using __import__ so that I may
I wonder what's the best approach for using third party js libraries on a
Can someone explain the best way to handle this approach. I am using TinyMCE
I have a question about best approach. I am not sure which approach is
Using which of these is the best approach while planning Silverlight application? UserControls or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.