Announcement Announcement Module
Collapse
No announcement yet.
properties setter doesnot work properly Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • properties setter doesnot work properly

    Hi, all:

    I am tring to use spring to create some beans in the context, part of my applicationContext.xml is like following, if I use the commented out part, then always there are some issues, it does not work, but If I use the non-commented out part, it will works fine, the only difference is commented-part is using the properties from some other file, but non-commented part is directly using the value.

    I think maybe the problem is from spring, when it call the setter method, if it is string, it does not do trim operation, so that is why some wired problems happening.

    Any suggestion or help will be appreciated.

    Thanks
    Jack



    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer">
    <property name="locations">
    <list>
    <value>myserver.properties</value>
    </list>
    </property>

    </bean>


    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailS enderImpl">
    <!-- for some reason , this way does not work, I believe this is bug from setter method, it doesn't do a trim after get the value from here
    <property name="host">
    <value>${mail.host}</value>
    </property>
    <property name="username">
    <value>${mail.username}</value>
    </property>
    <property name="password">
    <value>${mail.password}</value>
    </property>

    <property name="javaMailProperties">
    <props>
    <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
    <prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
    </props>
    </property>
    -->


    <property name="host"><value>smtp.mydomain.com</value></property>
    <property name="username"><value>aaaaa</value></property>
    <property name="password"><value>bbbbb</value></property>
    <property name="javaMailProperties"><props><prop key="mail.smtp.auth">true</prop></props></property>
    </bean>

    also

    <bean id="cronTrigger1" class="org.springframework.scheduling.quartz.CronT riggerBean">
    <property name="jobDetail" ref="scheduleJob"/>
    <!--<property name="cronExpression"><value>${report.scheduler.cr onexpression}<value/></property>-->
    <property name="cronExpression" value="30 * * * * ?"/>
    </bean>


    part of myserver.properties file:

    mail.host=smtp.mydomain.com
    mail.username=aaaaa
    mail.password=bbbbb
    mail.smtp.auth=true
    mail.smtp.timeout=25000
    mail.default.from=report@


    report.generation.cronexpression = 0 53 * * * ?
    report.scheduler.cronexpression = 30 * * * * ?

  • #2
    It doesn't trim and should not trim the parameter - what if you want to pass in a String with leading/trailing spaces? Make sure your Strings are properly formatted or, if you want, apply trimming yourself at setter level.

    Comment


    • #3
      I don't agree with you, maybe it should not trim, I am not sure.

      But org.springframework.scheduling.quartz.CronT riggerBean and org.springframework.mail.javamail.JavaMailS enderImpl are from Spring, so Spring team should make sure it works fine for both situation.

      If you are from spring team, please do this test by your self to see if it works.


      I don't care this too much, but this makes srping very ugly, because part of your configuration will be at another properties file, but part of the configuration will be in the applictionContext.xml file, I don't think it is good.

      Any body has the similar issues? or any useful suggestions? Thanks

      By the way, it works fine for hibernate properties setting inside of the applicationcontext file, I mean like followng:

      ======================

      <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource" destroy-method="close">
      <property name="driverClassName"><value>${hibernate.connecti on.driver_class}</value></property>
      <property name="url"><value>${hibernate.connection.url}</value></property>
      <property name="username"><value>${hibernate.connection.user name}</value></property>
      <property name="password"><value>${hibernate.connection.pass word}</value></property>
      </bean>


      <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
      <property name="dataSource"><ref local="dataSource"/></property>
      <property name="hibernateProperties">
      <props>
      <prop key="hibernate.dialect"> ${hibernate.dialect} </prop>
      <prop key="hibernate.default_schema"> ${hibernate.default_schema} </prop>
      <prop key="hibernate.dialect"> ${hibernate.dialect} </prop>
      <prop key="hibernate.show_sql"> ${hibernate.show_sql} </prop>
      </props>
      </property>
      <property name="mappingResources">
      <list>
      <value>mytable.xml</value>

      </list>
      </property>

      ========================

      Comment


      • #4
        so here I just want to know why it works fine if I directly use the value inside of the applicationContext.xml, and it always throw out following exception if I use ${mail.username}

        -----------
        Authentication failed: null; nested exception is javax.mail.AuthenticationFailedException: null
        -------------

        ====================
        <property name="host">
        <value>${mail.host}</value>
        </property>
        <property name="username">
        <value>${mail.username}</value>
        </property>
        <property name="password">
        <value>${mail.password}</value>
        </property>

        <property name="javaMailProperties">
        <props>
        <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
        <prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
        </props>
        </property>

        Comment


        • #5
          Ok, here is what I found, for host it is OK to use following style:

          <property name="host">
          <value>${mail.host}</value>
          </property>

          but if I use this style for username or password or both, then it will throw out this exception:

          Authentication failed: null; nested exception is javax.mail.AuthenticationFailedException: null

          any help on this will be appreciated.

          Thanks

          Comment


          • #6
            I had some NPE myself when I've used JavaMail and the solution in my case, was to download the sources and see what line was complaining about the exception. If I recall correctly, some properties (which were not documented as required) were not set and this in turn, lead to null objects.

            Comment


            • #7
              But inside of JavaMailSenderImpl , there are setUsername and setPassword methods, this(JavaMailSenderImpl ) is from Spring, as to how to pass these information to JavaMail implementation, it will decided or implemented by Spring, so I still think this issue is from Spring.



              /*
              * Copyright 2002-2006 the original author or authors.
              *
              * Licensed under the Apache License, Version 2.0 (the "License");
              * you may not use this file except in compliance with the License.
              * You may obtain a copy of the License at
              *
              * http://www.apache.org/licenses/LICENSE-2.0
              *
              * Unless required by applicable law or agreed to in writing, software
              * distributed under the License is distributed on an "AS IS" BASIS,
              * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
              * See the License for the specific language governing permissions and
              * limitations under the License.
              */

              package org.springframework.mail.javamail;

              import java.io.IOException;
              import java.io.InputStream;
              import java.util.ArrayList;
              import java.util.Date;
              import java.util.HashMap;
              import java.util.List;
              import java.util.Map;
              import java.util.Properties;

              import javax.activation.FileTypeMap;
              import javax.mail.AuthenticationFailedException;
              import javax.mail.MessagingException;
              import javax.mail.NoSuchProviderException;
              import javax.mail.Session;
              import javax.mail.Transport;
              import javax.mail.internet.MimeMessage;

              import org.apache.commons.logging.Log;
              import org.apache.commons.logging.LogFactory;

              import org.springframework.mail.MailAuthenticationExcepti on;
              import org.springframework.mail.MailException;
              import org.springframework.mail.MailParseException;
              import org.springframework.mail.MailPreparationException;
              import org.springframework.mail.MailSendException;
              import org.springframework.mail.SimpleMailMessage;

              /**
              * Production implementation of the JavaMailSender interface.
              * Supports both JavaMail MimeMessages and Spring SimpleMailMessages.
              * Can also be used as plain MailSender implementation.
              *
              * <p>Allows for defining all settings locally as bean properties.
              * Alternatively, a pre-configured JavaMail Session can be specified,
              * possibly pulled from an application server's JNDI environment.
              *
              * <p>Non-default properties in this object will always override the settings
              * in the JavaMail Session. Note that if overriding all values locally, there
              * is no added value in setting a pre-configured Session.
              *
              * @author Dmitriy Kopylenko
              * @author Juergen Hoeller
              * @since 10.09.2003
              * @see javax.mail.internet.MimeMessage
              * @see org.springframework.mail.SimpleMailMessage
              * @see org.springframework.mail.MailSender
              * @see #setJavaMailProperties
              * @see #setHost
              * @see #setPort
              * @see #setUsername
              * @see #setPassword
              * @see #setSession
              */
              public class JavaMailSenderImpl implements JavaMailSender {

              public static final String DEFAULT_PROTOCOL = "smtp";

              public static final int DEFAULT_PORT = -1;


              protected final Log logger = LogFactory.getLog(getClass());

              private Session session = Session.getInstance(new Properties(), null);

              private String protocol = DEFAULT_PROTOCOL;

              private String host;

              private int port = DEFAULT_PORT;

              private String username;

              private String password;

              private String defaultEncoding;

              private FileTypeMap defaultFileTypeMap;


              /**
              * Constructor for JavaMailSenderImpl.
              * <p>Initializes the "defaultFileTypeMap" property with a default
              * ConfigurableMimeFileTypeMap.
              * @see #setDefaultFileTypeMap
              * @see ConfigurableMimeFileTypeMap
              */
              public JavaMailSenderImpl() {
              ConfigurableMimeFileTypeMap fileTypeMap = new ConfigurableMimeFileTypeMap();
              fileTypeMap.afterPropertiesSet();
              this.defaultFileTypeMap = fileTypeMap;
              }

              /**
              * Set JavaMail properties for the Session. A new Session will be created
              * with those properties. Use either this or setSession, not both.
              * <p>Non-default properties in this MailSender will override given
              * JavaMail properties.
              * @see #setSession
              */
              public void setJavaMailProperties(Properties javaMailProperties) {
              this.session = Session.getInstance(javaMailProperties, null);
              }

              /**
              * Set the JavaMail Session, possibly pulled from JNDI. Default is a new Session
              * without defaults, i.e. completely configured via this object's properties.
              * <p>If using a pre-configured Session, non-default properties in this
              * MailSender will override the settings in the Session.
              * @see #setJavaMailProperties
              */
              public void setSession(Session session) {
              if (session == null) {
              throw new IllegalArgumentException("Cannot work with a null Session");
              }
              this.session = session;
              }

              /**
              * Return the JavaMail Session.
              */
              public Session getSession() {
              return session;
              }

              /**
              * Set the mail protocol. Default is SMTP.
              */
              public void setProtocol(String protocol) {
              this.protocol = protocol;
              }

              /**
              * Return the mail protocol.
              */
              public String getProtocol() {
              return protocol;
              }

              /**
              * Set the mail server host, typically an SMTP host.
              */
              public void setHost(String host) {
              this.host = host;
              }

              /**
              * Return the mail server host.
              */
              public String getHost() {
              return host;
              }

              /**
              * Set the mail server port. Default is -1, letting JavaMail
              * use the default SMTP port (25).
              */
              public void setPort(int port) {
              this.port = port;
              }

              /**
              * Return the mail server port.
              */
              public int getPort() {
              return port;
              }

              /**
              * Set the username for the account at the mail host, if any.
              * <p>Note that the underlying JavaMail Session has to be configured
              * with the property "mail.smtp.auth" set to "true", else the specified
              * username will not be sent to the mail server by the JavaMail runtime.
              * If you are not explicitly passing in a Session to use, simply specify
              * this setting via JavaMailSenderImpl's "javaMailProperties".
              * @see #setJavaMailProperties
              * @see #setSession
              * @see #setPassword
              */
              public void setUsername(String username) {
              this.username = username;
              }

              /**
              * Return the username for the account at the mail host.
              */
              public String getUsername() {
              return username;
              }

              /**
              * Set the password for the account at the mail host, if any.
              * <p>Note that the underlying JavaMail Session has to be configured
              * with the property "mail.smtp.auth" set to "true", else the specified
              * password will not be sent to the mail server by the JavaMail runtime.
              * If you are not explicitly passing in a Session to use, simply specify
              * this setting via JavaMailSenderImpl's "javaMailProperties".
              * @see #setJavaMailProperties
              * @see #setSession
              * @see #setUsername
              */
              public void setPassword(String password) {
              this.password = password;
              }

              /**
              * Return the password for the account at the mail host.
              */
              public String getPassword() {
              return password;
              }

              /**
              * Set the default encoding to use for MimeMessages created by this
              * JavaMailSender. Such an encoding will be auto-detected by MimeMessageHelper.
              * @see MimeMessageHelper
              */
              public void setDefaultEncoding(String defaultEncoding) {
              this.defaultEncoding = defaultEncoding;
              }

              /**
              * Return the default encoding for MimeMessages, or <code>null</code> if none.
              */
              public String getDefaultEncoding() {
              return defaultEncoding;
              }

              /**
              * Set the default Java Activation FileTypeMap to use for MimeMessages
              * created by this JavaMailSender. A FileTypeMap specified here will be
              * autodetected by MimeMessageHelper, avoiding the need to specify the
              * FileTypeMap for each MimeMessageHelper instance.
              * <p>For example, you can specify a custom instance of Spring's
              * ConfigurableMimeFileTypeMap here. If not explicitly specified, a default
              * ConfigurableMimeFileTypeMap will be used, containing an extended set
              * of MIME type mappings (as defined by the <code>mime.types</code> file
              * contained in the Spring jar).
              * @see MimeMessageHelper#setFileTypeMap
              * @see ConfigurableMimeFileTypeMap
              */
              public void setDefaultFileTypeMap(FileTypeMap defaultFileTypeMap) {
              this.defaultFileTypeMap = defaultFileTypeMap;
              }

              /**
              * Return the default Java Activation FileTypeMap for MimeMessages,
              * or <code>null</code> if none.
              */
              public FileTypeMap getDefaultFileTypeMap() {
              return defaultFileTypeMap;
              }


              //---------------------------------------------------------------------
              // Implementation of MailSender
              //---------------------------------------------------------------------

              public void send(SimpleMailMessage simpleMessage) throws MailException {
              send(new SimpleMailMessage[] { simpleMessage });
              }

              .......

              Comment


              • #8
                I fixed it , thanks, I don't think it is from Spring.

                Comment


                • #9
                  What was your problem? Knowing the issue might help others in the future.

                  Comment

                  Working...
                  X