加载Spring IOC容器 和 获取bean
public class IoCTest {
ApplicationContext ioc;
@Before
public void before(){
// 加载spring容器
//ApplicationContext spring的顶层核心接口
// ClassPathXmlApplicationContext 根据项目路径的xml配置来实例化spring容器
// FileSystemXmlApplicationContext 根据磁盘路径的xml配置来实例化spring容器
// AnnotationConfigApplicationContext 根据javaconfig 来配置实例化spring容器
// 在容器实例化的时候 就会加载所有的bean
ioc=new ClassPathXmlApplicationContext("spring-ioc.xml");
}
@Test
public void test01(){
System.out.println("Spring容器已加载");
// 获取bean
// 1.通过类来获取bean getBean(User.class)
// 2. 通过bean的名字或者id来获取Bean (User) ioc.getBean("user");
// 3. 通过名字+类型
User bean = ioc.getBean("user",User.class);
System.out.println(bean);
}
}
bean的相关设置(一)
<?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" xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--设置Bean的别名-->
<alias name="user" alias="tulingxueyuan"></alias>
<!--使用name也可以设置别名
使用 空格或者,或者; 设置多个别名
-->
<bean class="cn.tulingxueyuan.beans.User" id="user" name="user2 user3,user4;user5">
<description>用来描述一个Bean是干嘛的</description>
</bean>
<!--使用import可以导入其他spring的xml配置文件
<import resource="spring-ioc.xml"></import>-->
<!--***************************依赖注入 Begin*************************************-->
<!--基于setter方法的依赖注入
注意: name属性对应的set方法的名字
比如 setId -> name="id" setXxx -> name="xxx"
-->
<bean class="cn.tulingxueyuan.beans.User" id="user6">
<property name="idxx" value="1"></property>
<property name="username" value="徐庶"></property>
<property name="realname" value="吴彦祖"></property>
</bean>
<!--基于构造函数的依赖注入
1. 基于name属性设置构造函数参数
2. 可以只有value属性
3. 如果省略name属性 一定注意参数顺序
4. 如果参数顺序错乱
可以使用name,
还可以使用index:设置参数的下标 从0开始
还可以使用type: 在错乱的参数类型一致的情况下不能使用
-->
<bean class="cn.tulingxueyuan.beans.User" id="user7">
<constructor-arg name="id" value="1"></constructor-arg>
<constructor-arg name="username" value="徐庶"></constructor-arg>
<constructor-arg name="realname" value="刘德华"></constructor-arg>
</bean>
<!--复杂数据类型的依赖注入-->
<bean class="cn.tulingxueyuan.beans.Person" id="person">
<property name="id" value="1"></property>
<!--设置null-->
<property name="name">
<null></null>
</property>
<property name="gender" value=""></property>
<!--引用外部Bean
<property name="wife" ref="wife"></property>-->
<!--使用内部bean 依赖注入其他bean-->
<property name="wife">
<bean class="cn.tulingxueyuan.beans.Wife">
<property name="age" value="18"></property>
<property name="name" value="地理热吗"></property>
</bean>
</property>
<!--list 注入:
如果泛型是基本数据类型<value>
如果泛型是bean <bean>-->
<property name="hobbies">
<list>
<value>唱歌</value>
<value>跳舞</value>
</list>
</property>
<!--map 注入
如果value是基本数据类型<entry key="1" value="Java"></entry>
如果value是bean value-ref-->
<property name="course">
<map>
<entry key="1" value="Java"></entry>
<entry key="2" value="数据库"></entry>
</map>
</property>
</bean>
<!--使用p命名空间简化基于setter属性注入XML配置
p:按Alt+Enter 自动加上命名空间
设置基本数据类型 或者p:wife-ref 引用外部bean
如果有集合类型 就不支持, 需要额外配置 <property>
-->
<bean class="cn.tulingxueyuan.beans.Wife" id="wife" p:age="18" p:name="迪丽热巴">
</bean>
<bean class="cn.tulingxueyuan.beans.Person" id="person2" p:wife-ref="wife2" >
<property name="hobbies">
<list>
<value>唱歌</value>
<value>跳舞</value>
</list>
</property>
</bean>
<!--使用c命名空间简化基于构造函数的XML-->
<bean class="cn.tulingxueyuan.beans.Wife" id="wife2" c:age="20" c:name="林青霞">
<!--<constructor-arg></constructor-arg>-->
</bean>
<!--***************************依赖注入 End*************************************-->
</beans>
bean的相关设置(二)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--控制bean加载顺序 当一个bean想让另一bean在它之前加载可以设置depends-on
<bean class="cn.tulingxueyuan.beans.User" id="user" depends-on="wife"></bean>
<bean class="cn.tulingxueyuan.beans.Wife" id="wife"></bean>-->
<!--懒加载
就不会在spring容器加载的时候 加载该bean
而是在使用的时候才会加载该bean
<bean class="cn.tulingxueyuan.beans.Wife" id="wife" lazy-init="true"></bean>-->
<!--作用域scope
singleton 默认值 同一个id始终只会创建一次bean
prototype 多例(原型) 每一次使用都会创建一个bean
<bean class="cn.tulingxueyuan.beans.Person" id="person" scope="prototype" >
</bean>
-->
<!--使用静态工厂方法实例化Bean
<bean class="cn.tulingxueyuan.beans.Person" id="person" factory-method="createPersonFactory" >
</bean>-->
<!--使用实例工厂方法实例化
<bean class="cn.tulingxueyuan.beans.PersonFacotry" id="personFacotry"></bean>
<bean class="cn.tulingxueyuan.beans.Person" id="person"
factory-bean="personFacotry"
factory-method="createPersonFacotryMethod" >
</bean>-->
<!-- 自动注入
byType 根据类型去自动匹配 当出现多个类型或者匹配到类型则会报错
byName 根据set方法的名字去自动匹配
constructor 根据构造器去匹配
优先会根据参数名字去匹配,假如参数名字没有匹配到,会根据参数类型去匹配
会根据构造函数的参数进行完整的匹配注入: 如果构造函数的参数Person(Wife wife3,User user) ioc容器里面必须要有同时有wife和user
名字没有匹配到会根据类型匹配 类型假如出现多个会注入失败但是不会报错
当根据类型匹配到多个 可以使用 1.设置某个bean为主要bean primary="true"
2.设置不需要自动注入的bean autowire-candidate="false" 忽略自动注入
<bean class="cn.tulingxueyuan.beans.Person" id="person" autowire="byType" >
</bean>
<bean class="cn.tulingxueyuan.beans.Wife" id="wife" autowire-candidate="false">
<property name="name" value="迪丽热巴"></property>
</bean>
<bean class="cn.tulingxueyuan.beans.Wife" id="wife2" >
<property name="name" value="迪丽热吗"></property>
</bean>
<bean class="cn.tulingxueyuan.beans.User" id="user"></bean>-->
<!--生命周期回调
<bean class="cn.tulingxueyuan.beans.Person" id="person" init-method="initByConfig" destroy-method="destroyByConfig"></bean>
-->
<!--配置第三方bean
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<property name="username" value="${mysql.username}"></property>
<property name="password" value="${mysql.password}"></property>
<property name="url" value="${mysql.url}"></property>
<property name="driverClassName" value="${mysql.driverClassName}"></property>
</bean>-->
<!--引入外部属性资源文件
<context:property-placeholder location="db.properties"></context:property-placeholder>-->
<bean class="cn.tulingxueyuan.beans.Person" id="person">
<!--运算表达式-->
<property name="id" value="#{1+2}"></property>
<!--引用外部bean-->
<property name="wife" value="#{wife}"></property>
<!--调用bean的属性-->
<property name="name" value="#{wife.name}"></property>
<!--调用bean的方法-->
<property name="gender" value="#{wife.getName()}"></property>
<!--调用静态方法-->
<property name="birthday" value="#{T(cn.tulingxueyuan.beans.PersonFacotry).getNowDate() }"></property>
</bean>
<bean class="cn.tulingxueyuan.beans.Wife" id="wife">
<property name="name" value="迪丽热吗"></property>
</bean>
</beans>
我成功因为我志在成功