我正在尝试一个简单的例子,但它没有用。
实现Aware的ApplicationContextHolder类:
@Component
public class ApplicationContextHolder implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext context)
throws BeansException {
this.context = context;
}
public static ApplicationContext getApplicationContext(){
return context;
}
}
App.java
public static void main( String[] args )
{
ApplicationContext context = ApplicationContextHolder.getApplicationContext();
// context in above line is coming as null
}
下面是我的xml条目:
<context:component-scan base-package="com.XXX" />
请让我知道为什么context
在 App.jaa 类中显示为null。我是否需要为在xml文件中实现Aware
某个类的类创建一些条目?
答案 0 :(得分:4)
您必须首先初始化ApplicationContext
public static void main( String[] args )
{
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
// 'spring.xml' has to add ApplicationContextHolder to its scan path
ApplicationContext contextFromHolder =
ApplicationContextHolder.getApplicationContext();
}
答案 1 :(得分:1)
ApplicationContetAware 要由任何希望收到运行的ApplicationContext通知的对象实现的接口。
在你的情况下,没有。您必须首先通过Java代码引导Spring,因为这是一个独立/控制台应用程序。 您必须首先初始化applicationcontext / BeanFactory。
使用ApplicationContext(如果使用xml配置,最好使用ClassPathXmlApplicationContext)
ApplicationContext context = new ClassPathXmlApplicationContext(contextfile.xml);
ApplicationContextHolder 现在将具有当前ApplicationContext的引用。
答案 2 :(得分:0)
完整可行的示例如下:
MyContext.java
package jbr;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class MyContext implements ApplicationContextAware {
private static ApplicationContext ctx;
public void setApplicationContext(ApplicationContext context) throws BeansException {
ctx = context;
}
public static ApplicationContext getCtx() {
return ctx;
}
}
<强> Person.java 强>
package jbr;
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private int age;
}
<强> conf.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: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/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="jbr" />
<bean id="myCtx" class="jbr.MyContext" />
<bean id="person" class="jbr.Person">
<property name="name" value="Ranjith" />
<property name="age" value="23" />
</bean>
</beans>
<强> App.java 强>
package jbr;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("jbr/conf.xml");
Person p = (Person) context.getBean("person");
System.out.println(p.getName() + " > " + p.getAge());
ApplicationContext ctx = MyContext.getCtx();
Person p1 = (Person) ctx.getBean("person");
System.out.println(p1.getName());
}
}
<强>输出强>
May 11, 2017 3:26:15 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@37bba400: startup date [Thu May 11 15:26:14 IST 2017]; root of context hierarchy
May 11, 2017 3:26:15 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [jbr/conf.xml]
Ranjith > 23
Ranjith