我想替换我的spring应用程序的xml配置以获取注释样式。
我有这样的@Controller:
@Controller
@RequestMapping("/events")
public class EventController extends AbstractController{
@Autowired
EventService eventService;
Jaxb2Marshaller refMarsh;
///
}
在我有这样的xml配置之前:
<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.epam.hhsystem.model.candidate.Candidate</value>
<value>com.epam.hhsystem.model.vacancy.Vacancy</value>
<value>com.epam.hhsystem.model.event.Event</value>
</list>
</property>
</bean>
<bean id="eventController" class="com.epam.hhsystem.ws.controller.EventController">
<property name="jaxb2Mashaller" ref="refMarsh" />
</bean>
</beans>
我在注释风格上替换了第一个豆:
@Configuration
public class ContextConfiguration {
@Bean(name = "refMarsh")
public Jaxb2Marshaller jaxb2Marshaller() {
Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
jaxb2Marshaller.setClassesToBeBound(Event.class);
jaxb2Marshaller.setClassesToBeBound(Candidate.class);
jaxb2Marshaller.setClassesToBeBound(Vacancy.class);
return jaxb2Marshaller;
}
}
如何替换第二个bean?
答案 0 :(得分:0)
关键是如何配置Web配置是使用“WebApplicationInitializer”还是带有侦听器的web.xml,然后在配置中可以扩展WebMvcConfigurerAdapter或使用@EnableWebMvc,请检查这些YT视频
http://www.youtube.com/watch?v=-pV351E_stM
编辑2: 好的,你添加了@ComponentScan并添加了控制器的包吗?在视频中你必须使用@ComponentScan
答案 1 :(得分:0)
可能你想要这个:
@Bean(name = "eventController")
public EventController getEventController(@Qualifier("refMarsh") Jaxb2Marshaller marshaller) {
EventController controller = new EventController();
controller.setJaxb2Mashaller(marshaller);
return controller;
}