Spring Scan configuration file

Source: Internet
Author: User

The spring configuration file corresponds to the parent container, the SPRINGMVC configuration file is a sub-container, the former general configuration of data sources, transactions, annotations and so on, of course, you can further refine some of the configuration into other XML, the latter general configuration of the control layer related, such as static resources, view resolver. When the system starts, the parent container is initialized, and the child container is initialized. This involves a problem where configuring a component scan configures a full-component scan, which causes the service component to be scanned two times, causing the transaction to fail to process. Therefore, it is best to do only the controller scan in the SPRINGMVC configuration file, and scan the other components in the spring configuration file.
In the spring configuration file, configure:

<context:component-scan base-package="com"/>
    • 1

Configure in the SPRINGMVC configuration file:

<context:component-scan base-package="com.**.controller"/>
    • 1

So you can do your duties.
In use, these two profiles work differently. If you want to inject some of the variables in the system configuration file using @value, be aware that if you want to use injected variables in the controller, you need to configure them in the SPRINGMVC configuration file:

<context:property-placeholder location="classpath:{your variable file}.properties"/>
    • 1

If it is configured only in the spring configuration file, it will not be injected successfully in the controller.
The test demo is as follows:

 @RunWith (Springjunit4classrunner.class)  @ContextConfiguration (locations = { "classpath: Applicationcontext.xml ", " Classpath:servlet-dispatcher.xml "}) public class injectest { @Value ( "${ly.key}") private String key;  @Test public void test () {System.out.println (     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

There are two ways to inject based on @value, placeholders and spel expressions

    //占位符方式    @Value("${jdbc.url}")    private String url;
    • 1
    • 2
    • 3
    //SpEL表达方式,其中代表xml配置文件中的id值configProperties    @Value("#{configProperties[‘jdbc.username‘]}")    private String userName;
    • 1
    • 2
    • 3

Both of these approaches need to be configured differently in XML

    <!--Configure single properties based on placeholder mode---<!--<context:property-placeholder location= "Conf/jdbc.properties"/>--><!--Configure multiple properties based on placeholder mode--<BeanId="Propertyconfigurer"class="Org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer" ><PropertyName= "locations" > <list> <value>classpath:config/resource/dev/application.properties</value> < Value>classpath:config/resource/dev/lyframework.properties</ value> <value>classpath: Config/resource/dev/common.properties</value> </list> </property > </BEAN>       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    <!--Configure multiple properties ID values based on spel expressions for configproperties to provide in Java code to use--<bean id=  "configproperties" class= " Org.springframework.beans.factory.config.PropertiesFactoryBean "> < property name= "locations" > Span class= "Hljs-tag" ><list> < Value>classpath:/conf/jdbc.properties</value> </list> </ property> </BEAN>       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    <!--基于SpEL表达式 配置单个properties -->    <!--<util:properties id="configProperties" location="classpath:conf/jdbc.properties"/>-->

Spring Scan configuration file

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.