英伟达gt730920A(2g显存)能玩GTA5吗?开低效行吗?cpu是i3 4g内存

12:52 提问
Spring无法获取ApplicationContext上下文对象怎么解决?
涉及到的类包括:
ITestService.java
2. TestServiceImp.java
3. autoWiringService.java
以及Spring配置文件:
4.autoWiring.xml
对应的源码为:
1. ITestService.java
package com.z.test.
* Created by Z on .
public interface ITestService {
public void annoTest();
2.TestServiceImp.java
package com.z.test.
import org.springframework.stereotype.S
* Created by Z on .
* 如何引入注解
@Service("testServiceImp")
public class TestServiceImp implements ITestService {
public void annoTest(){
System.out.println("注解调用");
3.autoWiringService.java
package com.z.test.
import org.springframework.context.ApplicationC
import org.springframework.context.support.ClassPathXmlApplicationC
import javax.annotation.R
* Created by Z on .
public class AutoWiringService {
@Resource(name ="testServiceImp")
ITestService iTestS
public void autoTest(){
iTestService.annoTest();
public static void main(String[] args){
ApplicationContext context =new ClassPathXmlApplicationContext("autowiring.xml");
AutoWiringService autoW=(AutoWiringService)context.getBean("autoWiringService");
autoW.autoTest();
使用了@Service 和 @Resource注解。目录结构:
在AutoWiringService运行主方法进行测试时,总是有错误如下:
猜测是否是无法读取xml文件?还是哪里没有配置好?
按赞数排序
autoWiring.xml文件是这样配置的:
&?xml version="1.0" encoding="UTF-8"?&
&beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cotent="http://www.springframework.org/schema/cache"
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/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/context
&!--Bean的自动装配--&
&!--&bean id="autoWiringDAO" class="com.z.autowiring.AutoWiringDAO" /&--&
&bean id="autoWiringService" class="com.z.test.annotation.AutoWiringService" /&
&!-- 扫描 哪些包或者类 需要用到注解 --&
&context:component-scan base-package="com.z.test.annotation" /&
&!-- 使用注解 --&
&cotent:annotation-driven/&
你在web.xml中配置了吗,检查一下,那边很重要
spring的配置文件是什么样的呢,有没有配置注解扫描的包?
其他相似问题2012年12月 移动平台大版内专家分月排行榜第三
本帖子已过去太久远了,不再提供回复功能。spring里头各种获取ApplicationContext的方法 - ImportNew
| 标签: ,
为啥写这个文章呢?spring各个版本不同,以及和系统框架套在一起不同,导致获取的方式不同,网络上各种版本,太乱了,写获取方式的人都不写这个获取方式是在本地还是在WEB,在那种应用服务器下,在spring那个版本下,太过分了!
我这写一些,常见的,可能经常要用的版本;
首先了解,为什么要获取这个东西:当你想通过spring获取一个你指定的类的实例的时候,而又没有通过spring加载到当前调用的类里面,例如你在filter里面,可能要对人员角色做判定,此时还没到业务层代码,但是又要访问数据库或其他的服务类。
然后再确保一点:这个context是一个全局变量,spring加载的时候,根handle信息就被装载,无论是本地应用程序还是web应用都是这样,下面分别说下如果是本地程序和其他情况的获取方式。
如果是main方法,你要启动spring,有很多方法,有基于annotation的注解来讲配置文件装载起来,当然,你想获取applicationCntext可在main方法中这样获取:
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource(&beans.xml&));//这样来加载配置文件
还有没有其他的方式呢?有的
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {&a.xml&, &b.xml&});
还有没有其他的?有
XmlWebApplicationContext context = new XmlWebApplicationContext();
context.setConfigLocations(new String[] {&aaa.xml& , &bb.xml&});
MockServletContext msc = new MockServletContext();
context.setServletContext(msc);
context.refresh();
msc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
其实方法差不多,他们有着继承关系,所以方法很多,你每次new的时候,相当于重新创建一个applicationContext,他会重新装载,所以不适合反复调用,如果自己new,你就应当把它放到一个全局变量中,用main启动的,当然你通过直接或间接的static应用到这个application即可。
而在WEB上呢,有一种是通过spring来加载spring本身的方式是:
通过实现接口:
org.springframework.context.ApplicationContextAware
然后spring反射,来源文章:/xuyang/blog/static/4/
这种方式适在spring 2、3当中均有效:
import org.springframework.beans.BeansE
import org.springframework.context.ApplicationC
import org.springframework.context.ApplicationContextA
import org.springframework.stereotype.S
public class SpringContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationC
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextHolder.applicationContext = applicationC
public static ApplicationContext getApplicationContext() {
return applicationC
public static Object getBean(String beanName) {
return applicationContext.getBean(beanName);
public static &T&T getBean(String beanName , Class&T&clazz) {
return applicationContext.getBean(beanName , clazz);
我这里是通过annotation注解的,如果不是annotation,那么可以通过配置文件:
&bean class=&xxx.xxx.xxx.SpringContextHolder&&&/bean&
来进行注入操作,结果一样,如果的spring配置中,没有设置byName的话,bean的配置里面记得要加参数来设置applicationContext来反射进去。
而你要加载spring,很多时候,并不是进入业务层的,因为反射是反射到业务层的,你还没有进入业务层,怎么来获取这个反射的东西呢?除非你反射的时候,用static变量来获取,那么就没有问题了;所以上面的例子中他也用的是static;
当你不想用static来反射,而经常想要用到它的时候,就有很多种获取方式了。
在spring 3以前的版本,我们在WEB应用中通常是这样获取的:
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(context);
而contexnt是什么呢?如果是servlet中,是可以直接通过getServletContext()获取,
而通过request要这样获取:
对于所有的tomcat通用的写法是:
ServletContext context = req.getSession().getServletContext();
对于tomcat 7以上的写法是(也就是tomcat 7可以直接从request中获取servletContext,tomcat6不行,必须通过session才可以):
ServletContext context = req.getServletContext();
其实从spring 3过后,获取的方法就有所改变,变得很诡异,因为竟然不兼容以前的获取方法,spring 3当中将其进行了进一步的包装,你在其他地方可能看到各种各样的版本。
spring 2中之所以可以那样获取,是因为spring 2当中通常会配置一个listener,由他来加载spring,他在filter之前;spring 3当中,通过org.springframework.web.servlet.DispatcherServlet来装载spring的信息,初始化在其父亲类:org.springframework.web.servlet.FrameworkServlet中方法:initWebApplicationContext();
跟踪方法明显看到内部获取增加了一个参数:
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext(),attrName);
这个参数是什么呢?
经过跟踪可以发现是:
FrameworkServlet.SERVLET_CONTEXT_PREFIX + getServletName()
而SERVLET_CONTEXT_PREFIX的定义是:
public static final String SERVLET_CONTEXT_PREFIX = FrameworkServlet.class.getName() + &.CONTEXT.&;
“org.springframework.web.servlet.FrameworkServlet.CONTEXT.”
而getServletName()呢?他是当前请求的servlet,可以获取到的一个web.xml里面配置的名称,例如,
如果你的web.xml中配置的是:
&servlet-name&spring&/servlet-name&
&servlet-class&org.springframework.web.servlet.DispatcherServlet&/servlet-class&
&load-on-startup&1&/load-on-startup&
&/servlet&
说明getServletName()的结果就是spring,否则就是其他,那么如果是spring,就是:
org.springframework.web.servlet.FrameworkServlet.CONTEXT.spring
ok,如果按照上面的配置,获取方式就是:
request.getSession().getServletContext().getAttribute(&org.springframework.web.servlet.FrameworkServlet.CONTEXT.spring&);
tomcat 7以上可以写成:
request.getServletContext().getAttribute(&org.springframework.web.servlet.FrameworkServlet.CONTEXT.spring&);
更为好的写法是:
request.getSession().getServletContext().getAttribute(FrameworkServlet.SERVLET_CONTEXT_PREFIX +&spring&);
以下为spring为了方便,做的一些扩展:
spring为了业务代码中获取这个参数方便,在进入业务代码前做了一个操作,在DispatcherServlet的方法:doService中doDispatch调用之前:
request.setAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE, getWebApplicationContext());
也就是,当你进入Controller以后,获取就不用那么麻烦了,你只需要这样就能获取到:
request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
当然,你可以将值写进去,看定义是:
public static final String WEB_APPLICATION_CONTEXT_ATTRIBUTE = DispatcherServlet.class.getName() + &.CONTEXT&;
那么值就应该是:
org.springframework.web.servlet.DispatcherServlet.CONTEXT
所以在Controller中你还可以这样来获取:
request.getAttribute(&org.springframework.web.servlet.DispatcherServlet.CONTEXT&)
经过spring包装后,你也可以通过:
RequestContextUtils.getWebApplicationContext(request , context)
来获取,源码如下:
其实它获取的方式和上面给的方法是一样的,RequestContextUtils.getWebApplicationContext在spring 3当中,如果没有启动ContextLoaderListener(当然你可以配置监听),是不会成功的。
ContextLoaderListener的简单配置为(web.xml中):
&listener&
&listener-class&org.springframework.web.context.ContextLoaderListener&/listener-class&
&/listener&
spring 3以后基本不这样配置了。
你这明显有问题,你这开辟了5个线程,每个线程都执行50万次操作,就等于=250000...
关于ImportNew
ImportNew 专注于 Java 技术分享。于日 11:11正式上线。是的,这是一个很特别的时刻 :)
ImportNew 由两个 Java 关键字 import 和 new 组成,意指:Java 开发者学习新知识的网站。 import 可认为是学习和吸收, new 则可认为是新知识、新技术圈子和新朋友……
新浪微博:
推荐微信号
反馈建议:@
广告与商务合作QQ:
– 好的话题、有启发的回复、值得信赖的圈子
– 写了文章?看干货?去头条!
– 为IT单身男女服务的征婚传播平台
– 优秀的工具资源导航
– 活跃 & 专业的翻译小组
– 国内外的精选博客文章
– UI,网页,交互和用户体验
– JavaScript, HTML5, CSS
– 专注Android技术分享
– 专注iOS技术分享
– 专注Java技术分享
– 专注Python技术分享
& 2017 ImportNewSpring中三种获取ApplicationContext的方法
package com.lc.
import org.springframework.context.ApplicationC
import org.springframework.context.support.FileSystemXmlApplicationC
public class App1 {
public static void main(String[] args) {
* 1.从ApplicationContext中取bean
//ApplicationContext ac=new ClassPathXmlApplicationContext("com/lc/ioc/beans.xml");
//当我们去实例化beans.xml,该文件中配置的bean被实例(该bean scope是 singleton)从bean中取出student
* 2.通过文件路径来获取ApplicationContext(用的并不多)
ApplicationContext ac=new FileSystemXmlApplicationContext("src\\com\\lc\\ioc\\beans.xml");
* 3.从XmlBeanFactory获得
//如果我们使用beanfactory去获取bean,当你只是实例化该容器, 那么容器的bean不被实例化,只有当你去使用getBean某个bean时,才会实时的创建.
BeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/lc/ioc/beans.xml"));
factory.getBean("student");
* 4.判断是不是单例的
//获取两个student
Student s1=(Student) ac.getBean("student");
Student s2=(Student) ac.getBean("student");
System.out.println(s1+" "+s2);通过监听器 获取Spring加载后的applicationContext - 江湖小虾米 - ITeye技术网站
最近遇到的是这样一个问题:
需要在Spring完成加载之后,获取到Spring管理的 ApplicationContext。
上网查了几个方法,大家都说实现接口ApplicationContextAware
实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。Spring初始化时,会通过该方法将ApplicationContext 对象注入。
但是实际操作中遇到了问题,配置了这个类,也加了SpringBean的配置,但是启动的时候,Spring没有帮我注入这个对象,我也不太明白为什么,希望有明白的大神们讲解一下。
PS:问题解决,参考于是我就用了这样一个方法实现:
还是实现了这样一个接口:
public class ApplicationContextHelper implements ApplicationContextAware
private static ApplicationC
public void setApplicationContext(ApplicationContext contex) throws BeansException
ApplicationContextHelper.context =
public static ApplicationContext getContext()
}
由于我前面遇到的问题是,spring启动的时候没有帮我注入这个ApplicationContext的对象,那么我就自己给它设进去,于是就在Spring启动的监听后面,自己加了一个监听
&listener&
&listener-class&org.springframework.web.context.ContextLoaderListener&/listener-class&
&/listener&
&listener&
&listener-class&com.kewen.util.GetContext&/listener-class&
&/listener&
监听类实现如下:
public class GetContext implements ServletContextListener
private static WebApplicationContext webApplicationC
private static ApplicationContextHelper helper = new ApplicationContextHelper();
public void contextDestroyed(ServletContextEvent arg0)
public void contextInitialized(ServletContextEvent arg0)
webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(arg0.getServletContext());
helper.setApplicationContext(webApplicationContext);
这样我们就可以在别的地方调用ApplicationContextHelper的getContext()方法,获取到需要的ApplicationContext对象了
浏览: 77300 次
点赞!!!简单实用
感谢楼主!按照你的做法成功了!
说得好啊,学习了
来江湖小虾米的博客学习一下,第5条心得写得好!佐佐成你好,佐佐 ...

我要回帖

更多关于 英伟达gt630 的文章

 

随机推荐