gpt4 book ai didi

java - XMLAdapter 上的编译时和加载时编织和自动连线不起作用

转载 作者:行者123 更新时间:2023-12-02 01:21:15 25 4
gpt4 key购买 nike

我有一个基于 XML 的数据库,并且定义了一个用户模型,其中包含对角色(另一个模型)的引用列表。我已将 XMLAdapter 附加到 Roles 属性以自动填充角色。为此,我已在此适配器中 @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;

我想知道我在编织方面错过了什么。也欢迎使用更简单的方法来实现这些自动填充属性。

最佳答案

通过忘记编译时编织和加载时编织,并将 RolesRepository 注入(inject) UsersRepository 中,初始化 RoleAdapter< 的实例,“解决”了该问题 使用注入(inject)的存储库并将此实例添加到解码器中。

@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
}
}

关于java - XMLAdapter 上的编译时和加载时编织和自动连线不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57624394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com