@MrXiao
2017-06-13T13:05:56.000000Z
字数 5675
阅读 1060
未分类
A.基于分层设计
UserAction 使用UserService,new UserService()
UserService 使用UserDAO,new UserDAO()
UserDAO
B.工厂模式,使用面向接口编程设计,解决层与层之间的紧耦合
制作接口,制作实现类,制作对象工厂
A a = AFactory.createA();B b = BFactory.createB();a.setB(b);
C.工厂模式+配置
制作XML文件,将实现类配置到XML文件中
读取XML文件中的配置信息,得到实现类的类名
使用反射思想,获取实现类对象 class.newInstance();
A a = Factory.create(“a”);B b = Factory.create(“b”);a.setB(b);
<!--配置xml-->a=AImplb=BImpl
D.自动的工厂+配置
//返回装配好的aA a = Factory.create(“a”);
<!--xml配置--><bean id=“a” class=“AImpl”><property name=“b” ref=“b”/></bean><bean id=“b” class=“BImpl”/>
E.工厂由Spring提供,实现类使用XML格式配置
工厂由Spring提供,实现类使用XML格式配置
//返回装配好的aA a = ApplicationContext.getBean(“a”);
<!--xml配置--><bean id=“a” class=“AImpl”><property name=“b” ref=“b”/></bean><bean id=“b” class=“BImpl”/>
DI(Dependency Injection)依赖注入,指应用程序依赖Spring为其提供运行时所需的资源.
更能描述容器其特点的名字——“依赖注入”(Dependency Injection)
IoC容器应该具有依赖注入功能,因此也可以叫DI容器
IOC和DI其实是同一概念的两种说法,两者站立的角度不一样。
IOC:从spring的角度看,资源的控制权限被反转了
DI:从应用程序的角度看,应用程序要依赖spring为其注入资源。
操作步骤:
//初始化Resource对象Resource res = new ClassPathResource("applicationContext.xml");//初始化BeanFactory对象BeanFactory bf = new XmlBeanFactory(res);//根据id名称获取BeanUserService service = (UserService) bf.getBean("userService");
AppplicationContext与BeanFactory的区别
A. 加载方式不同
AppplicationContext:立即加载,加载配置文件时即加载
BeanFactory:延迟加载,获取Bean实例时才加载
B. AppplicationContext具有更多的功能
国际化处理
事件传递
Bean自动装配
各种不同应用层的Context实现
注意:实际开发中,优先选择ApplicationContext对象,避免使用BeanFactory
Schema风格离线约束配置方式
A.拷贝访问路径
B.打开MyEclipse设置中的XML catalog选项
C.创建新的映射
D.选择对应的schema风格的约束文件
E.将key type修改为schema location
F.将要配置的路径复制到key中
静态工厂初始化
<bean id="userService2"<!--配置中所配置的class配置成工厂类的类名-->class="com.topvision.bean.UserStataicFactory"<!--配置工厂类中的创建实例的静态方法-->factory-method="getInst"></bean>
实例工厂初始化
提供一个实例工厂类,使用其中的实例方法获取对象。由于该工厂类本身需要创建对象,因此该对象必须受Spring控制,所以必须配置该工厂类为Bean.
<!—实例化工厂Bean --><bean id="uf" class="com.topvision.bean.UserFactory"></bean><!--使用实例工厂创建Bean --><!--factory-bean: 配置实例工厂在Spring范围内对应的Bean的id名称--><!--factory-method:配置工厂类中的创建实例的实例方法--><bean id="userService3" factory-bean="uf" factory-method="getInst2"></bean>
Spring初始化的Bean默认为单例模式,如果想修改成非单例模式需要修改Bean的作用范围。
<bean id=”beanId” class=”BeanClassName” scope=”prototype”></bean>
scope属性:
singleton:单例
prototype:非单例
request:请求对象范围request.setAttribute("beanId",obj);
session:会话Session范围request.getSession().setAttribute("beanId",obj);
globalSession:全局会话,分布式服务器
定义Bean初始化与销毁时的动作,属于回调方法配置
定义bean时指定两个回调方法,控制bean的初始化与销毁操作时执行的动作
<bean id="user" class="com.topvision.lifecycle.User" init-method="init" destroy-method="destroy" scope="prototype"></bean>
构造器注入(了解)
A.在domain中提供对象的构造方法
B.xml配置中设置构造方法的参数
<!-- constructor-arg:使用构造器传递参数 --><!-- value:赋值 --><bean id="bean6" class="com.topvision.bean.xml.Bean6"><constructor-arg value="topvision"/><constructor-arg value="2017"/></bean>
注意:如果类型匹配不成功,可以为配置中指定index索引属性,对应构造器中参数的位置.
<bean id="bean6" class="com.topvision.bean.xml.Bean6"><constructor-arg index="0" value="2014" type="java.lang.Integer"></constructor-arg><constructor-arg index="1" value="topvision" type="java.lang.String"></constructor-arg></bean>
说明:构造器传参受构造器参数位置和类型的限定,因此不常使用
setter注入
前提:setter注入要求Bean必须提供无参可访问构造方法
A.注入简单类型
1.提供对应要注入的属性
2.为每个要注入的属性提供对应的标准封装setter方法
3.在配置中为Bean指定要注入的属性,使用property元素 name=“属性名” value=”值”
<property name="属性名" value="值"/>
B.注入引用类型
1.为某个Bean注入引用类型的值,首先在Bean对应的类中声明对应的属性
private TeacherDAO dao;
2.为每个要注入的属性提供对应的标准封装setter方法 (访问器)
public void setDao(TeacherDAO dao) {this.dao = dao;}
3.必须保障引入的对象是Spring控制的Bean
<!-- 声明引用类型的资源为Bean --><bean id="teacherDao" class="com.topvision.di.setter.TeacherDAO"> </bean>
4.在Bean的属性注入中,使用ref引用对应的资源 ref=”beanId/beanName”
<!-- setter注入 --><!-- name:属性名 --><!-- value:简单类型的值 --><!-- ref:引用别的Bean,beanId/beanName --><bean id="bean7" class="com.topvision.bean.xml.Bean7"><property name="name" value="topvision"/><property name="age" value="8"/><property name="dao" ref="teacherDao"></property></bean>
从Spring2.5开始提供使用注解的形式配置Bean。
如需为Bean定义名称,在参数中添加Bean名称@Component("beanName")
用于检测对应的Bean是否配置了注解,并加载配置了注解的类
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.topvision.annotation"></context:component-scan>
说明:扫描路径可以设置多个,中间使用,隔开,扫描路径包含指定包和子包内所有的类,通常使用*号通配符匹配路径
@Autowired@Value("topvision")private String msg;
注意:注解自动装配属性值无需提供对应属性的setter方法
@Autowired@Qualifier("anno2")private AnnotationOther other;