相关疑难解决方法(0)

如何将属性值注入使用注释配置的Spring Bean?

我有一堆Spring bean,它们是通过注释从类路径中获取的,例如

@Repository("personDao")
public class PersonDaoImpl extends AbstractDaoImpl implements PersonDao {
    // Implementation omitted
}
Run Code Online (Sandbox Code Playgroud)

在Spring XML文件中,定义了一个PropertyPlaceholderConfigurer:

<bean id="propertyConfigurer" 
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="/WEB-INF/app.properties" />
</bean> 
Run Code Online (Sandbox Code Playgroud)

我想将app.properites中的一个属性注入上面显示的bean中.我不能简单地做一些事情

<bean class="com.example.PersonDaoImpl">
    <property name="maxResults" value="${results.max}"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

因为PersonDaoImpl在Spring XML文件中没有特征(它是通过注释从类路径中获取的).我有以下几点:

@Repository("personDao")
public class PersonDaoImpl extends AbstractDaoImpl implements PersonDao {

    @Resource(name = "propertyConfigurer")
    protected void setProperties(PropertyPlaceholderConfigurer ppc) {
    // Now how do I access results.max? 
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我不清楚我如何访问我感兴趣的房产ppc?

java spring dependency-injection

285
推荐指数
11
解决办法
66万
查看次数

获取Spring应用程序上下文

有没有办法在Spring应用程序中静态/全局请求ApplicationContext的副本?

假设主类启动并初始化应用程序上下文,是否需要将它通过调用堆栈传递给任何需要它的类,或者有没有办法让类询问先前创建的上下文?(我认为必须是单身?)

java configuration spring

210
推荐指数
10
解决办法
32万
查看次数

在liquibase CustomTaskChange类中使用其他spring bean

我需要做一些数据迁移,这在liquibase变更集中太复杂了.我们用春天

这就是为什么我写了一个实现liquibase.change.custom.CustomTaskChange类的类.然后我从变更集中引用它.

到目前为止一切都很好.

我的问题是: 是否可以从这样的类中访问其他spring bean?

当我尝试在这个类中使用一个自动装配的bean时,它是null,这让我觉得现在根本没有完成自动装配?

我还在其他一些帖子中读到,必须在所有其他bean之前初始化Liquibase bean,这是正确的吗?

这是我写的类的片段:

@Component
public class UpdateJob2 implements CustomTaskChange {

private String param1;

@Autowired
private SomeBean someBean;

@Override
public void execute(Database database) throws CustomChangeException {
    try {
        List<SomeObject> titleTypes = someBean.getSomeObjects(
                param1
        );
    } catch (Exception e) {         
        throw new CustomChangeException();
    }
...
Run Code Online (Sandbox Code Playgroud)

我得到一个异常,在调试时我可以看到someBean为null.

这是SpringLiquibase的配置:

@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
@ComponentScan({
"xxx.xxx.."})
public class DatabaseConfiguration {

@Bean
public SpringLiquibase springLiquibase() {
    SpringLiquibase liquibase = new SpringLiquibase();
    liquibase.setDataSource(dataSource());
    liquibase.setChangeLog("classpath:liquibase-changelog.xml");
    return liquibase;
}
... …
Run Code Online (Sandbox Code Playgroud)

java spring liquibase

6
推荐指数
2
解决办法
2824
查看次数

注入不适用于新操作员

任何人都可以解释为什么@Inject对象在使用new运算符初始化类时为null ?

public class A{
    @Inject
    B b;
    ...
    ...
}
Run Code Online (Sandbox Code Playgroud)

当创建上述类的A a = new A();,我得到b的null.有谁能解释为什么?我知道它在Inject A类时有效.但是我想知道为什么它不适用于new运算符.春天做什么?

java spring

5
推荐指数
1
解决办法
4149
查看次数

在 JavaFX 中添加 Spring 依赖注入(JPA Repo、Service)

我有一个 java FX 基本应用程序,它有一个简单的场景(一个表单)。我有一个 Mysql Db,我正在使用 Spring JPA(spring data jpa,即存储库/实体)与之交互。

现在,因为我们知道 javaFx 有一些生命周期钩子,即:init() start() 和 stop()。

假设我想使用 JPA save()方法在数据库中插入数据。通常,如果是我的控制器,则是正常的数据库注入,例如:

@Autowired
EmployeeRepo employeeRepo;
Run Code Online (Sandbox Code Playgroud)

本来可以工作的。但是,我无法在生命周期方法中访问此(或任何自动装配注入)。

public void start(Stage primaryStage) throws Exception {

// Some Code

employeeRepo.findAll() <- This is returning null

Run Code Online (Sandbox Code Playgroud)

但是,当我添加一个测试方法并使用相同的方法时,它工作正常:

@PostConstruct
public void test() {
// Repo object is not giving null
}
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以手动将依赖项注入我的按钮侦听器中或将其传递给启动方法。

请让我知道是否有解决方案,因为我是 JavaFX 的新手

java javafx spring-data-jpa javafx-8 spring-boot

1
推荐指数
1
解决办法
2210
查看次数