我正在尝试使用Springs java-config为我的应用程序上下文运行Junit功能测试.我不确定它是否是Spring或Junit的问题...只是不确定..应用程序在我的本地服务器上运行正常(击中数据库),但是当我尝试运行我的测试时它会爆炸.我刚刚从xml移动到java配置,所以我总是可以导入我的xml上下文文件(我知道在我的测试中可以工作),但是我想继续使用我的java配置.
我的测试班
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class,
classes = { AppConfig.class, ServletConfig.class })
@EnvironmentConfiguration(db2Enabled = true)
public class EventDAOTest {
Run Code Online (Sandbox Code Playgroud)
StackTrace
Feb 4, 2014 11:51:45 AM com.aoins.config.CachingPropertyFileReader <clinit>
SEVERE: ERROR - Configuration Framework not detected!
Feb 4, 2014 11:51:45 AM com.aoins.config.CachingPropertyFileReader readFile
SEVERE: Unable to build property reference for: (common).
2014-02-04 11:51:45,510 | DEBUG | : | LoggerConfigurer | Msg: Log4j configured with: 'C:/javalog/log4jPCProperties.xml'
2014-02-04 11:51:48,248 | INFO | : | EnvironmentConfigurer | Msg: DB2 Connection was established
2014-02-04 11:51:48,279 …Run Code Online (Sandbox Code Playgroud) 我想为Spring Boot项目添加基于方法的安全性.
似乎所有我需要是增加PermissionEvaluator和MethodSecurityExpressionHandler豆类,注释我WebSecurityConfigurerAdapter用@EnableGlobalMethodSecurity(prePostEnabled = true)和与方法@PreAuthorize("isAuthenticated() and hasPermission(#param, 'somePermissionName')").
但是在添加一个PermissionEvaluator豆之后
@Bean
public PermissionEvaluator permissionEvaluator() {
HelloPermissionEvaluator bean = new HelloPermissionEvaluator();
return bean;
}
Run Code Online (Sandbox Code Playgroud)
我得到一个IllegalArgumentException:"需要一个ServletContext来配置默认的servlet处理":
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultServletHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.web.servlet.HandlerMapping org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping()] threw exception; nested exception is java.lang.IllegalArgumentException: A ServletContext is required to configure default …Run Code Online (Sandbox Code Playgroud) 我有一个简单的弹簧启动应用程序已经运行:我可以卷曲到端点等.
我正在尝试按照弹簧引导参考指南(第115页)将其配置为使用HTTPS ,并将这样的代码添加到我的@Configuration类中:
@Bean
@Inject
public EmbeddedServletContainerCustomizer tomcatCustomizer(@Value("${keystore.file}") String keystoreFile,
@Value("${keystore.password}") String keystorePassword,
@Value("${keystore.type}") String keystoreType,
@Value("${keystore.alias}") String keystoreAlias) throws FileNotFoundException
{
final String absoluteKeystoreFile = ResourceUtils.getFile(keystoreFile).getAbsolutePath();
EmbeddedServletContainerCustomizer tomcatCustomizer = (ConfigurableEmbeddedServletContainer factory) -> {
TomcatEmbeddedServletContainerFactory containerFactory = (TomcatEmbeddedServletContainerFactory) factory;
containerFactory.addConnectorCustomizers((TomcatConnectorCustomizer) (Connector connector) -> {
connector.setSecure(true);
connector.setScheme("https");
connector.setAttribute("keystoreFile", absoluteKeystoreFile);
connector.setAttribute("keystorePass", keystorePassword);
connector.setAttribute("keystoreType", keystoreType);
connector.setAttribute("keyAlias", keystoreAlias);
connector.setAttribute("clientAuth", "false");
connector.setAttribute("sslProtocol", "TLS");
connector.setAttribute("SSLEnabled", true);
});
};
return tomcatCustomizer;
}
Run Code Online (Sandbox Code Playgroud)
但是,只有这个代码添加,我以前工作的应用程序现在获得以下堆栈跟踪:
Caused by: java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling …Run Code Online (Sandbox Code Playgroud) 我试图找出问题好几天但没有运气.我遇到了这个问题,但由于我没有在这里做任何测试,因此无法弄清楚是什么问题.java.lang.IllegalArgumentException:需要ServletContext来配置默认的servlet处理
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultServletHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'defaultServletHandlerMapping' threw exception; nested exception is java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1119) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1014) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
Run Code Online (Sandbox Code Playgroud)
我正在使用弹簧靴
@ComponentScan
@EnableAutoConfiguration
public class Application {
.....
}
public class ApplicationWebXml extends SpringBootServletInitializer { …Run Code Online (Sandbox Code Playgroud)