无法加载ApplicationContext(带有注释)
这是我上课的考试。
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = AppConfig.class, loader = AnnotationConfigContextLoader.class)
public class UserServiceImplIT {
@Autowired
private SampleService sampleService;
@BeforeClass
public static void setUp() {
System.out.println("-----> SETUP <-----");
}
@Test
public void testSampleService() {
assertTrue(true);
}
}
异常(许多异常,main: Failed to load ApplicationContext
,
Error creating bean with name 'defaultServletHandlerMapping'
。
Failed to instantiate [org.springframework.web.servlet.HandlerMapping]:
Factory method 'defaultServletHandlerMapping' threw exception;,
A ServletContext is required to configure default servlet handling
)
com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)上的JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)在sun.reflect.NativeMethodAccessorImpl.invoke0(Native
Method)处在sun.reflect.NativeMethodAccessorImpl。
com.intellij.rt.execution.application处java.lang.reflect.Method.invoke(Method.java:491)处sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)处invoke(NativeMethodAccessorImpl.java:57)
.AppMain.main(AppMain.java:140)由以下原因引起:org.springframework.beans.factory.BeanCreationException:创建类org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration:名称为’defaultServletHandlerMapping’的bean时出错通过工厂方法实例化失败;嵌套的异常是org.springframework.beans。BeanInstantiationException:无法实例化[org.springframework.web.servlet.HandlerMapping]:工厂方法’defaultServletHandlerMapping’抛出异常;嵌套异常是java.lang.IllegalArgumentException:在org.springframework.beans.factory.support上的org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)上配置默认servlet处理时,需要一个ServletContext。
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)处的AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory处
AppConfig-基本配置文件。
@Configuration@ComponentScan("ru.moneymanager.web")
@EnableWebMvc
@EnableTransactionManagement
@PropertySource(value = {"classpath:application.properties"})
@Import({SecurityConfig.class,})
public class AppConfig {
@Bean
public SampleService getSampleService() {
return new SampleServiceImpl();
}
@Bean
public InternalResourceViewResolver setupViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
@Autowired
private Environment environment;
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[]{"ru.moneymanager.web"});
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
return dataSource;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("hibernate.hbm2ddl.auto"));
return properties;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(s);
return txManager;
}
}
安全配置
@Configuration@ComponentScan("ru.moneymanager.web")
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
AuthenticationService authenticationService;
@Override
protected void configure(HttpSecurity http) throws Exception {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
filter.setForceEncoding(true);
// отключена защита csrf на время тестов
http.csrf().disable().addFilterBefore(filter,CsrfFilter.class);
http.authorizeRequests().antMatchers("/account/**").hasRole("USER")
.antMatchers("/user/**").hasRole("ADMIN")
.and().formLogin();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(authenticationService);
}
}
其他配置文件 在git project中配置
该项目, 告诉我,怎么了?为什么会出错?
回答:
您的测试需要一个ServletContext:add @WebIntegrationTest
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = AppConfig.class, loader = AnnotationConfigContextLoader.class)
@WebIntegrationTest
public class UserServiceImplIT
…或在此处查找其他选项:https :
//docs.spring.io/spring-boot/docs/current/reference/html/boot-features-
testing.html
@WebIntegrationTest
不再是首选。@SpringBootTest
要么@WebMvcTest
以上是 无法加载ApplicationContext(带有注释) 的全部内容, 来源链接: utcz.com/qa/427890.html