خرید بک لینک

Vote count: 0

I have a spring application which has come a long way now. I recently implemented ServerSocket and that is where things began to act up. My ServerSocket loads fine and i can coect to it easly, but for some reason the constructor is called multiple times(which causes port issues). The class is created as a bean in the "WebAppConfig extends WebMvcConfigurerAdapter" class, which is where the constructor is called. I did some testing and found out that the WebMvcConfigurerAdapter is loaded two or more times(By adding a println to its constructor). Why this happens is my question.

I use a combination of xml and aotations in my project. I want to go over to only aotation based spring, but when i tried this i was unable to control the session timeout. Because of this i have stuck to this combination and never thought much more of it before now. Can this combination cause the WebMvcConfigurerAdapter to be loaded multiple times?

Here is my xml configurations: web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">
    <session-config>
        <session-timeout>
            60
        </session-timeout>
    </session-config>
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-patte>/</url-patte>
    </servlet-mapping>
</web-app>

Spring-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

<context:component-scan base-package="com"/>


</beans>

My WebAppConfig class looks like this:

@Configuration
@ComponentScan("com")
@EnableWebMvc
public class WebAppConfig extends WebMvcConfigurerAdapter {
    private static int startups = 0;

    public WebAppConfig(){
        System.out.println(++startups);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("resources/**").addResourceLocations("resources/"); //.setCachePeriod(31556926);
    }

    @Bean
    public IntealResourceViewResolver viewResolver() {
        IntealResourceViewResolver viewResolver = new IntealResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/view/");
        viewResolver.setSuffix(".jsp");
        retu viewResolver;
    }

    //This is the ServerSocket Bean.
    @Bean
    public PlayerCoections playerCoections(){
        retu new PlayerCoections();
    }
}

The only place i have written anything about WebAppConfig is in the SecurityConfig class where i use the aotation "@Import({WebAppConfig.class})" before the public class definition.

Almost solved When i wrote this question I just had to try to remove to @Import aotation from the securityConfig class. this solved the problem above! The WebAppConfig class now loads just 1 time!

I say almost solved because I still wounder how this will change my application. Is the import aontation even needed? All the security configurations in the SecurityConfig class still works... Here is the SecurityConfig class:

@Configuration
@EnableWebSecurity
//@Import({WebAppConfig.class})
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("stackoverflow").password("stackoverflow").roles("ADMIN");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .....
    }
}

asked 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: يکشنبه 29 فروردين 1395 ساعت: 21:19

صفحه بندی