java
spring
1. spring框架搭建
加入jar包 SPRING_HOME/dist下的: org.springframework.asm-3.1.3.RELEASE.jar org.springframework.beans-3.1.3.RELEASE.jar org.springframework.context-3.1.3.RELEASE.jar org.springframework.core-3.1.3.RELEASE.jar org.springframework.expression-3.1.3.RELEASE.jar 加入common-logging.jar包
加入配置文件 在src下创建一个applicationContext.xml并加入以下配置:
将对象交给Spring管理 在beans中通过bean标签来配置将要被侵称劣spring管理的类
从spring容器中获取对象 1. 加载配置文件 BeanFactory bf = new ClassPathXmlApplicationContext('applicationContext.xml'); 2. 获取对象(传入在xml中配置的bean的对应id) 改诸UserService us = (UserService) bf.getBean('userService');
sping的注入
自定义类型转换器
编写一个转换器类,继承与PropertyEditorSupport,并重写其中的setAsText方法进行转换 转换后调用本类的setValue来设置值
配置到spring中来使用
自动装配 在beans上通过设置属性 default-autowire来实现自动装配 byName: 根据bean的id和set方法的名称匹配来自动装配 byType: 根据bean的类型和set方法的参数类型来匹配装配(如果找到多个bean会报错)