代码中标题的问题:
@Transactional (readonly = true)
public interface FooService {
void doSmth ();
}
public class FooServiceImpl implements FooService {
...
}
Run Code Online (Sandbox Code Playgroud)
VS
public interface FooService {
void doSmth ();
}
@Transactional (readonly = true)
public class FooServiceImpl implements FooService {
...
}
Run Code Online (Sandbox Code Playgroud) 我有以下简单的应用程序
Users 实体
@Entity
public class Users implements Serializable {
@Id
@GeneratedValue
private long id;
private String name;
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
private Set<UserRoleUser> userRoleUser;
// GETTERS AND SETTERS
}
Run Code Online (Sandbox Code Playgroud)
UserRole 实体
@Entity
public class UserRole implements Serializable {
@Id
@GeneratedValue
private long id;
private String roleName;
@OneToMany(mappedBy = "userrole", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<UserRoleUser> userRoleUser;
// GETTERS AND SETTERS
}
Run Code Online (Sandbox Code Playgroud)
UserRoleUser 多对多解析器类
@Entity
public class UserRoleUser implements Serializable {
@Id
@GeneratedValue …Run Code Online (Sandbox Code Playgroud) 我们一直在使用@Autowired基于Java的Spring配置并取得了一些成功,但现在我们失去了控制权.每个人都开始在任何地方添加自动连接的依赖项,创建周期和奇怪的错误.
所以我们正在考虑使用构造函数注入和Spring配置的自动装配.
旧:
class Bean {
@Autowired Foo foo;
}
@Configuration
@Import( FooCfg.class )
class BeanCfg {
@Bean public Bean bean() { return new Bean(); }
}
Run Code Online (Sandbox Code Playgroud)
新:
class Bean {
public Bean(Foo foo) {...}
}
@Configuration
class BeanCfg {
@Autowired FooCfg fooCfg;
@Bean public Bean bean() { return new Bean(fooCfg.foo()); }
}
Run Code Online (Sandbox Code Playgroud)
这非常有效(并且它驱使人们分割bean而不是创建具有10个以上构造函数参数的怪物).
但是当Bean有一个方法注释时它会失败,@Transactional因为CGLIB然后尝试创建一个失败的代理,因为它找不到无参构造函数.
这是什么解决方案?
spring ×3
java ×2
annotations ×1
autowired ×1
cglib ×1
coding-style ×1
querydsl ×1
spring-boot ×1