Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spring bean 初始化 #12

Open
blueskyzs opened this issue Jun 17, 2017 · 8 comments
Open

spring bean 初始化 #12

blueskyzs opened this issue Jun 17, 2017 · 8 comments

Comments

@blueskyzs
Copy link

blueskyzs commented Jun 17, 2017

请问,按照您的ssm项目配置,为什么所有bean都不是默认启动的时候初始化呢,而是在第一次发请求的时候所有bean才初始化.在您的配置文件中,并没有发现什么延迟初始化相关的配置。但是spring项目本身默认是启动的时候就初始化所有bean,实在没找到原因,请老师指点。 @codingXiaxw

@281613520
Copy link

估计是因为是webapplication初始化而不是rootapplication初始化的原因。web.xml在配置时,将配置文件配置在dispatcherservlet中初始化了,这样子避免了一个bean 使用范围的问题

@blueskyzs
Copy link
Author

我不是太明白你说的意思。其实我就是想让bean在服务启动的时候就初始化,不知道哪里出了问题

@281613520
Copy link

这样子,用org.springframework.web.context.ContextLoaderListener这个监听器加载dao和service,web在servlet中加载试试。

@281613520
Copy link

http://blog.csdn.net/hengyunabc/article/details/47072637 记得结果告诉我下。。

@blueskyzs
Copy link
Author

好的,谢谢先

@codingXiaxw
Copy link
Owner

http://codingxiaxw.cn/2016/11/11/39-Spring/ 看看我的这篇文章第四节“创建的细节”,通过在xml文件中设置bean的scope属性,可以控制bean在服务启动的时候就初始化或是我们需要使用这个pojo对象的时候再初始化

@blueskyzs
Copy link
Author

blueskyzs commented Jul 9, 2017

@codingXiaxw 可能是我没有描述清楚问题,为了测试,我在controller类加了默认构造方法打印一句话,服务启动后并没有打印这句话,这是问题现象;
问题解决了,需要在web.xml里面配置DispatcherServlet加上<load-on-startup>1</load-on-startup>

加上后,启动容器后就会打印controller默认构造方法那句话了。老师的列子中没有加哈。
我在Intellij idea下测试
@281613520

@codingXiaxw
Copy link
Owner

可以的小伙子

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants