Spring常用知识点总结.doc
文本预览下载声明
Spring的作用
帮助我们产生对象
帮助我们管理对象
帮助我们管理对象之间的关系
Spring配置文件的读取方式
(1)ApplicationContext
1.ApplicationContext ac = new
FileSystemXmlApplicationContext(src/applicationContext.xml);
2.ApplicationContext ac = new
ClassPathXmlApplicationContext(applicationContext.xml);
(2)BeanFactory
1.Resource rs = new FileSystemResource(src/applicationContext.xml);
BeanFactory factory = new XmlBeanFactory(rs);
2.Resource rs = new ClassPathResource(spring.xml);
BeanFactory factory=new XmlBeanFactory(rs);
Spring 读取多个配置文件
1.String [] xmls = new String[]{“spring1.xml”,”spring2.xml”,”spring3.xml”};
ApplicationContext context = new ClassPathXmlApplicationContext(xmls);
2.使用通配符spring1.xml spring2.xml spring3.xml
ApplicationContext context = new ClassPathXmlApplicationContext(“spring*.xml”);
3.beans
import resource=services.xml/
import resource=resources/messageSource.xml/
import resource=/resources/themeSource.xml/
bean id=bean1 class=.../
bean id=bean2 class=.../
/beans
Bean实例的创建方式
1.使用类的无参数构造方法构造
该方式要求在开发类的时候类一定要有无参数的构造方法。
2.使用静态工厂实例化
bean id=“us” class=“sf.UserServiceFactory” factory-method=“createService”/
Public class UserServiceFactoty{
public static UserService createService(){
return new UserService();
}
}
3.使用实例工厂方法实例化
bean id=“uf” class=“sf.UserServiceFactory”/
bean id=“us” factory-bean=“uf” factory-method=“createService”/
Public class UserServiceFactory{
public UserService createService(){return new UserService();
}
}
Spring 依赖注入的三种方式
1.接口注入
2.Setter注入
注入属性xx时,必须要有对应的setXx()方法,在property标签中,如果是基本类型或者是String类型 用value标签,如果是引用其他的类类型用ref标签,如果是其他特殊类型如List,Map,Property等用以下方式:
1:List
public class HelloWorld{
//定义一个List变量msg
List msg=null;
public void setMsg(List msg){
this.msg = msg;
}
}
xml文件:
bean id=Hello class=HelloWorld
property name=msg
list
valueHello World!/value
valueHello World2!/value
显示全部