2

I have old Spring project that is using xml configuration. And I want rewrite this configuration using java annotations (for some reasons). In this xml configuration I have this creepy code:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns....>
    <!-- ... -->
    <import resource="classpath:/spring/${service.loader:service}-loader.xml" />
    <!-- ... -->
</beans>

How I may rewrite this import statement using java annotations?

@Configuration
@Import({
     // TODO What do I need write here?
})
public class SpringConfig {
     // ...
}

Update:

Note: I want rewrite all xml files, and I need import java class (from property or default). I think I need condition statement for this.

3 Answers 3

3
@Configuration
public class Loader {
  /*configurations in classpath:/spring/${service.loader:service}
  -loader.xml file */

}

@Configuration
@Import({Loader.class})
public class SpringConfig {
     // ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

You may want to import from xml basedc onfiguration you may use the below tag to import resource in annotation based spring configuration.

@ImportResource("/spring/${service.loader:service}-loader.xml")

But if you are converting every context xml into corresponding annotation based @Conguration then you should do it with below tag

@Import(OtherConfiguration.class)

1 Comment

thanks, but mean how I may import another java configuration using condition? Or SpEl?
0

If you use spring-boot then you can do something like this

@SpringBootApplication
@ImportResource("classpath:myconfig.xml")
public class MyClasss{

   public static void main(String[] args) {
      SpringApplication.run(Application.class, args); //Up and running
   }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.