2012-03-05 29 views
5

Tôi đang sử dụng tốc độ 1.7 withe spring 3.1 framework để gửi email. vận tốc được sử dụng cho các mẫu email.Cách sử dụng vận tốc 1.7 với Spring

Dưới đây là cấu hình

<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> 

và dưới đây là mã của tôi

@Component 
public class EmailUtils { 

    @Autowired 
    private static VelocityEngine velocityEngine; 

    public static void sendMail(String subject, Map data, String template, 
      String toName, String toAddress) { 


     HtmlEmail email = new HtmlEmail(); 

     try { 
      email.setHostName(hostName); 
      email.setSmtpPort(smtpPort); 
      email.setSubject(subject); 

      System.out.println(template +" template"); 
      System.out.println(data +" data "); 
      System.out.println(velocityEngine +" velocityEngine "); 

      String message = VelocityEngineUtils.mergeTemplateIntoString(
        velocityEngine, template, data); 

      System.out.println(message +" message message "); 

      email.setMsg(message); 
      email.addTo(toAddress, toName); 
      email.setFrom(fromAddress, fromName); 
      email.send(); 
     } catch (EmailException e) { 
      // TODO Auto-generated catch block 

      e.printStackTrace(); 
     } 
    } 
} 

Khi tôi chạy các ứng dụng tôi nhận được lỗi sau.

java.lang.NullPointerException 
at org.springframework.ui.velocity.VelocityEngineUtils.mergeTemplate(VelocityEngineUtils.java:58) 

làm động cơ tốc độ là không.

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> 

<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> 

Xin hãy giúp tôi. Có cấu hình nào khác mà tôi cần phải làm không?

+1

Bạn đã cấu hình đúng mùa xuân để sử dụng cấu hình XML, và cũng có chú thích cấu hình? –

+0

bạn đang chuyển cho mẫu của bạn những gì? Tôi nghi ngờ có thể có một vấn đề ở đó. – JasonG

+0

Ashish, bạn có thấy nhận xét của nico_ekito về cấu hình chú thích không? Bạn có '' trong XML ở đâu đó không? Nó không có trong đoạn mã cấu hình XML mẫu của bạn ở đây. – Rup

Trả lời

5

Tôi nghĩ bạn không thể sử dụng @Autowired với trường tĩnh. Bạn có thể làm việc xung quanh điều này với một setter không tĩnh:

@Autowired 
public void setVelocityEngine(VelocityEngine ve) { 
    EmailUtils.velocityEngine = ve; 
} 

hoặc theo cách khác nhưng tôi rất muốn giới thiệu chuyển EmailUtils vào một bean không tĩnh. Spring xử lý các thành phần không tĩnh tốt hơn nhiều (không có các hacks xấu xí cần thiết), nó sẽ có thể thực hiện hoán đổi và mã của bạn sẽ dính vào DI philosphy.

+0

Xin chào, cảm ơn trả lời của bạn. Tôi đã xóa tĩnh như bạn đã nói. Bây giờ tôi nhận được Không có bean phù hợp với loại [org.apache.velocity.app.VelocityEngine] được tìm thấy cho sự phụ thuộc: dự kiến ​​ít nhất 1 bean đủ điều kiện làm ứng cử viên tự động cho sự phụ thuộc này. trong khi triển khai ứng dụng. Hãy giúp tôi. – ashishjmeshram

+0

'VelocityEngineFactoryBean' tạo đối tượng kiểu' VelocityEngine'. Làm thế nào để bạn chỉ ra các tập tin ngữ cảnh được nạp? – mrembisz

+0

Tôi vẫn nhận được lỗi tương tự. – ashishjmeshram

8

Tôi gặp vấn đề tương tự khi sử dụng. Tôi có thể giải quyết nó theo cách sau:

@Configuration 
public class ApplicationConfiguration { 

    @Bean 
    public VelocityEngine getVelocityEngine() throws VelocityException, IOException{ 
     VelocityEngineFactory factory = new VelocityEngineFactory(); 
     Properties props = new Properties(); 
     props.put("resource.loader", "class"); 
     props.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); 
     factory.setVelocityProperties(props); 
     return factory.createVelocityEngine();  
    } 
} 

Nhưng thay vào đó - bởi vì tôi đã sử dụng mẫu vận tốc cho quan điểm của tôi và đã tuyên bố tình VelocityConfigurer, vì vậy tôi thay Autowired trong việc kê khai của tôi về VelocityConfigurer và gọi getVelocityEngine() trong vấn đề này.

Tôi đã phát hiện ra rằng, nếu nó được tự động lưu vào @ Service-s hoặc @ Component-s và bạn đã khai báo trong tệp sharedContext.xml được chia sẻ, thì khai báo xml phải nằm trong tệp sharedContext.xml được chia sẻ đó tốt hơn là cấu hình xxx-servlet.xml.

Tôi nghĩ rằng điều này là do applicationContext.xml được chia sẻ giữa tất cả các servlet trong ứng dụng, vì vậy nó phải được xử lý hoàn toàn trước các tệp xxx-servlet.xml.

Ngoài ra, bạn có thể cho phép xuân bean factory gỡ lỗi và xem nếu nó có bất kỳ vấn đề instantiating nó:

log4j.category.org.springframework.beans.factory=DEBUG 
1

Đây là nỗ lực của tôi, tôi khởi tạo "velocityEngine" đậu bằng tay qua ClassPathXmlApplicationContext:

Spring application-context.xml

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

Đây là template_text.vm của tôi, đặt nó trong classpath, cùng cấp với ứng dụng-conte xt.xml.

Dear $userName, 

You have made the following request on $userTime. 

#if($isFileMissing) 
Missing file(s) found in home directory: 
#foreach($missingFile in $missingFiles) 
- $missingFile 
#end 
#end 

Best regards, 

Đây là mã Java:

public static void main(String[] args) throws Exception { 
    String[] springConfig = {"application-context.xml"}; 
    org.springframework.context.ApplicationContext context = new org.springframework.context.support.ClassPathXmlApplicationContext(springConfig); 

    java.util.Map<String, Object> model = new java.util.HashMap<String, Object>(); 
    model.put("userName", "John Low"); 
    model.put("userTime", "2015-10-28 23:59:59"); 
    model.put("isFileMissing", true); 
    model.put("missingFiles", new String[] {"line 1", "line 2", "line 3"}); 
    String text = org.springframework.ui.velocity.VelocityEngineUtils.mergeTemplateIntoString((VelocityEngine)context.getBean("velocityEngine"), "/template_text.vm", "UTF-8", model); 
    System.out.println(text); 
} 

Sản lượng:

Dear John Low, 

You have made the following request on 2015-10-28 23:59:59. 

Missing file(s) found in home directory: 
- file 1 
- file AAA 
- line 3 

Best regards, 
Các vấn đề liên quan