springmvc后java有哪些新兴的mvc框架
答案:2 悬赏:70
解决时间 2021-02-04 00:49
- 提问者网友:挣扎重来
- 2021-02-03 10:42
springmvc后java有哪些新兴的mvc框架
最佳答案
- 二级知识专家网友:恕我颓废
- 2021-02-03 12:01
SSH、SSI/SSM、现在很多企业都用SpringMVC结合自己的框架开发项目
全部回答
- 1楼网友:一场云烟
- 2021-02-03 13:05
可以有以下几种方法
方法一:在初始化时保存applicationcontext对象
代码:
applicationcontext ac = new filesystemxmlapplicationcontext("applicationcontext.xml");
ac.getbean("beanid");
说明:这种方式适用于采用spring框架的独立应用程序,需要程序通过配置文件手工初始化spring的情况。
方法二:通过spring提供的工具类获取applicationcontext对象
代码:
import org.springframework.web.context.support.webapplicationcontextutils;
applicationcontext ac1 = webapplicationcontextutils.getrequiredwebapplicationcontext(servletcontext sc);
applicationcontext ac2 = webapplicationcontextutils.getwebapplicationcontext(servletcontext sc);
ac1.getbean("beanid");
ac2.getbean("beanid");
说明:
这种方式适合于采用spring框架的b/s系统,通过servletcontext对象获取applicationcontext对象,然后在通过它获取需要的类实例。
上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。
其中 servletcontext sc 可以具体 换成 servlet.getservletcontext()或者 this.getservletcontext() 或者 request.getsession().getservletcontext(); 另外,由于spring是注入的对象放在servletcontext中的,所以可以直接在servletcontext取出 webapplicationcontext 对象: webapplicationcontext webapplicationcontext = (webapplicationcontext) servletcontext.getattribute(webapplicationcontext.root_web_application_context_attribute);
方法三:继承自抽象类applicationobjectsupport
说明:抽象类applicationobjectsupport提供getapplicationcontext()方法,可以方便的获取到applicationcontext。
spring初始化时,会通过该抽象类的setapplicationcontext(applicationcontext context)方法将applicationcontext 对象注入。
方法四:继承自抽象类webapplicationobjectsupport
说明:类似上面方法,调用getwebapplicationcontext()获取webapplicationcontext
方法五:实现接口applicationcontextaware
说明:实现该接口的setapplicationcontext(applicationcontext context)方法,并保存applicationcontext 对象。
spring初始化时,会通过该方法将applicationcontext对象注入
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯