What Spring configuration

1, XML configuration files.

Dependency and services required Bean specified in the configuration file in XML format. These profiles typically contain many bean definitions and specific configuration options for your application. They usually begin with bean tag. E.g:

<bean id="studentBean" class="org.edureka.firstSpring.StudentBean">
    <property name="name" value="Edureka"></property>
</bean>

  

2, annotations configuration.

You can use in the relevant class, method, or field declarations notes, Bean will configure the component class itself, rather than use XML to describe Bean assembly. By default, Spring annotation unopened container assembly. Therefore, you need to enable it in the Spring configuration file before using it. E.g:

<beans>
<context:annotation-config/>
<!-- bean definitions go here -->
</beans>

  

@Service
@Component
@Repository
@Controlle

  

3, Java Config configuration.

Spring for Java configuration is achieved through the use of @Bean and @Configuration.

  • @Bean Notes played with  <bean /> elements of the same role. The method used, the method returns the current value indicates a bean
  • @Configuration Class allows other by simply calling a class in the same  @Bean way to define dependencies between Bean. Equivalent to the spring XML configuration file

E.g:

@Configuration
public class StudentConfig {
    
    @Bean
    public StudentBean myStudent() {
        return new StudentBean();
    }
    
}

Java Config configuration explained: https: //blog.csdn.net/peng86788/article/details/81188049

This so-called ways nothing good or bad, mainly to see the use, in general, is this:

  Related to the global configuration, such as database configuration, MVC configuration, etc., configured with Java Config mode

  Related to the service configuration, use annotation mode

Guess you like

Origin www.cnblogs.com/yangyanbo/p/12368088.html