I have the following class:
#Configuration
#EnableWebSocketMessageBroker
#EnableScheduling
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
#Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableStompBrokerRelay(
"/topic",
"/queue/");
config.setApplicationDestinationPrefixes("/app");
}
#Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint(
"/wsdemo").withSockJS();
}
}
I would like to be able to NOT configure the class above whenever I'm running tests. is that possible?
Thanks!
A plain Junit test (without the spring runner) will ensure the class is not configured. you can then use mock objects (see Mockito) to satisfy any dependencies.
If you have separate "application-context.xml" for your tests, then there you must have directive:
<context:component-scan base-package="...">
...
</context:component-scan>
Modify it as below:
<context:component-scan base-package="...">
...
<context:exclude-filter type="regex" expression="{package}.WebSocketConfig"/>
</context:component-scan>
Related
How to configure an Interceptor, through Annotation only(I do NOT like to register the interceptor in .XML file, I do not use .XML based configuration)?
Note : I see in example on internet it is suggesting to use org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter, when I tried to use it, I found it is DEPRECATED
I am testing on SpringWebMVC-5 with SpringBoot-2
In Spring5 you can use org.springframework.web.servlet.config.annotation.WebMvcConfigurer:
#EnableWebMvc
#Configuration
public class WebConfig implements WebMvcConfigurer {
#Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(....);
}
}
In spring the resource handler is working fine
<mvc:resources mapping="/Lab/**" location="/WEB-INF/Assets/Lab/"/>
<mvc:resources mapping="/Tools/**" location="/WEB-INF/Assets/Tools/"/>
<mvc:resources mapping="/Images/**" location="/WEB-INF/Assets/Images/"/>
How can i add multiple resources in spring boot?
The below code is not working
#Configuration
#EnableWebMvc
public class ResourceHandlers extends WebMvcConfigurerAdapter
{
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{
registry
.addResourceHandler("/Lab/**")
.addResourceLocations("/WEB-INF/Assets/Lab/");
registry
.addResourceHandler("/Tools/**")
.addResourceLocations("/WEB-INF/Assets/Tools/");
registry
.addResourceHandler("/Images/**")
.addResourceLocations("/WEB-INF/Assets/Images/");
}
}
registry
.addResourceHandler("/Lab/**", "/Tools/**", "/Images/**")
.addResourceLocations("/WEB-INF/Assets/Lab/",
"/WEB-INF/Assets/Tools/",
"/WEB-INF/Assets/Images/");
It allows multiple arguments
I want to provide different database for running unitTest than using default production database. I thought about using profile to solve this issue.
This is spring4 boot project, so everything is annotated.
This is what I am doing:
Under src/main/resources, I put application.properties:
spring.datasource.url=jdbc:postgresql://localhost:5432/services
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.driver-class-name=org.postgresql.Driver
Under src/test/resources, I put application-test.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/services_test
spring.datasource.username=postgres
spring.datasource.password=Hercules1
spring.datasource.driver-class-name=org.postgresql.Driver
Then, I put #ActiveProfiles("test") before test, now when I run the unit test, I immediately meet this error:
java.lang.IllegalStateException: Failed to load ApplicationContext
I googled quite a lot and nothing can solve this error.
Can you point out what's wrong with my solution?
Thanks
Just suffixing -test to application.properties will not make the properties a candidate for the profile that you activate. You need to do the following:
A datasource configuration interface:
public interface DatasourceConfig {
public void setup();
}
Test Datasource configuration:
#Component
#Profile("test")
public class ProductionDatasourceConfig implements DatasourceConfig {
#Override
public void setup() {
// Set up your test datasource
}
}
Production datasource configuration:
#Component
#Profile("prod")
public class ProductionDatasourceConfig implements DatasourceConfig {
#Override
public void setup() {
// Set up your prod datasource
}
}
Activate the profile:
#ActiveProfiles("test")
Inject the datasource based on environment:
#Autowired
DatasourceConfig datasourceConfig;
Beans declared in XML can also be mapped to the profile as below:
<beans profile="dev">
<bean id="devDatasourceConfig" class="org.profiles.DevDatasourceConfig" />
</beans>
<beans profile="prod">
<bean id="productionDatasourceConfig" class="org.profiles.ProductionDatasourceConfig" />
</beans>
I'm trying to upgrade my spring mvc project to utilize the new annotations and get rid of my xml. Previously I was loading my static resources in my web.xml with the line:
<mvc:resources mapping="/resources/**" location="/resources/" />
Now, I'm utilizing the WebApplicationInitializer class and #EnableWebMvc annotation to startup my service without any xml files, but can't seem to figure out how to load my resources.
Is there an annotation or new configuration to pull these resources back in without having to use xml?
For Spring 3 & 4:
One way to do this is to have your configuration class extend WebMvcConfigurerAdapter, then override the following method as such:
#Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
Spring 5
As of Spring 5, the correct way to do this is to simply implement the WebMvcConfigurer interface.
For example:
#Configuration
#EnableWebMvc
public class MyApplication implements WebMvcConfigurer {
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
See deprecated message in: WebMvcConfigurerAdapter
I am trying to autowire a class into a WebSocketServlet in the following way:
#Configurable(autowire=Autowire.BY_TYPE)
public class MyServlet extends WebSocketServlet {
#Autowired
public MyClass field;
// etc...
}
Here's what my configuration looks like:
<context:annotation-config />
<context:component-scan base-package="org.*" />
<bean id="config" class="org.*.MyClass">
<!-- a bunch of properties -->
</bean>
Note that autowire used to work just fine as long as I was in a Spring #Controller. I had to step out of that because i don't know how to map a WebSocketsServlet to a method of the #Controller as you do with regular servlets.
Any idea what I might be missing?
In order to use #Configurable, you need to have these line in tour context:
<context:load-time-weaver aspectj-weaving="true"/>
<context:spring-configured/>
<context:annotation-config />
<context:component-scan base-package="org.*" />
In addition, I think you must reference spring-aspect in the Import-Library section of your Manifest.
I didn't succeed to make it work, there is a post on this in the Virgo forum at Eclipse.
If you succeed, let me know how ;)
Getting rid of #Configurable and doing the following in the servlet init method does the trick:
#Override
public void init() throws ServletException {
super.init();
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
As per the spring documentation
Externalized values may be looked up by injecting the Spring Environment into a #Configuration class using the #Autowired or the #Inject annotation:
#Configuration
public class AppConfig {
#Inject Environment env;
#Bean
public MyBean myBean() {
MyBean myBean = new MyBean();
myBean.setName(env.getProperty("bean.name"));
return myBean;
}
}