Srping:是一种优秀的轻量级企业开发框架。大大地简化了企业应用开发的复杂性。通过IOC和AOP同一了对对象的配置、查找和生命周期的管理,从而实现了业务层中不同基础业务的分离。统一管理,效率高。
*Spring中的对象并不依赖SpringAPI
核心容器的主要组成部分是BeanFactory
Application context 扩展了BeanFactory
Spring模块
1、AOP:声明事务的管理等
2、JDBC和Dao:底层操作
3、ORM关系映射模式 :Resource mapping
4、web模块
5、MVC模块
Spring控制反转IOC
是用容器去new对象,而不是用代码
由容器去控制程序,控制权由程序代码交给了容器
依赖注入
容器将依赖关系动态的注入到组件中
在src目录下的application.xml中的配置文件:
<!--bean里放类实例化的id 和类的全类名 -->
<bean id="teacher" class="com.yyy.ioc.bean.教师">
<!--property中 跟类中的属性 多个属性往下边跟就好了 -->
<property name="姓名">
<!--在value中存值-->
<value>王老师</value>
</property>
</bean>
<!--set、或者list集合-->
<property name="teachers">
<set>
<value>刘老师</value>
<value>李老师</value>
<value>张老师</value>
</set>
</property>
<!--Map集合-->
<property name="map">
<map>
<entry key="key1" value="value1"></entry>
<entry key="key2" value="value2"></entry>
<entry key="key3" value="value3"></entry>
</map>
</property>
<!--自定义类型-->
<property name="stu">
<ref bean="stu" />
</property>
获取在application.xml中存的值的取法:
//
ClassPathResource resource=new ClassPathResource("/application.xml");
BeanFactory bean =new XmlBeanFactory(resource);
student si =(student)bean.getBean("stu");
bean 中的就相当于调用的默认的无参构造方法
property 中的内容就相当于调用的setxxx方法
一般的说EL表达式就是相当于getxxx方法
用构造器构造
用构造器构造时,需要注意:
首先在bean中 需要写写全参数的构造函数
public class UserInfo {
private Integer userId;
private String userName;
private String userPass;
public UserInfo(Integer userId, String userName, String userPass) {
this.userId = userId;
this.userName = userName;
this.userPass = userPass;
}
set/get方法。。。。
}
其次,在application.xml中,还需要配置,I将property标签换成constructor-arg标签
<bean id="ui" class="com.yyy.bean.UserInfo" >
<!--index 跟的是参数下标的顺序-->
<constructor-arg index="0">
<value>1<value>
</constructor-arg>
<constructor-arg index="1">
<value>张三<value>
</constructor-arg>
<constructor-arg index="2">
<value>123<value>
</constructor-arg>
</bean>