spring第一天
Spring
spring负责管理项目中所有的对象,可看作对象的管家,是对象的容器。
是一个分层的(一站式)轻量级开源框架。
1.导jar包

2.配置文件

添加约束:




在配置文件中配置:

spring_hello
package cn.itcast.s_hello; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.itcast.bean.User; public class TestHello { @Test public void testHello(){ //first.创建容器对象 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); //second.向容器要user对象 User u = (User) ac.getBean("user"); //third.输出 System.out.println(u); } }
IOC(控制反转)



scope:
singleton:单例对象,被标识为单例的对象在容器中只会存在一个实例。
prototype:多例原型,每次在获得才会创建,每次创建都是新的对象。整合struts2时要配成多例的,action每次都是一个新action。
模块化配置:

注入:

构造注入

复杂类型注入:
<bean name="cb" class="cn.itcast.bean.CollectionBean"> <!-- 当只有一个参数时 <property name="arr" value="Lehoop"></property> --> <property name="arr"> <array> <value>aaa</value> <value>bbb</value> <ref bean="car"/> </array> </property> <property name="list"> <list> <value>lll</value> <value>iii</value> <ref bean="user"/> </list> </property> <property name="mao"> <map> <entry key="peng" value="lehoop"></entry> <entry key-ref="user" value="路虎"></entry> </map> </property> <property name="prop"> <props> <prop key="driverClass">com....</prop> <prop key="root">root..</prop> <prop key="password">123456..</prop> </props> </property> </bean>

加入这个jar
浙公网安备 33010602011771号