我已经更新了以下文件。我的问题是:
错误消息是什么意思?
在E.java的MethodE()中,我能够执行最后两行,
this.b = new B(this.entities); this.b.MethodA();
而不是使用new,使用Spring Annotation? 谢谢你们!
main.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class main {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("spring.xml");
E obj = (E) context.getBean("e");
obj.MethodE();;
}
}
A.java
import java.util.Collection;
public abstract class A<T> {
private Collection<T> entities;
public A(Collection<T> entities){
this.entities = entities;
}
public void MethodA() {
//To process entities such as save them to Database by using Hibernate.
System.out.println("this is MethodA of Class A");
}
}
B.java
import java.util.Collection;
import java.util.Iterator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Component;
@SuppressWarnings({ "hiding", "rawtypes" })
@Component
public class B<Class> extends A {
private C c;
private D d;
private Collection<Class> entities;
@SuppressWarnings("unchecked")
public B(Collection<Class> entities) {
super(entities);
// TODO Auto-generated constructor stub
this.entities = entities;
for(Iterator iterator = entities.iterator(); iterator.hasNext();){
if (iterator.next() instanceof C)
this.c = (C) iterator.next();
if(iterator.next() instanceof D)
this.d = (D) iterator.next();
}
}
public C getC() {
return c;
}
@Autowired
public void setC(C c) {
this.c = c;
}
public D getD() {
return d;
}
@Autowired
public void setD(D d) {
this.d = d;
}
@Override
public void MethodA() {
System.out.println("this is override methodA to see if injection works." + this.d.getStrAttr1());
}
}
C.java
import org.springframework.stereotype.Component;
@Component
public class C {
private String strAttr1;
private int iAttr2;
public String getStrAttr1() {
return strAttr1;
}
public void setStrAttr1(String strAttr1) {
this.strAttr1 = strAttr1;
}
public int getiAttr2() {
return iAttr2;
}
public void setiAttr2(int iAttr2) {
this.iAttr2 = iAttr2;
}
}
D.java
import org.springframework.stereotype.Component;
@Component
public class D {
private String strAttr1;
private int iAttr2;
public String getStrAttr1() {
return strAttr1;
}
public void setStrAttr1(String strAttr1) {
this.strAttr1 = strAttr1;
}
public int getiAttr2() {
return iAttr2;
}
public void setiAttr2(int iAttr2) {
this.iAttr2 = iAttr2;
}
}
E.java
import java.util.Collection;
import org.springframework.stereotype.Component;
@Component
public class E {
private C c;
private D d;
private B b;
private Collection<Class> entities;
public E() {
}
@SuppressWarnings("unchecked")
public void MethodE(){
this.c.setStrAttr1("this is my class C's attr1");
this.c.setiAttr2(9);
this.d.setStrAttr1("this is my class D's attr1");
this.d.setiAttr2(7);
this.entities.addAll((Collection<? extends Class>) this.c);
this.entities.addAll((Collection<? extends Class>) this.d);
this.b = new B(this.entities);
this.b.MethodA();
}
}
spring.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd"
default-autowire="byName">
<context:annotation-config/>
<context:component-scan base-package=""/>
</beans>
错误讯息:
Exception in thread "main" org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException) (Caused by org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException))
at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:543)
at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:235)
at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:209)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:351)
at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:145)
at org.springframework.context.support.AbstractRefreshableApplicationContext.<init>(AbstractRefreshableApplicationContext.java:84)
at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.<init>(AbstractRefreshableConfigApplicationContext.java:59)
at org.springframework.context.support.AbstractXmlApplicationContext.<init>(AbstractXmlApplicationContext.java:58)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:136)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at main.main(main.java:9)
Caused by: org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException)
at org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:397)
at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:529)
... 10 more
Caused by: java.lang.NullPointerException
at org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:374)
... 11 more