首页 > 代码库 > 2、一些配置

2、一些配置

1、导入资源文件

<context:property-placeholder location="classpath:db.properties"/>

用${}来引用

2、SpEL

使用#{}

3、 bean生命周期方法

 1 public class Bean1 {
 2 
 3     public Bean1() {
 4         System.out.println("Bean1()");
 5     }
 6 
 7     public void init() {
 8         System.out.println("init...");
 9     }
10 
11     @Override
12     public String toString() {
13         return this.getClass().getName();
14     }
15 
16     public void destroy() {
17         System.out.println("destroy...");
18     }
19 
20 }

配置:

1 <bean id="bean1" class="demo.Bean1" init-method="init" destroy-method="destroy" />

4、配置bean的时parent、p命名空间和depends-on。。。的使用

5、bean的后置处理器

 1 public class MyBeanPostProcessor implements BeanPostProcessor {
 2 
 3     @Override
 4     public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
 5         System.out.println("postProcessBeforeInitialization: " + beanName);
 6         return bean;
 7     }
 8 
 9     @Override
10     public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
11         System.out.println("postProcessAfterInitialization: " + beanName);
12         return bean;
13     }
14 
15 }

  配置:

1 <bean class="demo.MyBeanPostProcessor" />

6、在classpath中扫描组件

1 <context:component-scan base-package="" resource-pattern="">
2     <context:include-filter type="" expression=""/>
3     <context:exclude-filter type="" expression=""/>
4 </context:component-scan>

2、一些配置