ApplicationContextAware 获得不到Bean

a327369238 2016-08-09 11:40:52
写了一个类实现ApplicationContextAware去获取bean,完全是照着例子写的啊,怎么还是报空指针错误!代码如下:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

<display-name>spring-test</display-name>
<!-- spring config begin -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:config/spring/local/appcontext-*.xml
</param-value>
</context-param>
<!-- end -->

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>

appcontext-service.xml(config/spring/local下,idea的Resource下)
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
">
<bean id="springTest" class="com.fcc.spring.test.SpringTest" />

<bean id="helloWorld" class="com.fcc.spring.test.HelloWorld">
<property name="message" value="Hello World!" />
</bean>
</beans>

HelloWorld.java
package com.fcc.spring.test;

import java.io.Serializable;

public class HelloWorld implements Serializable{
private String message;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}

SpringTest.java
package com.fcc.spring.test;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import javax.annotation.Resource;

public class SpringTest implements ApplicationContextAware{

private static ApplicationContext applicationContext;


public static void main(String[] args){
HelloWorld obj = (HelloWorld)getBean("helloWorld");
System.out.println("obj = " + obj);
System.out.println("The message value is " + obj.getMessage());
}

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("applicationContext before : " + applicationContext);
SpringTest.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext(){
return applicationContext;
}
public static Object getBean(String name) throws BeansException{
System.out.println("applicationContext = " + applicationContext);
return applicationContext.getBean(name);
}
}

运行SpringTest的main方法报错:
applicationContext = null
Exception in thread "main" java.lang.NullPointerException
at com.fcc.spring.test.SpringTest.getBean(SpringTest.java:32)
at com.fcc.spring.test.ApplicationContextAwareTest.main(ApplicationContextAwareTest.java:8)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Process finished with exit code 1

不知道哪里错了,我理了下顺序,应该是这样的啊,但是就是不知道为什么,实现了ApplicationContextAware接口,但Spring初始化时候不回去初始化那个SpringTest的bean,连setApplicationContext()方法都没进,是哪里配置错了?
...全文
270 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
a327369238 2016-08-10
  • 打赏
  • 举报
回复
我知道问题所在了,我这个写的是控制器程序,根本就没有tomcat,也就不会读web.xml等一系列配置文件。。
科比请留步 2016-08-09
  • 打赏
  • 举报
回复
bean里面只有id啊
a327369238 2016-08-09
  • 打赏
  • 举报
回复
引用 3 楼 an_2016 的回复:
[quote=引用 2楼我是你的主体 的回复:][quote=引用 1 楼 an_2016 的回复:] bean里面只有id啊
第一个不就是只要id和class就好了么 第二个不是有属性注入么 [/quote]我手机看的,我好像就看到了id,没有class[/quote] 哦,有class的,你看看我的配置有问题没
科比请留步 2016-08-09
  • 打赏
  • 举报
回复
引用 2楼我是你的主体 的回复:
[quote=引用 1 楼 an_2016 的回复:] bean里面只有id啊
第一个不就是只要id和class就好了么 第二个不是有属性注入么 [/quote]我手机看的,我好像就看到了id,没有class
a327369238 2016-08-09
  • 打赏
  • 举报
回复
引用 1 楼 an_2016 的回复:
bean里面只有id啊
第一个不就是只要id和class就好了么 第二个不是有属性注入么

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧