2016-11-22 107 views
0

我想在jaxbmarshaler中自動導入以用於我的spring啓動應用程序。這似乎並不能說明編組人員。以下是我目前擁有,任何tipson什麼我缺少jaxbmarshaller spring boot找不到bean?

Maven的依賴性:

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-oxm</artifactId> 
</dependency> 

--- code 

@Component 

    public class DoSomethingService { 

     @Autowired 
     private Jaxb2Marshaller marshaller; 
     marshaller.marshal(MyObject,Stringwriter) 

我得到的是說它無法找到Jaxb2Marsaller豆,任何想法的錯誤?

回答

0

爲了使用自動連線,首先需要創建一個Marshaller bean,將必要的類/包指向編組。然後你可以注入它。

0

您需要有一個或多個配置類,其中應該可用於自動裝配的所有bean應該定義。它類似於Spring中的ApplicationContext XML

<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"/> 

在java中的配置定義的bean:

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.oxm.jaxb.Jaxb2Marshaller; 

@Configuration 
public class SomeName{ 
@Bean 
    public Jaxb2Marshaller marshaller() { 
     Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); 
     //any setters 
     return marshaller; 
    } 
} 
+0

由於在加入這一點,它現在的工作。此外,我不得不添加我想要掃描的軟件包,並且它可以正常工作 marshaller.setPackagesToScan(「package」); – qwertyqwerty