Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there anyway to disable annotation in spring4?

I have a question, maybe simple, but I can not find out the solution.

I am using spring boot and added some annotation to the code like this:

@EnableEurekaClient
@SpringBootApplication
@EnableCaching
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

But in some other environment, for example, in production environment, we want to remove EurekaClient, but I do not want to manually remove it manually for each environment, instead, I want to use environment variable or command line parameter to control the behavior. I suppose to do this way:

@EnableEurekaClient(Enabled = {EnableEureka})
@SpringBootApplication
@EnableCaching
public class MyApplication {
        public static void main(String[] args) {
            SpringApplication.run(MyApplication.class, args);
        }
}

Then I can easily start this application without touching the code.

Can anyone tell me if this is possible? If so, how can I do it?

Thanks

like image 574
user3006967 Avatar asked Aug 16 '16 23:08

user3006967


People also ask

How do I turn off annotations in Spring?

If you find that specific auto-configure classes are being applied that you don't want, you can use the exclude attribute of @EnableAutoConfiguration to disable them. If the class is not on the classpath, you can use the excludeName attribute of the annotation and specify the fully qualified name instead.

Which annotation is used for Autopopulating?

@EnableAutoConfiguration: It auto-configures the bean that is present in the classpath and configures it to run the methods.

How do I turn on annotations in Spring?

Annotations are turned off by-default by Spring Framework. In order to turn it on we have to provide a entry in Spring Configuration file. Using tag context:annotation-config from Spring's context namespace makes it turn on. Annotation makes development easier and faster.


1 Answers

You would want to work with Spring Boot Profiles. Split out the @EnableEurekaClient to another @Configuration class and also add an @Profile("eureka-client") to the class. Then when starting up the application you can set a -Dspring.profiles.active=eureka-client for the environments other than production.

Example:

@SpringBootApplication
@EnableCaching
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

@Configuration
@EnableEurekaClient
@Profile("eureka-client")
public class EurekaClientConfiguration {
}
like image 79
Shawn Clark Avatar answered Oct 22 '22 14:10

Shawn Clark