Springboot configuring @PropertySource, @ImportResource, @Bean

Source: Internet
Author: User

First, @PropertySource

@PropertySource: Load the specified configuration file

@PropertySource (value = {"Classpath:person.properties"}) @Component
= "Person")
Public class Person {
}

Second, @ImportResource: Import the Spring configuration file, let the contents of the configuration file take effect

Spring boot does not have a spring configuration file, and the configuration files we write ourselves are not automatically recognized;
Want spring's configuration file to take effect, load it in, @ImportResource label on a configuration class

@ImportResource (locations = {"Classpath:applicationContext.xml"})
@SpringBootApplication
Public class Springbootconfigapplication {

Public Static void Main (string[] args) {
Springapplication.run (springbootconfigapplication. class , args);
}
}

Third, Springboot recommend the way to add components to the container, the recommended way to use full annotation

1. Configuration class @configuration------>spring configuration file
2. Add components to the container using @bean

/** * @Configuration: Indicates that the current class is a configuration class; Replace the previous spring configuration file * * Add components with <bean><bean/> tags in the configuration file * */
@Configuration
Public class Myappconfig {
  // Add the return value of the method to the container; The default ID of this component in the container is the method name
  @Bean
   Public HelloService HelloService () {
System.out.println ("configuration class @bean adding components to the container ...");
    return New HelloService ();
}
}

Springboot configuration @PropertySource, @ImportResource, @Bean

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.