相关疑难解决方法(0)

为什么Spring的@Configurable有时可以工作,有时候不工作?

我正在尝试通过Spring的@Configurable注释w/@Resource在需要注入的字段上使用自动依赖注入.这涉及一些设置,比如将spring-agent.jar传递给我的JVM.有关详细信息,请参见此处.

它主要起作用.当我的Tomcat启动时,我看到AspectJ init消息,我的User对象自动获取FileService引用等.

问题是有时它不会发生.它似乎是完全随机的; 有时我启动并且不会注入依赖项,有时它们是.我以前遇到过@Transactional在我的用户上的问题,因为它造成了冲突,我相信代理.我正在使用JPA,因此我的用户标有@Entity,所以我现在最好的猜测是这会产生冲突.我读过你不能自动代理代理.为了抵消冲突,我在网上找到了一些关于排除CGLIBjavassist的注释,这些注释是Hibernate(我的JPA impl)使用的.

线索:

  • 这是全有或全无.我的所有@Configurable实例都已注入,或者都没有注入.
  • 从数据库重新加载(重新实例化)实体似乎没有帮助; 它要么工作要么不工作.
  • 重新启动Tomcat任何时间也都不会修复它.唯一似乎再次掷骰子的是重新部署.换句话说,如果我重新部署它可能会起作用.

我怎么能弄清楚出了什么问题?是否有人使用@Configurable和JPA?为什么我的dependencyCheck = true在没有实际注入依赖项时抛出错误?

实体

@Entity
@Configurable(dependencyCheck = true)
@NamedQueries( { @NamedQuery(name = "User.findAll", query = "SELECT user FROM User user"),
    @NamedQuery(name = "User.findByEmail", query = "SELECT user FROM User user WHERE user.email = :email") })
public abstract class User extends BaseModel {

private static final long serialVersionUID = 7881431079061750040L;

@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;

@Column(unique = true, nullable = …
Run Code Online (Sandbox Code Playgroud)

java spring annotations dependency-injection jpa

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

对 Spring 非托管 bean 的依赖注入

我有一个非托管的 JPA 域类。它是通过new操作符实例化的。

UserAccount account = new UserAccount();
userRepository.save(account)
Run Code Online (Sandbox Code Playgroud)

在我的UserAccount课堂上,我有一个beforeSave()方法依赖于我SecurityService对密码进行哈希编码。

我的问题是“如何让 spring DI 将安全服务注入我的实体?”。似乎 AspectJ 和 LoadTimeWeaving 是我需要的。我已经尝试了一个用于配置的数组,但我似乎无法让它们中的任何一个工作。NullPointerException尝试在注入的对象上调用方法时,我总是得到一个。

UserAccount.java(这是 JPA 实体)

@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);  
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

试图表明 spring …

java spring aspectj spring-boot spring-aspects

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

How to configure Spring to use aspectj for Transactional?

According this answer /sf/answers/240083021/ To use @Transactional when I execute local method I did following steps:

added to pom.xml:

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.7</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.7</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

replace in context

<tx:annotation-driven transaction-manager="transactionManager"/>
Run Code Online (Sandbox Code Playgroud)

with

<tx:annotation-driven transaction-manager="transactionManager" mode="aspectj"/>
Run Code Online (Sandbox Code Playgroud)

after it I saw

Could not obtain transaction-synchronized Session for current thread

additionally added:

<context:load-time-weaver/>
Run Code Online (Sandbox Code Playgroud)

But when make mvn clean install I see(problem occures when tests try to execute. In my test I up appliction context):

    java.lang.IllegalStateException: Failed …
Run Code Online (Sandbox Code Playgroud)

java proxy spring aspectj maven

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