Bean在applicationContext中,但@Autowired注释不起作用

时间:2019-02-21 15:19:52

标签: java spring kotlin datasource spring-jdbc

bean数据源位于我的bean工厂中,但是当我尝试访问数据源的方法时得到了NPE。似乎@Autowired注释不起作用

@Configuration
@EnableTransactionManagement
public class DataSourceConfiguration {

    @Autowired
    private DataSource dataSource;

    public static void main(String[] args) throws SQLException {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        showBeans(context);
        dataSourceDemo();
    }

    private static void showBeans(ApplicationContext context) {
        System.out.println(Arrays.toString(context.getBeanDefinitionNames()));
    }

    private static void dataSourceDemo() throws SQLException {
        DataSourceConfiguration demo = new DataSourceConfiguration();
        demo.showDataSource();
    }

    @Bean(destroyMethod = "close")
    public DataSource dataSource() throws Exception {
        Properties properties = new Properties();
        properties.setProperty("driverClassName", "com.mysql.jdbc.Driver");
        properties.setProperty("url", "jdbc:mysql://127.0.0.1:3306/orange?characterEncoding=utf-8");
        properties.setProperty("username", "mysql");
        properties.setProperty("password", "123456");
        return BasicDataSourceFactory.createDataSource(properties);
    }

    @Bean
    public PlatformTransactionManager transactionManager() throws Exception {
        return new DataSourceTransactionManager(dataSource());
//        return new DataSourceTransactionManager(dataSource);
    }

    public void showDataSource() throws SQLException {
        System.out.println("dataSource: " + dataSource.toString());
        Connection connection = dataSource.getConnection();
        System.out.println("connection: " + connection.toString());
        connection.close();
    }


}

然后我执行程序并获得如下输出:

Exception in thread "main" java.lang.NullPointerException
at com.pain.DataSourceConfiguration.showDataSource(DataSourceConfiguration.java:40)
at com.pain.DataSourceTest.dataSourceDemo(DataSourceTest.java:23)
at com.pain.DataSourceTest.main(DataSourceTest.java:14)

[dataSourceConfiguration,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,orgspring。 transaction.annotation.ProxyTransactionManagementConfiguration,org.springframework.transaction.config.internalTransactionAdvisor,transactionAttributeSource,transactionInterceptor,org.springframework.transaction.config.internalTransactionalEventListenerFactory,dataSource,transactionManager,org.springframework.aop.config.internalAutoProxyCreator]

2 个答案:

答案 0 :(得分:3)

您的问题就在这里

DataSourceConfiguration demo = new DataSourceConfiguration();

您是在不使用spring的AOP的情况下手动创建对象。这样DataSourceConfiguration不会被包裹在代理中,并且依赖管理也会失败。

要解决此问题,您必须在静态上下文之外的其他地方@Autowire

答案 1 :(得分:1)

放大,将程序驱动程序放在@Configuration类中并手动实例化该类似乎是超级错误!

您的配置类应该主要配置Bean,因此应如下所示:

@Configuration
@EnableTransactionManagement
public class DataSourceConfiguration {

    @Autowired
    private DataSource dataSource;

    @Bean(destroyMethod = "close")
    public DataSource dataSource() throws Exception {
        Properties properties = new Properties();
        properties.setProperty("driverClassName", "com.mysql.jdbc.Driver");
        properties.setProperty("url", "jdbc:mysql://127.0.0.1:3306/orange?characterEncoding=utf-8");
        properties.setProperty("username", "mysql");
        properties.setProperty("password", "123456");
        return BasicDataSourceFactory.createDataSource(properties);
    }

    @Bean
    public PlatformTransactionManager transactionManager() throws Exception {
        return new DataSourceTransactionManager(dataSource());
    }
}

如果您使用的是普通弹簧,则可以完成所需的操作,显示有关数据源的信息,并执行以下操作。以这种方式将spring用作服务定位器非常简单,甚至可能是反模式):

public class MainCaller {

    public static void main(String[] args) {

       ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-context.xml");
       DataSource ds = (DataSource)applicationContext.getBean("DataSource");
       showDataSource(ds);

    }
     public static void showDataSource(Datasource dataSource) throws SQLException {
        System.out.println("dataSource: " + dataSource.toString());
        Connection connection = dataSource.getConnection();
        System.out.println("connection: " + connection.toString());
        connection.close();
    }

}