Rectangle 27 186

Annotations have their use, but they are not the one silver bullet to kill XML configuration. I recommend mixing the two!

For instance, if using Spring, it is entirely intuitive to use XML for the dependency injection portion of your application. This gets the code's dependencies away from the code which will be using it, by contrast, using some sort of annotation in the code that needs the dependencies makes the code aware of this automatic configuration.

However, instead of using XML for transactional management, marking a method as transactional with an annotation makes perfect sense, since this is information a programmer would probably wish to know. But that an interface is going to be injected as a SubtypeY instead of a SubtypeX should not be included in the class, because if now you wish to inject SubtypeX, you have to change your code, whereas you had an interface contract before anyways, so with XML, you would just need to change the XML mappings and it is fairly quick and painless to do so.

I haven't used JPA annotations, so I don't know how good they are, but I would argue that leaving the mapping of beans to the database in XML is also good, as the object shouldn't care where its information came from, it should just care what it can do with its information. But if you like JPA (I don't have any expirience with it), by all means, go for it.

In general: If an annotation provides functionality and acts as a comment in and of itself, and doesn't tie the code down to some specific process in order to function normally without this annotation, then go for annotations. For example, a transactional method marked as being transactional does not kill its operating logic, and serves as a good code-level comment as well. Otherwise, this information is probably best expressed as XML, because although it will eventually affect how the code operates, it won't change the main functionality of the code, and hence doesn't belong in the source files.

I would summarise this advice as: use annotations for AOP (transactions can be treated as an aspect, for instance), but don't use it for dependency injection.

java - Xml configuration versus Annotation based configuration - Stack...

java xml spring annotations
Rectangle 27 186

Annotations have their use, but they are not the one silver bullet to kill XML configuration. I recommend mixing the two!

For instance, if using Spring, it is entirely intuitive to use XML for the dependency injection portion of your application. This gets the code's dependencies away from the code which will be using it, by contrast, using some sort of annotation in the code that needs the dependencies makes the code aware of this automatic configuration.

However, instead of using XML for transactional management, marking a method as transactional with an annotation makes perfect sense, since this is information a programmer would probably wish to know. But that an interface is going to be injected as a SubtypeY instead of a SubtypeX should not be included in the class, because if now you wish to inject SubtypeX, you have to change your code, whereas you had an interface contract before anyways, so with XML, you would just need to change the XML mappings and it is fairly quick and painless to do so.

I haven't used JPA annotations, so I don't know how good they are, but I would argue that leaving the mapping of beans to the database in XML is also good, as the object shouldn't care where its information came from, it should just care what it can do with its information. But if you like JPA (I don't have any expirience with it), by all means, go for it.

In general: If an annotation provides functionality and acts as a comment in and of itself, and doesn't tie the code down to some specific process in order to function normally without this annotation, then go for annotations. For example, a transactional method marked as being transactional does not kill its operating logic, and serves as a good code-level comment as well. Otherwise, this information is probably best expressed as XML, because although it will eventually affect how the code operates, it won't change the main functionality of the code, and hence doesn't belong in the source files.

I would summarise this advice as: use annotations for AOP (transactions can be treated as an aspect, for instance), but don't use it for dependency injection.

java - Xml configuration versus Annotation based configuration - Stack...

java xml spring annotations
Rectangle 27 3

I also think a mix is the best thing, but it also depends on the type of configuration parameters. I'm working on a Seam project which also uses Spring and I usually deploy it to different development and test servers. So I have split:

  • Injecting beans as members of other beans (or reusing a Spring XML defined value in many beans): Annotations

The key difference is that you don't have to recompile the code for all changing server-specific configurations, just edit the xml file. There's also the advantage that some configuration changes can be done by team members who don't understand all the code involved.

java - Xml configuration versus Annotation based configuration - Stack...

java xml spring annotations
Rectangle 27 3

I also think a mix is the best thing, but it also depends on the type of configuration parameters. I'm working on a Seam project which also uses Spring and I usually deploy it to different development and test servers. So I have split:

  • Injecting beans as members of other beans (or reusing a Spring XML defined value in many beans): Annotations

The key difference is that you don't have to recompile the code for all changing server-specific configurations, just edit the xml file. There's also the advantage that some configuration changes can be done by team members who don't understand all the code involved.

java - Xml configuration versus Annotation based configuration - Stack...

java xml spring annotations
Rectangle 27 2

In the scope of DI container, I consider annotation based DI is abusing the use of Java annotation. By saying that, I don't recommend to use it widely in your project. If your project does really needs the power of DI container, I would recommend to use Spring IoC with Xml based configuration option.

If it is just for a sake of Unit-test, developers should apply Dependency Inject pattern in their coding and take advantages from mocking tools such as EasyMock or JMock to circumvent dependencies.

You should try to avoid using DI container in its wrong context.

java - Xml configuration versus Annotation based configuration - Stack...

java xml spring annotations
Rectangle 27 2

In the scope of DI container, I consider annotation based DI is abusing the use of Java annotation. By saying that, I don't recommend to use it widely in your project. If your project does really needs the power of DI container, I would recommend to use Spring IoC with Xml based configuration option.

If it is just for a sake of Unit-test, developers should apply Dependency Inject pattern in their coding and take advantages from mocking tools such as EasyMock or JMock to circumvent dependencies.

You should try to avoid using DI container in its wrong context.

java - Xml configuration versus Annotation based configuration - Stack...

java xml spring annotations
Rectangle 27 14

I always think about annotations as some kind of indicator of what a class is capable of, or how it interacts with others.

Spring XML configuration on the other hand to me is just that, configuration

For instance, information about the ip and port of a proxy, is definetly going into an XML file, it is the runtime configuration.

Using @Autowire,@Element to indicate the framework what to do with the class is good use of annotations.

Putting the URL into the @Webservice annotation is bad style.

But this is just my opinion. The line between interaction and configuration is not always clear.

Annotation and Annotation based configuration(Java config) are two different things and the OP asks about the later while you talk about the former.

java - Xml configuration versus Annotation based configuration - Stack...

java xml spring annotations
Rectangle 27 14

I always think about annotations as some kind of indicator of what a class is capable of, or how it interacts with others.

Spring XML configuration on the other hand to me is just that, configuration

For instance, information about the ip and port of a proxy, is definetly going into an XML file, it is the runtime configuration.

Using @Autowire,@Element to indicate the framework what to do with the class is good use of annotations.

Putting the URL into the @Webservice annotation is bad style.

But this is just my opinion. The line between interaction and configuration is not always clear.

Annotation and Annotation based configuration(Java config) are two different things and the OP asks about the later while you talk about the former.

java - Xml configuration versus Annotation based configuration - Stack...

java xml spring annotations
Rectangle 27 40

Using Spring Boot (1.2.4) and Jackson (2.4.6) the following annotation based configuration worked for me.

@Configuration
public class JacksonConfiguration {

    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);

        return mapper;
    }
}

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

java - Configuring ObjectMapper in Spring - Stack Overflow

java json spring jackson object-object-mapping
Rectangle 27 16

@Component 
public class MyBean { 
    @Autowired
    public MyBean(@Value("${prop1}") String arg1, @Value("${prop2}") String arg2) { 
        // ... 
    } 
}

But <context:property-override .../> is a very specific thing, it's not suitable here.

Will the property file have "myBean.prop1=foo" or just "prop1=foo"? That is, are the properties scoped by bean name?

prop1=foo
myBean.prop1=foo
@Value("${myBean.prop1}")
<context:property-placeholder .../>

java - Spring: constructor injection of primitive values (properties) ...

java spring dependency-injection annotations
Rectangle 27 16

@Component 
public class MyBean { 
    @Autowired
    public MyBean(@Value("${prop1}") String arg1, @Value("${prop2}") String arg2) { 
        // ... 
    } 
}

But <context:property-override .../> is a very specific thing, it's not suitable here.

Will the property file have "myBean.prop1=foo" or just "prop1=foo"? That is, are the properties scoped by bean name?

prop1=foo
myBean.prop1=foo
@Value("${myBean.prop1}")
<context:property-placeholder .../>

java - Spring: constructor injection of primitive values (properties) ...

java spring dependency-injection annotations
Rectangle 27 1

There are some pros and cons of annotation configuration from my experience:

  • When it comes to JPA configuration since it is done once and usually are not changed quite often I prefer to stick to annotation configuration. There maybe a concern regarding possibility to see a bigger picture of configuration - in this case I use MSQLWorkbench diagrams.
  • Xml configuration is very good to get a bigger picture of application but it maybe cumbersome to find some errors until runtime. In this case Spring @Configuration annotation sounds as a better choice since it let you see a bigger picture as well and also allows to validate configuration on compile time.
  • As for Spring configuration I prefer to combine both approaches: use @Configuration annotation with Services and Query interfaces and xml configuration for dataSource and spring configuration stuff like context:component-scan base-package="..."
  • But xml configuration bits java annotations when it comes to flow configuration(Spring Web Flow or Lexaden Web Flow) since it is extremely important to see a bigger picture of the whole business process. And it sounds cumbersome to have it implemented with annotations approach.

I prefer combining both approaches - java annotations and essential xml minimum that minimize configuration hell.

java - Xml configuration versus Annotation based configuration - Stack...

java xml spring annotations
Rectangle 27 1

There are some pros and cons of annotation configuration from my experience:

  • When it comes to JPA configuration since it is done once and usually are not changed quite often I prefer to stick to annotation configuration. There maybe a concern regarding possibility to see a bigger picture of configuration - in this case I use MSQLWorkbench diagrams.
  • Xml configuration is very good to get a bigger picture of application but it maybe cumbersome to find some errors until runtime. In this case Spring @Configuration annotation sounds as a better choice since it let you see a bigger picture as well and also allows to validate configuration on compile time.
  • As for Spring configuration I prefer to combine both approaches: use @Configuration annotation with Services and Query interfaces and xml configuration for dataSource and spring configuration stuff like context:component-scan base-package="..."
  • But xml configuration bits java annotations when it comes to flow configuration(Spring Web Flow or Lexaden Web Flow) since it is extremely important to see a bigger picture of the whole business process. And it sounds cumbersome to have it implemented with annotations approach.

I prefer combining both approaches - java annotations and essential xml minimum that minimize configuration hell.

java - Xml configuration versus Annotation based configuration - Stack...

java xml spring annotations
Rectangle 27 1

For Spring Framework I like the idea of being able to use the @Component annotation and setting the "component-scan" option so that Spring can find my java beans so that I do not have to define all of my beans in XML, nor in JavaConfig. For example, for stateless singleton java beans that simply need to be wired up to other classes (via an interface ideally) this approach works very well. In general, for Spring beans I have for the most part moved away from Spring XML DSL for defining beans, and now favor the use of JavaConfig and Spring Annotations because you get some compile time checking of your configuration and some refactoring support that you don't get with Spring XML configuration. I do mix the two in certain rare cases where I've found that JavaConfig/Annotations can't do what is available using XML configuration.

For Hibernate ORM (haven't used JPA yet) I still prefer the XML mapping files because annotations in domain model classes to some degree violates The Clean Architecture which is a layering architectural style I have adopted over the past few years. The violation occurs because it requires the Core Layer to depend on persistence related things such as Hibernate or JPA libraries and it makes the domain model POJOs a bit less persistence ignorant. In fact the Core Layer is not supposed to depend on any other infrastructure at all.

However, if The Clean Architecture is not your "cup of tea" then I can see there are definitely advantages (such as convenience and maintainability) of using Hibernate/JPA annotations in domain model classes over separate XML mapping files.

java - Xml configuration versus Annotation based configuration - Stack...

java xml spring annotations
Rectangle 27 1

For Spring Framework I like the idea of being able to use the @Component annotation and setting the "component-scan" option so that Spring can find my java beans so that I do not have to define all of my beans in XML, nor in JavaConfig. For example, for stateless singleton java beans that simply need to be wired up to other classes (via an interface ideally) this approach works very well. In general, for Spring beans I have for the most part moved away from Spring XML DSL for defining beans, and now favor the use of JavaConfig and Spring Annotations because you get some compile time checking of your configuration and some refactoring support that you don't get with Spring XML configuration. I do mix the two in certain rare cases where I've found that JavaConfig/Annotations can't do what is available using XML configuration.

For Hibernate ORM (haven't used JPA yet) I still prefer the XML mapping files because annotations in domain model classes to some degree violates The Clean Architecture which is a layering architectural style I have adopted over the past few years. The violation occurs because it requires the Core Layer to depend on persistence related things such as Hibernate or JPA libraries and it makes the domain model POJOs a bit less persistence ignorant. In fact the Core Layer is not supposed to depend on any other infrastructure at all.

However, if The Clean Architecture is not your "cup of tea" then I can see there are definitely advantages (such as convenience and maintainability) of using Hibernate/JPA annotations in domain model classes over separate XML mapping files.

java - Xml configuration versus Annotation based configuration - Stack...

java xml spring annotations
Rectangle 27 3

There are other aspect to compare like refactoring and other code changes. when using XML it takes serous effort to make refactoring because you have to take care of all the XML content. But it is easy when using Annotations.

java - Xml configuration versus Annotation based configuration - Stack...

java xml spring annotations
Rectangle 27 3

There are other aspect to compare like refactoring and other code changes. when using XML it takes serous effort to make refactoring because you have to take care of all the XML content. But it is easy when using Annotations.

java - Xml configuration versus Annotation based configuration - Stack...

java xml spring annotations
Rectangle 27 6

  • The XML configuration is at one place, instead of being scattered all over the source code in case of annotations. Some people may argue that IDEs like STS allow you to look at all annotations based configuration in one place, but I never like having dependencies on IDEs.
  • Its takes a little more efforts to write XML config, but it saves a lot of time later when you search for dependencies and try to understand the project.
  • XML keeps configuration well organized and simple. Hence is easier to understand, it helps new relatively inexperienced team members get up to speed quickly.
  • Allows you to change the config without a need to recompile and redeploy code. So it is better, when it comes to production support.

So in short XML configuration takes a little more efforts, but it saves you a lot of time & headache later in big projects.

java - Spring annotation-based DI vs xml configuration? - Stack Overfl...

java spring configuration dependency-injection annotations
Rectangle 27 10

from my experience, I would prefer(or rather am forced by limitations) to use a combination of XML and annotation based DI . If I need to inject a Map of elements inside a bean , I would have to define a util:map and autowire it . Also, I need to use XML DI to inject datasource into the sessionFactory if I have multiple datasources and so on . So a combination of both would be requited .

I prefer the usage of component-scan to autodetect the services and Dao . This cuts down a lot of Configuration (We cut down the configuration files by around 50% switching to component-scan). Annotation based DI supports both byName(@Resource) and byType(@Autowired).

In short my advice to be to go for a fixture of both . I feel that more annotation support will definitely be on cards in future Spring releases.

java - Spring annotation-based DI vs xml configuration? - Stack Overfl...

java spring configuration dependency-injection annotations