XMLAdapter 上的编译时和加载时编织和自动装配不起作用
Compile-Time and Load-Time Weaving and Autowired on XMLAdapter not working
我有一个基于 XML 的数据库,我定义了一个用户模型,其中包含对角色(另一个模型)的引用列表。我已将 XML 适配器附加到角色 属性 以自动填充角色。为此,我在这个适配器中@autowired RoleRepository。
但是,无论我做什么,存储库都不会自动装配(总是 null
)。我已经配置了编译时编织、加载时编织,还尝试了一个能够将自身加载到 运行 JVM invesdwin-instrument
.
中的检测 java 代理
@Configurable(autowire = Autowire.BY_TYPE)
public class RoleAdapter extends XmlAdapter<String, List<Role>> {
@Autowired
protected RoleRepository roleRepository;
public RoleAdapter() {
}
@Override
public List<Role> unmarshal(String nameList) throws Exception {
// code using roleRepository
}
@Override
public String marshal(List<Role> roles) throws Exception {
// some code
}
}
@SpringBootApplication
@EnableConfigurationProperties({MyProperties.class})
@EntityScan(basePackages = { ... })
@EnableDiscoveryClient
@EnableLoadTimeWeaving(aspectjWeaving=EnableLoadTimeWeaving
.AspectJWeaving.ENABLED)
@EnableSpringConfigured // tried this in a separate config
public class MyApplication {
static { // this was not here, added in a desperate move
DynamicInstrumentationLoader.waitForInitialized();
DynamicInstrumentationLoader.initLoadTimeWeavingContext();
}
// some code
/**
* Main method, used to run the application.
*
* @param args the command line arguments
*/
public static void main(String[] args) {
// dynamically attach java agent to jvm if not already present
DynamicInstrumentationLoader.waitForInitialized();
// weave all classes before they are loaded as beans
DynamicInstrumentationLoader.initLoadTimeWeavingContext();
if (!InstrumentationLoadTimeWeaver.isInstrumentationAvailable()) {
throw new IllegalStateException("Instrumentation not available!");
} else { // it always gets here
System.out.println("Instrumentation available!");
}
SpringApplication app = new SpringApplication(MyApplication.class);
DefaultProfileUtil.addDefaultProfile(app);
Environment env = app.run(args).getEnvironment();
logApplicationStartup(env);
}
// more code
}
以及用户中的角色字段
@XmlElement(type = String.class)
@XmlJavaTypeAdapter(RoleAdapter.class)
@XmlSchemaType(name = "IDREFS")
protected List<Role> roles;
我想知道我在编织方面错过了什么。也欢迎使用更简单的方法来实现这些自动填充属性。
"Solved" 通过忘记编译时编织和加载时编织,并将 RolesRepository
注入 UsersRepository
的问题,用 RoleAdapter
初始化一个实例注入存储库并将此实例添加到解组器中。
@Repository
public class UserRepository extends TimestampRepository<User> {
public static final String USERS_BY_USERNAME_CACHE = "usersByUsername";
public static final String USERS_BY_EMAIL_CACHE = "usersByEmail";
public UserRepository(
MyProperties myProperties,
ExistTemplate existTemplate,
Jaxb2Marshaller marshaller,
Jaxb2Marshaller unmarshaller,
RoleRepository roleRepository) {
super(
new UserEntityInformation(myProperties.getDatabase().getDbname()),
existTemplate, marshaller, unmarshaller);
marshaller.setAdapters(new RoleAdapter(roleRepository));
unmarshaller.setAdapters(new RoleAdapter(roleRepository));
}
// more code
}
public class RoleAdapter extends XmlAdapter<String, List<Role>> {
protected final RoleRepository roleRepository;
public RoleAdapter(RoleRepository roleRepository) {
this.roleRepository = roleRepository;
}
@Override
public List<Role> unmarshal(String nameList) throws Exception {
// code using roleRepository
}
@Override
public String marshal(List<Role> roles) throws Exception {
// some code
}
}
我有一个基于 XML 的数据库,我定义了一个用户模型,其中包含对角色(另一个模型)的引用列表。我已将 XML 适配器附加到角色 属性 以自动填充角色。为此,我在这个适配器中@autowired RoleRepository。
但是,无论我做什么,存储库都不会自动装配(总是 null
)。我已经配置了编译时编织、加载时编织,还尝试了一个能够将自身加载到 运行 JVM invesdwin-instrument
.
@Configurable(autowire = Autowire.BY_TYPE)
public class RoleAdapter extends XmlAdapter<String, List<Role>> {
@Autowired
protected RoleRepository roleRepository;
public RoleAdapter() {
}
@Override
public List<Role> unmarshal(String nameList) throws Exception {
// code using roleRepository
}
@Override
public String marshal(List<Role> roles) throws Exception {
// some code
}
}
@SpringBootApplication
@EnableConfigurationProperties({MyProperties.class})
@EntityScan(basePackages = { ... })
@EnableDiscoveryClient
@EnableLoadTimeWeaving(aspectjWeaving=EnableLoadTimeWeaving
.AspectJWeaving.ENABLED)
@EnableSpringConfigured // tried this in a separate config
public class MyApplication {
static { // this was not here, added in a desperate move
DynamicInstrumentationLoader.waitForInitialized();
DynamicInstrumentationLoader.initLoadTimeWeavingContext();
}
// some code
/**
* Main method, used to run the application.
*
* @param args the command line arguments
*/
public static void main(String[] args) {
// dynamically attach java agent to jvm if not already present
DynamicInstrumentationLoader.waitForInitialized();
// weave all classes before they are loaded as beans
DynamicInstrumentationLoader.initLoadTimeWeavingContext();
if (!InstrumentationLoadTimeWeaver.isInstrumentationAvailable()) {
throw new IllegalStateException("Instrumentation not available!");
} else { // it always gets here
System.out.println("Instrumentation available!");
}
SpringApplication app = new SpringApplication(MyApplication.class);
DefaultProfileUtil.addDefaultProfile(app);
Environment env = app.run(args).getEnvironment();
logApplicationStartup(env);
}
// more code
}
以及用户中的角色字段
@XmlElement(type = String.class)
@XmlJavaTypeAdapter(RoleAdapter.class)
@XmlSchemaType(name = "IDREFS")
protected List<Role> roles;
我想知道我在编织方面错过了什么。也欢迎使用更简单的方法来实现这些自动填充属性。
"Solved" 通过忘记编译时编织和加载时编织,并将 RolesRepository
注入 UsersRepository
的问题,用 RoleAdapter
初始化一个实例注入存储库并将此实例添加到解组器中。
@Repository
public class UserRepository extends TimestampRepository<User> {
public static final String USERS_BY_USERNAME_CACHE = "usersByUsername";
public static final String USERS_BY_EMAIL_CACHE = "usersByEmail";
public UserRepository(
MyProperties myProperties,
ExistTemplate existTemplate,
Jaxb2Marshaller marshaller,
Jaxb2Marshaller unmarshaller,
RoleRepository roleRepository) {
super(
new UserEntityInformation(myProperties.getDatabase().getDbname()),
existTemplate, marshaller, unmarshaller);
marshaller.setAdapters(new RoleAdapter(roleRepository));
unmarshaller.setAdapters(new RoleAdapter(roleRepository));
}
// more code
}
public class RoleAdapter extends XmlAdapter<String, List<Role>> {
protected final RoleRepository roleRepository;
public RoleAdapter(RoleRepository roleRepository) {
this.roleRepository = roleRepository;
}
@Override
public List<Role> unmarshal(String nameList) throws Exception {
// code using roleRepository
}
@Override
public String marshal(List<Role> roles) throws Exception {
// some code
}
}