注入时环境为空

时间:2015-06-30 21:49:33

标签: java spring

这个问题已被问过几次,但他们都在使用@Configuration注释。对于我正在做的事情,这不是必要的。我正在使用单例模式来创建与数据库的连接。但是我收到以下错误:

[ERROR] com.htd.aop.logging.LoggingAspect - Exception in com.htd.web.rest.PoResource.generateJobTicket() with cause = null
java.lang.NullPointerException: null
    at com.htd.config.DataSourceDAO.<init>(DataSourceDAO.java:24) ~[classes/:na]
    at com.htd.config.DataSourceDAO.getInstance(DataSourceDAO.java:35) ~[classes/:na]
    at com.htd.repository.PoDataSource.<init>(PoDataSource.java:12) ~[classes/:na]
    at com.htd.web.rest.util.createJobTicket.<init>(createJobTicket.java:16) ~[classes/:na]
    at com.htd.web.rest.PoResource.generateJobTicket(PoResource.java:339) ~[classes/:na]
    at com.htd.web.rest.PoResource$$FastClassBySpringCGLIB$$cfcd338a.invoke(<generated>) ~[spring-core-4.1.6.RELEASE.jar:na]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717) ~[spring-aop-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85) ~[spring-aop-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at com.htd.aop.logging.LoggingAspect.logAround(LoggingAspect.java:49) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_31]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_31]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_31]
    at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_31]

错误告诉我看看我的DataSourceDAO类的第24行,因为它是null。它指的是以下内容:

// Step 1: JDBC props       
    String DB_URL = env.getProperty("spring.datasource.url");
    String USER = env.getProperty("spring.datasource.username");
    String PASS = env.getProperty("spring.datasource.password");

完整代码:

public class DataSourceDAO {

    @Inject
    private Environment env;

    private static DataSourceDAO instance;

     java.sql.Connection conn = null;
     java.sql.Statement stmt = null;

    // Step 1: JDBC props       
    String DB_URL = env.getProperty("spring.datasource.url");
    String USER = env.getProperty("spring.datasource.username");
    String PASS = env.getProperty("spring.datasource.password");

    private DataSourceDAO() { }

    public static DataSourceDAO getInstance() {

        if (instance == null) {
            synchronized (DataSourceDAO.class) {
                if (instance == null) {
                    instance = new DataSourceDAO();
                }
            }
        }
        return instance;
    }

    public java.sql.Connection getConnection() throws ClassNotFoundException,
            SQLException {

        Class.forName("com.mysql.jdbc.Driver");

        if (conn == null) {

            conn = DriverManager.getConnection(DB_URL, USER, PASS);

        }

        return conn;
    }
}

我不确定为什么它会为空。 @Inject应该可以正常工作,或者我认为。

1 个答案:

答案 0 :(得分:1)

首先,你不会在手动实例化的类中注入任何东西。

修复了类的手动初始化后,你应该注意到在构造函数中你没有可用的注入属性,它们都是在实例化之后设置的。

如果您想在实例化对象后执行某些操作,则应使用@PostConstruct回调(http://www.mkyong.com/spring/spring-postconstruct-and-predestroy-example/