ApplicationContext 代表IOC容器(控制反转)
ApplicationContext的主要实现类:
——ClassPathXmlApplicationContext:从类路径下加载配置文件
——FileSystemXmlApplicationContext:从文件系统中加载配置文件
依赖注入的方式:
——属性注入(通过setter方法注入Bean的属性值或依赖对象,使用<property>元素,使用name属性指定Bean的属性名称)
——构造方法注入(构造器注入在<constructor-arg>元素里声明属性,<constructor-arg>中没有name属性)
.xml中bean配置:
一、说明:1、如果字面值包含特殊字符可以使用<![CDATA[]]>包裹起来
2、可以使用property中的ref属性建立bean之间的引用关系
3、注入null值时,可使用专用的 <null/>元素标签
<!--配置 class:bean的全类名,通过反射的方式在IOC中创建Bean,所以要求Bean中必须有无参数的构造器 id:标识容器中的bean --> <bean id="helloworld" class="com.atguigu.spring.beans.HelloWorld"> <property name="name" value="1234123"></property> </bean> <!-- 使用构造器注入属性值可以指定参数的位置或类型!以区分重载的构造器! --> <bean id="car" class="com.atguigu.spring.beans.Car"> <constructor-arg value="benchi" index="0"></constructor-arg> <constructor-arg value="508000" type="int"></constructor-arg> <constructor-arg value="2" type="java.lang.Double"></constructor-arg> <!-- 如果字面值包含特殊字符可以使用<![CDATA[]]>包裹起来 --> <constructor-arg type="java.lang.String"> <value><![CDATA[<guangqi~shangqi>]]></value> </constructor-arg> </bean> <!-- 可以使用property中的ref属性建立bean之间的引用关系 --> <bean id="person" class="com.atguigu.spring.beans.Person"> <property name="name" value="lee"></property> <property name="age" value="24"></property> <property name="car" ref="car"></property> <!-- 或 <property name="car"> <ref bean="car"></ref> </property> --> <!-- 或内部bean --> <property name="car"> <bean class="com.atguigu.spring.beans.Car"> <!-- 省略。。。 --> </bean> </property> </bean>
二、通过<list>、<map>、<property>、<util>集合标签进行XML文件的配置:
<!-- 使用list节点 为List类型的属性赋值 --> <bean id="person3" class="com.atguigu.spring.beans.collection.Person"> <property name="name" value="zf"></property> <property name="age" value="26"></property> <property name="cars"> <list> <ref bean="car"/> </list> </property> </bean> <!-- 使用map节点 及map的entry子节点配置Map类型的成员变量 --> <bean id="newperson" class="com.atguigu.spring.beans.collection.NewPerson"> <property name="name" value="nn"></property> <property name="age" value="1"></property> <property name="cars"> <map> <entry key="AA" value-ref="car"></entry> </map> </property> </bean> <!-- 配置Properties --> <bean id="dataSource" class="com.atguigu.spring.beans.collection.DataSource"> <property name="properties"> <props> <prop key="user">root</prop> <prop key="password">1234</prop> <prop key="jdbcUrl">jdbc:mysql:///test</prop> <prop key="driverClass">com.mysql.jdbc.Driver</prop> </props> </property> </bean> <!-- 配置单例的集合bean,以供多个bean进行引用 ,需导入util命名空间--> <util:list id="cars"> <ref bean="car"/> </util:list> <bean id="person4" class="com.atguigu.spring.beans.collection.Person"> <property name="name" value="gg"></property> <property name="age" value="0"></property> <property name="cars" ref="cars"></property> </bean> <!-- 通过p命名空间为bean的属性赋值,需先导入p命名空间 --> <bean id="person5" class="com.atguigu.spring.beans.collection.Person" p:name="ljd" p:age="24" p:cars-ref="cars"></bean> </beans>
三、可以使用autowire属性指定自动装配的方式:
——byName 根据bean的名字和当前bean的setter风格的属性名进行自动装配
——byType 根据bean的类型和当前bean的属性的类型进行自动装配
四、bean的继承(parent)和依赖(depends-on)
<!-- 抽象bean:bean的abstract属性为true的bean.这样的bean不能被IOC容器实例化,只用来被继承配置 若某一个bean的class属性没有指定,则该bean必须是一个抽象(abstract)的bean --> <bean id="address" class="com.atguigu.spring.beans.relation.Address" p:city="zhengzhou" p:street="yihaoxian" abstract="true"></bean> <!-- bean配置的继承:使用bean的parent属性指定继承哪个bean 的配置 --> <bean id="address2" class="com.atguigu.spring.beans.relation.Address" parent="address" p:street="zibailu"></bean> <bean id="car" class="com.atguigu.spring.beans.collection.Car" p:carName="luhu" p:carPrice="750000" p:carType="suv" p:carCount="1"></bean> <!--要求再配置person时,必须有一个关联的car,换句话说Person这个bean依赖于Car这个bean --> <bean id="person" class="com.atguigu.spring.beans.relation.Person" p:name="lee" p:home-ref="address2" depends-on="car"></bean>
五、使用bean的 scope属性来配置作用域 :
singleton:默认值.容器初始化时创建bean实例,在整个容器的生命周期内只创建一个bean,单例的
prototype:原型的.容器初始化时不创建bean实例,而在每次请求时都创建一个新的bean实例,并返回
<bean id="person6" class="com.atguigu.spring.beans.collection.Person" scope="prototype"> <property name="name" value="jim"></property> <property name="age" value="10"></property> </bean>
.java:
//创建spring的IOC容器对象 //ApplicationContext 代表IOC容器 ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml"); //从IOC容器中获取Bean实例 //通过id获取IOC容器中的bean HelloWorld helloWorld = (HelloWorld)act.getBean("helloworld"); //利用类型返回IOC容器中的bean(要求IOC容器中必须只有一个该类型的Bean) HelloWorld helloWorld2 = act.getBean(HelloWorld.class);
获取配置文件bean中的值
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-mybatis.xml");
ComboPooledDataSource pool= (ComboPooledDataSource) ctx.getBean("dataSource"); jdbcUser = pool.getUser(); jdbcPassword = pool.getPassword(); jdbcUrl = pool.getJdbcUrl(); driverClass = pool.getDriverClass(); initialPoolSize = pool.getInitialPoolSize(); maxPoolSize = pool.getMaxPoolSize();