spring 常用配置

1.引入properties文件

<!-- spring2.0需要配置PropertyPlaceholderConfigurer,为location属性注入值 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:jdbc.properties"></property>
</bean
    
<!-- spring2.5之后 -->
<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- 在配置文件中使用${propName}获取外部属性文件的值 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="user" value="${jdbc.user}"></property>
    <property name="password" value="${jdbc.password}"></property>
    <property name="jdbcUrl" value="${jdbc.url}"></property>
    <property name="driverClass" value="${jdbc.driverClass}"></property>
    <property name="initialPoolSize" value="${jdbc.initpool}"></property>
    <property name="maxPoolSize" value="${jdbc.maxpool}"></property>
</bean>

2.自动扫描包

<!-- 自动扫描包,只扫描Controller -->
<context:component-scan base-package="com.pdsu"/>

<!-- 更详细的配置 -->
<context:component-scan base-package="com.pdsu" use-default-filters="false">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
  • use-default-filters

使用默认的 Filter 进行包扫描,而默认的 Filter 对标有 @Service,@Controller和@Repository 的注解的类进行扫描,默认值为true,当此值为false时,需要配合<context:include-filter />注解使用。

原文地址:https://www.cnblogs.com/dch0/p/11440635.html