依赖注入到Spring非托管bean中 [英] Dependency Injection into Spring non-managed beans

查看:209
本文介绍了依赖注入到Spring非托管bean中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非托管的JPA域类.通过new运算符实例化.

I have a JPA domain class that is non managed. It is instantiated via the new operator.

UserAccount account = new UserAccount();
userRepository.save(account)

在我的UserAccount类中,我有一个beforeSave()方法,该方法依赖于我的SecurityService来对密码进行哈希编码.

In my UserAccount class, I have a beforeSave() method which is dependent on my SecurityService to hash encode a password.

我的问题是如何获得Spring DI将安全服务注入我的实体?".似乎我需要AspectJ和LoadTimeWeaving.我已经尝试过使用数组进行配置,但是似乎无法使它们中的任何一个起作用.尝试在注入的对象上调用方法时,总是得到NullPointerException.

My questions is "How do I get spring DI to inject the security service into my entity?". Seems that AspectJ and LoadTimeWeaving is what I need. I've tried an array for configurations, but I can't seem to get any of them to work. I always get a NullPointerException when trying to call a method on the injected object.

UserAccount.java (这是JPA实体)

UserAccount.java (This is the JPA Entity)

@Entity
@Repository
@Configurable(autowire = Autowire.BY_TYPE)
public class UserAccount implements Serializable {

    @Transient
    @Autowired
    SecurityService securityService;

    private String passwordHash;

    @Transient
    private String password;

    public UserAccount() {
        super();
    }

    @PrePersist
    public void beforeSave() {
        if (password != null) {
            // NullPointerException Here!!!
            passwordHash = securityService.hashPassword(password);  
        }
    }
}

试图指出要使用AspectJ:

Trying to indicate to spring to use AspectJ:

NitroApp.java (主类)

NitroApp.java (The main class)

@SpringBootApplication
@EnableTransactionManagement
@EnableSpringConfigured
@PropertySources(value = {@PropertySource("classpath:application.properties")})
public class NitroApp extends SpringBootServletInitializer {


    public static void main (String[] args) {
        SpringApplication.run(NitroApp.class);
    }

}

build.gradle (配置)

build.gradle (Configuration)

buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }

    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:1.2.2.RELEASE"
        classpath "org.springframework:springloaded:1.2.2.RELEASE"
        classpath "org.springframework:spring-aspects:4.1.6.RELEASE"
    }
}

apply plugin: 'java'
apply plugin: 'aspectj'
apply plugin: 'application'
apply plugin: 'idea'
apply plugin: 'spring-boot'

repositories {
    jcenter()
    mavenLocal()
    mavenCentral()
}

mainClassName = 'com.noxgroup.nitro.NitroApp'
applicationName = "Nitro"

idea {
    module {
        inheritOutputDirs = false
        outputDir = file("$buildDir/classes/main/")
    }
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.springframework.boot:spring-boot-starter-actuator")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("net.sourceforge.nekohtml:nekohtml:1.9.15")
    compile("commons-codec:commons-codec:1.9")
    compile("org.postgresql:postgresql:9.4-1201-jdbc41")
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}

推荐答案

您可以在用于实例化UserAccount的类中注入Spring applicationContext.

You can inject Spring applicationContext in the class used to instanciate UserAccount.

@Autowired
private ApplicationContext applicationContext;

然后,以这种方式创建您的UserAccount bean:

Then, create your UserAccount bean this way :

UserAccount userAccount = applicationContext.getBean(UserAccount.class);

这样,您可以在UserAccount类中注入所需的依赖项.

This way, you can inject your required dependencies in the UserAccount class.

这篇关于依赖注入到Spring非托管bean中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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