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

查看:143
本文介绍了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。更新(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?

但是这个实现使你硬编码弹簧配置文件名并在你的代码中使用它。因此,更好的方法是选项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 Bean,您可以使用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:

  • Spring Framework: 7.8 Using AspectJ with Spring applications
  • Using Spring's @Configurable in three easy steps

请注意,要启用此功能,您必须添加启动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天全站免登陆