当spring控制器处理请求时,服务未连接:
@Controller
@RequestMapping(value = "/login")
public class LoginController {
@Inject
private AccountService accountService;
@RequestMapping(method = RequestMethod.POST)
public String handleLogin(HttpServletRequest request, HttpSession session){
try {
...
//Next line throws NullPointerException, this.accountService is null
Account account = this.accountService.login(username, password);
} catch (RuntimeException e) {
request.setAttribute("exception", e);
return "login";
}
}
}
AccountService 及其唯一的实现在模块中定义service
为:
package com.savdev.springmvcexample.service;
...
public interface AccountService {
...
package com.savdev.springmvcexample.service;
@Service("accountService")
@Repository
@Transactional
public class AccountServiceImpl implements AccountService {
Web 配置由位于web
模块中的文件加载:
package com.savdev.springmvcexample.web.config;
public class SpringMvcExampleWebApplicationInitializer implements WebApplicationInitializer
{
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
registerDispatcherServlet(servletContext);
}
private void registerDispatcherServlet(final ServletContext servletContext) {
WebApplicationContext dispatcherContext = createContext(WebMvcContextConfiguration.class);
...
}
private WebApplicationContext createContext(final Class<?>... annotatedClasses) {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(annotatedClasses);
return context;
}
将包扫描到发现 bean 的 WebMvcContextConfiguration 文件:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.savdev.springmvcexample.web", "com.savdev.springmvcexample.service" })
public class WebMvcContextConfiguration extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setOrder(1);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
加载此类,导致根据 InternalResourceViewResolver 逻辑使用视图解析。结果,扫描了“com.savdev.springmvcexample.web”,因为找到了处理请求的控制器。
扫描了“com.savdev.springmvcexample.service”,但它在另一个模块中,我不知道这是否是一个问题,但我没有收到任何错误。
更新:
@JBNizet,模块 - 表示 Maven 多模块项目中的模块。我已经删除了@Repository,现在我在测试中遇到错误:
NoSuchBeanDefinitionException:没有为依赖项找到类型为 [javax.sql.DataSource] 的合格 bean。
这意味着,这意味着弹簧轮廓未激活。仅为配置文件加载数据源。
在 Web 基础架构中,我使用以下方式管理配置文件:
public class SpringMvcExampleProfilesInitializer implements ApplicationContextInitializer<ConfigurableWebApplicationContext> {
@Override
public void initialize(ConfigurableWebApplicationContext ctx) {
ConfigurableEnvironment environment = ctx.getEnvironment();
List<String> profiles = new ArrayList<String>(getProfiles());
if( profiles == null || profiles.isEmpty() )
{
throw new IllegalArgumentException("Profiles have not been configured");
}
environment.setActiveProfiles(profiles.toArray( new String[0]));
}
//TODO add logic
private Collection<String> getProfiles() {
return Lists.newArrayList("file_based", "test_data");
}
}
如果我没记错的话 SpringMvcExampleProfilesInitializer 在加载 Spring ApplicationContext 之前使用。它是自动制作的。无需为此配置任何其他内容。但它不起作用。请纠正我,如果我错了。
请注意,初始化程序具有以下签名:
SpringMvcExampleProfilesInitializer implements ApplicationContextInitializer<ConfigurableWebApplicationContext>
在配置 DispatcherServlet 时,我可以使用以下方法进行设置:
setContextInitializers(ApplicationContextInitializer<ConfigurableApplicationContext>... contextInitializers)
如何设置 setContextInitializers 但传递一些实现ApplicationContextInitializer<ConfigurableWebApplicationContext>
但不传递的东西ApplicationContextInitializer<ConfigurableApplicationContext>