Spring 将数据源 bean 注入或自动装配到类 [英] Spring injecting or autowiring datasource bean to class

查看:31
本文介绍了Spring 将数据源 bean 注入或自动装配到类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个非常新手的问题,但我已经搜索过,要么我的理解有很大差距,要么做错了一些我无法弄清楚的事情.

this may be a very novice question, but I have searched and either I have a large gap in my understanding or am doing something incorrectly that I cannot figure out.

在我的上下文文件中,这是一个摘录

In my context file here is an excerpt

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${datasource.driverClassName}" />
    <property name="url" value="${datasource.url}" />
    <property name="username" value="${datasource.username}" />
    <property name="password" value="${datasource.password}" />
</bean>

<bean id="myBeanOne" class="a.b.c.myBeanOne">
         <property name="dataSource" ref="dataSource" />
</bean>

现在在 myBeanOne 中:

Now in myBeanOne I have:

private DataSource dataSource;

private JdbcTemplate jdbcTemplate;

@Autowired
public void setDataSource (DataSource dataSource) {
    this.jdbcTemplate = new JdbcTemplate(dataSource);
}

public void myMethod() {
    String sql = "'My generic SQL update query'";
    try {
        this.jdbcTemplate.update(sql);
    } catch (org.springframework.dao.EmptyResultDataAccessException ex) {
    }
    System.exit(0);
}

当我尝试在调用 setDataSource 的行上执行此操作时,出现此错误:

when I try to execute this on the line where setDataSource is invoked I get this error:

ERROR org.springframework.integration.handler.LoggingHandler 
    org.springframework.integration.MessageHandlingException: 
       java.lang.NullPointerException

上线:this.jdbcTemplate.update(sql);

我已经尝试了十种不同的配置来让它工作,但我似乎无法做到.感谢您提供任何帮助,谢谢.

I have tried maybe ten different configurations to get this to work, but I cannot seem to do it. Any assistance is appreciated, thank you.

根据 Luiggi 的评论:

as per Luiggi's comment:

//in yet another classes run method
myBeanOne bOne = SomeOtherClass.create();   //just returns new myBeanOne
bOne.myMethod();

SomeOtherClass 或此类在上下文中均未归类为 bean 或在上下文中没有任何存在.

Neither SomeOtherClass or this class are classified as beans in the context or have any presence in the context.

我知道这是一个非常基本的问题,但我正在努力解决这个问题.

I know that this is a very basic question but I am struggling with it.

感谢您的耐心等待.

推荐答案

如评论中所述,问题在于您手动创建 bean 而不是让 Spring 容器创建它.基本上,您正在这样做:

As noted in comments, the problem is that you're manually creating the bean instead of letting Spring container create it. Basically, you're doing this:

new MyBeanOne()

因此 Spring 容器无法注入您配置的任何字段,因此是 null 例如jdbcTemplate 字段.有一些解决方案:

So Spring container can't inject any of the fields you have configured thus being null e.g. jdbcTemplate field. There are some solutions to this:

  1. 将您的 SomeOtherClass 转换为 Spring 容器管理的 bean,并让它注入 MyBeanOne 实例(可能使用 @Autowired 注释).

  1. Convert your SomeOtherClass into a bean managed by Spring container and let it inject the MyBeanOne instance (probably using @Autowired annotation).

如果由于您需要手动创建 bean 而无法使用后一种方法,您可以手动创建 bean,如下所示:如何动态创建spring bean?

If latter approach can't be done since you need to manually create the bean, you can create the bean manually as shown here: How to create spring beans dynamically?

但是这个实现让你在某处硬编码 spring 配置文件名并在你的代码中使用它.因此,更好的方法是选项 3.

But this implementation makes you hardcode somewhere the spring config file name and use it in your code. So, a better approach would be option 3.

看看这个解决方案:按需创建新的 Spring Beans,您可以在其中创建一个客户端抽象类,其中包含一个 Spring 将实现的方法来检索 Spring 托管 bean 的新实例.

Look at this solution: Creating New Spring Beans on Demand, where you create a client abstract class with a method that Spring will implement to retrieve a new instance of your Spring managed bean.

<小时>

我找到了另一种使用 @Configurable 注释来处理这个问题的方法.通过用这个注解装饰你的 bean,你可以按需创建一个 bean 的新实例,Spring 将为你管理 Spring 托管 bean 的注入.但要实现这一点,Spring 需要在幕后使用方面,您应该为您的项目激活方面的使用.解释很长,所以我提供了深入解释这个解决方案的链接:


I found another way to handle this by using @Configurable annotation. By decorating your bean with this annotation, you can create a new instance of the bean on demand and Spring will manage the injection of Spring managed beans for you. But to achieve this, Spring needs to use aspects behind the scenes and you should activate usage of aspects for your project. The explanation is quite long, so I provide links that explain in depth this solution:

请注意,为了启用此功能,您必须在启动 JVM 时添加一个 Java 代理,以便在运行时使用方面来编织类.

Note that in order to enable this feature, you have to add a java agent when starting the JVM that will weave the class at runtime using aspects.

这篇关于Spring 将数据源 bean 注入或自动装配到类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆