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"/>
Configure in the SPRINGMVC configuration file:
<context:component-scan base-package="com.**.controller"/>
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"/>
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;
//SpEL表达方式,其中代表xml配置文件中的id值configProperties @Value("#{configProperties[‘jdbc.username‘]}") private String userName;
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>
<!--基于SpEL表达式 配置单个properties --> <!--<util:properties id="configProperties" location="classpath:conf/jdbc.properties"/>-->
Spring Scan configuration file