这是常见的写法,我们如何导入组件
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="user" class="com.ge.springxml.bean.UserBean">
<property name="loginName" value="egd"></property>
<property name="userName" value="二狗蛋"></property>
<property name="password" value="9527"></property>
<constructor-arg index="0" value="egd"></constructor-arg>
<constructor-arg index="1" value="二狗蛋"></constructor-arg>
<constructor-arg index="2" value="9527"></constructor-arg>
</bean>
<!-- 默认情况下,通过springIOC容器产生的组件实例,都是单实例的 -->
<!-- scope 作用范围,或者作用域:默认情况下,是:singleton 单实例的情况下:该对象的生命周期由spring容器完成负责 -->
<!-- scope="prototype" 以容器创建之初,创建的对象为原型,克隆出新的对象,当程序每次通过getBean()在 调用组件实例的时候,就会克隆出一个新的对象。
此时,该克隆对象的生命周期,spring容器只负责生,不负责养,它的生命周期由程序自己负责(JVM-GC) -->
<!-- 其他的了解,比如: scope="request" 表示组件的实例,它的生命周期与HTTP Request一致 scope="session"
表示组件的实例,它的生命周期与HTTP Session一致 scope="global session" 表示组件的实例,它的生命周期与整个集群范围内的HTTP
Session保持一致 -->
<bean id="userServiceImpl"
class="com.ge.springxml.usermag.service.impl.UserServiceImpl"
scope="prototype" autowire="constructor">
<!-- spring容器在完成组件与组件的装配时,有两种方式:手动装配,自动装配 -->
<!-- 手动装配就是使用property|constructor-arg 标记进行装配的方式 -->
<!-- <property name="userDaoImpl" ref="userDaoImpl"></property>
<constructor-arg index="0" ref="userDaoImpl"></constructor-arg> -->
<!-- 自动装配需要使用autowire=""该属性,当属性值为byName的时候,那么在实例化组件时,spring
容器将去检查该组件是否具有属性,并且是否提供了setter(),如果有:那么将获得该属性的属性名,将
属性名作为id,回到spring容器去检查,是否有与id相同的组件存在,如果存在则装配成功 ,如果不存在,在装配时,
不会报错,但是在程序运行时,将抛出NullPointException -->
<!-- 当属性值为byType的时候,那么在实例化组件时,spring
容器将去检查该组件是否具有属性,并且是否提供了setter(),如果有:那么将获得该属性的数据类型,然后回到spring容器
中去匹配是否有与该数据类型一样的组件实例存在,如果找到了并且只有1个,那么装配成功,如果找多了,将会抛出UnsatisfiedDependencyException,
然后提醒你找多了,并且spring容器崩溃。如果1个都没找到,在装配时,不会报错,但是在程序运行时,将抛出NullPointException-->
<!-- 当属性值为constructor的时候,此时的装配就采用的构造器装配方式,在实例化组件时,将获取构造函数中的
参数类型,然后使用byType的方式,完成组件装配,如果一个都没有匹配成功,那么将抛出异常UnsatisfiedDependencyException,
提供你一个都没找到,并且spring容器崩溃。其他的情况将于byType一样 -->
<!-- 当手动装配与自动装配同时存在的情况下,spring容器将以手动为主 -->
</bean>
<bean id="userDaoImpl" class="com.ge.springxml.usermag.dao.impl.UserDaoImpl"></bean>
<!-- 引入其他的配置文件-->
<import resource="configer.xml" />
</beans>
有一个Configer类,它含有以下属性:
private String[] strs;
private List<?> list;
private List<UserBean> users;
private Set<?> set;
private Map params;
private Properties props;
configer.xml(介绍如何利用spring管理这个类,也就是这个组件,并且初始赋值)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="configer" class="com.ge.springxml.bean.Configer">
<property name="strs">
<!-- 数组在进行设值时,可以用到array|list标记 -->
<array>
<!-- value 可以支持String,基本数据类型 -->
<value>张三</value>
<value>张三</value>
</array>
<!-- <list>
<value>张三</value>
<value>张三</value>
</list> -->
</property>
<property name="list">
<list>
<value>123456</value>
<ref bean="userServiceImpl"/>
<!-- bean 中嵌套bean 被嵌套的bean 无需定义id|name属性,因为容器会自动忽略
如果要使用嵌套bean,只能通过o.getter()去获取
嵌套bean 只能在该bean内部使用,无法参与到其他bean组件的引用
-->
<bean class="com.ge.springxml.bean.UserBean"></bean>
</list>
</property>
<!-- 没规定泛型可以乱放,规定那么就要按照规定的定义 -->
<property name="users">
<list>
<bean class="com.ge.springxml.bean.UserBean"></bean>
<ref bean="user"/>
</list>
</property>
<!-- 属性是set的情况-->
<property name="set">
<set>
<value>123456</value>
<ref bean="userServiceImpl"/>
<bean class="com.ge.springxml.bean.UserBean"></bean>
</set>
</property>
<!-- 属性是map的情况-->
<property name="params">
<map>
<entry key="a" value="123456"></entry>
<entry key="b" value-ref="user"></entry>
</map>
</property>
<!-- 属性是props的时候-->
<property name="props">
<props>
<prop key="a">渣渣</prop>
<prop key="b">灰灰</prop>
</props>
</property>
</bean>
</beans>
除了配置,也就是上面的xml文件之外,也可以用注解版,新建一个类,二选其一
/**
* 配置类
* 使用该类就可以替代applicationContext.xml文件
* @author Administrator
*
*/
@Configuration
@ComponentScan(basePackages= {"com.gezhi.springanno"})
public class ApplicationConfigure {
/**
* @Bean 表示该方法的返回是一个需要被spring容器管理起来的组件
* 该组件的id就是方法的方法名
* @return
*/
@Bean
public UserBean userBean() {
return new UserBean();
}
}