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 >
    <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 />注解使用。

相关文章:

  • 2021-08-27
  • 2021-06-03
  • 2021-06-04
  • 2021-10-06
  • 2022-12-23
  • 2021-06-25
  • 2021-06-27
猜你喜欢
  • 2021-07-10
  • 2021-07-25
  • 2021-06-04
  • 2022-02-18
  • 2022-02-07
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案