6、(DI)依赖注入
DI 与 IOC 的关系:
相同问题不同角度分析。
<bean> <property /> </bean>
复制代码
<property name="propertyName" value="propertyValue" ref="beanId"/>
复制代码
name:对应 bean 中的属性名,要求该属性必须提供可访问的 set 方法(严格规范为此名称是 set 方法对应名称)
value:设定非引用类型属性对应的值,不能与 ref 同时使用
ref:设定引用类型属性对应 bean 的 id ,不能与 value 同时使用
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <!--1.创建spring控制的资源--> <!--bean可以使用多个名称,使用那么属性完成,中间使用,隔开--> <bean id="userService" name="userService1" class="com.DI.service.Impl.UserServiceImpl"> <!--3.将要租注入的引用类型的变量通过property属性进行注入,对应的name是要注入的变量名 使用ref属性声明要注入的bean的id--> <property name="userDao" ref="userDao"/> <property name="num" value="666"/> <property name="version" value="楠木"/> </bean> <!--将要注入的资源声明为bean,引用类型需要声明,非引用类型的不用声明,直接赋值--> <bean id="userDao" class="com.DI.service.Impl.UserDaoImpl"/></beans>
复制代码
6.1、构造器注入
<bean> <constructor-arg /> </bean>
复制代码
<constructor-arg name="argsName" value="argsValue />
复制代码
name:对应 bean 中的构造方法所携带的参数名
value:设定非引用类型构造方法参数对应的值,不能与 ref 同时使用
其他属性:
<constructor-arg index="arg-index" type="arg-type" ref="beanId"/>
复制代码
ref:设定引用类型构造方法参数对应 bean 的 id ,不能与 value 同时使用
type :设定构造方法参数的类型,用于按类型匹配参数或进行类型校验
index :设定构造方法参数的位置,用于按位置匹配参数,参数 index 值从 0 开始计数
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <!--将要注入的资源声明为bean,引用类型需要声明,非引用类型的不用声明,直接赋值--> <bean id="userDao" class="com.DI.service.Impl.UserDaoImpl"/> <bean id="userService" name="userService1" class="com.DI.service.Impl.UserServiceImpl"> <!--不加name,也可以加index:从0开始--> <constructor-arg name="userDao" ref="userDao"/> <constructor-arg name="num" value="66666"/> <constructor-arg name="version" value="楠木"/> </bean></beans>
复制代码
6.2、Set 方式注入【重点】
依赖注入:Set 注入!
【环境搭建】
1.复杂类型
package com.blue.pojo;
public class Address {
private String address;
public String getAddress() { return address; }
public void setAddress(String address) { this.address = address; }}
复制代码
2.真实测试对象
public class Student {
private String name; private Address address; private String[] books; private List<String> hobbies; private Map<String,String> card; private Set<String> games; private String wife; private Properties into;
复制代码
beans.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.blue.pojo.Student"> <!-- 第一种,普通注入,value --> <property name="name" value="blue"/> </bean></beans>
复制代码
MyTest
public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Student student = (Student) context.getBean("student"); System.out.println(student.getName()); }}
复制代码
集合注入方式:
<property> <list></list> </property>
复制代码
(1)集合类型数据注入——list
<property name="al"> <list> <value>itheima</value> <value>66666</value> </list></property>
复制代码
(2)集合类型数据注入——props
<property name="properties"> <props> <prop key="name">itheima666</prop> <prop key="value">666666</prop> </props></property>
复制代码
(3)集合类型数据注入——array (了解)
<property name="arr"> <array> <value>123456</value> <value>66666</value> </array></property>
复制代码
(4)集合类型数据注入——set(了解)
<property name="hs"> <set> <value>itheima</value> <value>66666</value> </set></property>
复制代码
(5)集合类型数据注入——map(了解)
<property name="hm"> <map> <entry key="name" value="itheima66666"/> <entry key="value" value="6666666666"/> </map></property>
复制代码
掌握:props、list
案例:
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <!--1.创建spring控制的资源--> <!--bean可以使用多个名称,使用那么属性完成,中间使用,隔开--> <!-- <bean id="userService" name="userService1" class="com.DI.service.Impl.UserServiceImpl"> <!–3.将要租注入的引用类型的变量通过property属性进行注入,对应的name是要注入的变量名 使用ref属性声明要注入的bean的id–> <property name="userDao" ref="userDao"/> <property name="num" value="666"/> <property name="version" value="楠木"/> </bean>--> <!--将要注入的资源声明为bean,引用类型需要声明,非引用类型的不用声明,直接赋值--> <!--<bean id="userDao" class="com.DI.Dao.Impl.UserDaoImpl"> <!–不加name,也可以加index:从0开始–> <constructor-arg name="password" value="12345"/> <constructor-arg name="username" value="root"/> <constructor-arg name="driver" value="123"/> </bean>
<!–构造注入–>
<bean id="userService" name="userService1" class="com.DI.service.Impl.UserServiceImpl"> <!– 使用构造方法进行set注入,需要保障注入的属性与bean中定义的属性一致–> <!–一致指顺序一致或类型一致或使用index解决问题–> <constructor-arg name="userDao" ref="userDao"/> <constructor-arg name="num" value="66666"/> <constructor-arg name="version" value="楠木"/> </bean>-->
<!--其余类型注入-->
<bean id="userDao" class="com.DI.Dao.Impl.UserDaoImpl"> <!--不加name,也可以加index:从0开始--> <constructor-arg name="password" value="12345"/> <constructor-arg name="username" value="root"/> <constructor-arg name="driver" value="123"/> </bean>
<bean id="userService" name="userService1" class="com.DI.service.Impl.UserServiceImpl"> <property name="userDao" ref="userDao"/> <property name="bookDao" ref="bookDao"/> </bean> <bean id="bookDao" name="bookDao" class="com.DI.Dao.Impl.BookDaoImpl"> <property name="al"> <list> <value>楠木1</value> <value>楠木2</value> </list> </property> <property name="props"> <props> <prop key="name">小米</prop> <prop key="value">大米</prop> </props> </property> <property name="arr"> <array> <value>6666</value> <value>9999</value> </array> </property> <property name="hs"> <set> <value>楠木3</value> <value>楠木4</value> </set> </property> <property name="hm"> <map> <entry key="name" value="楠木5"></entry> <entry key="name" value="楠木6"></entry> </map> </property> </bean>
</beans>
复制代码
6.3、拓展方式注入
<bean p:propertyName="propertyValue" p:propertyName-ref="beanId"/>
复制代码
<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 https://www.springframework.org/schema/beans/spring-beans.xsd">
复制代码
后续课程中还将开启其他的命名空间,方式同上
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl" p:userDao-ref="userDao" p:bookDao-ref="bookDao" />
复制代码
标签 P
简化书写,直接注入属性值
xmlns:p="http://www.springframework.org/schema/p"
复制代码
注意点:p 命名和 c 命名空间不能直接使用,需要导入 xml 约束!
xmlns:p="http://www.springframework.org/schema/p"xmlns:c="http://www.springframework.org/schema/c"
复制代码
<?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.xsd">
<bean id="user" class="com.lfs.pojo.User" p:name="blue" p:age="18"/> </beans>
复制代码
加配置--测试:
test:
public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml"); User user = context.getBean("user", User.class); System.out.println(user); }}
复制代码
标签 C
c 标签:通过构造器注入
xmlns:c="http://www.springframework.org/schema/c"
复制代码
public class User { private String name; private int age;
public User() { }
public User(String name, int age) { this.name = name; this.age = age; }
复制代码
<bean id="user" class="com.blue.pojo.User" p:name="blue" p:age="18"/>
<bean id="user" class="com.blue.pojo.User" c:age="18" c:name="blue"/>
复制代码
SpEL
<property value="EL表达式"></bean>
复制代码
注意:所有属性值不区分是否引用类型,统一使用 value 赋值
所有格式统一使用 value=“********”
常量 #{10} #{3.14} #{2e5} #{‘itcast’}
引用 bean #{beanId}
引用 bean 属性 #{beanId.propertyName}
引用 bean 方法 beanId.methodName().method2()
引用静态方法 T(java.lang.Math).PI
运算符支持 #{3 lt 4 == 4 ge 3}
正则表达式支持 #{user.name matches‘[a-z]{6,}’}
集合支持 #{likes[3]}
案例:
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl"> <property name="userDao" value="#{userDao}"/> <property name="bookDao" value="#{bookDao}"/> <property name="num" value="#{666666666}"/> <property name="version" value="#{'itcast'}"/> </bean>
复制代码
properties 文件加载
xmlns:context="http://www.springframework.org/schema/context"
复制代码
3.加载指定的 properties 文件
<context:property-placeholder location="classpath:filename.properties">
复制代码
4.使用加载的数据
<property name="propertyName" value="${propertiesName}"/>
复制代码
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
"> <!--1.加载context标签命名空间的支持--> <!--xmlns:context="http://www.springframework.org/schema/context" http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd -->
<!--2.加载配置文件-->
<context:property-placeholder location="classpath:*.properties"/>
<!--1.创建spring控制的资源--> <!--bean可以使用多个名称,使用那么属性完成,中间使用,隔开--> <bean id="bookDao" class="com.properties.dao.Impl.BookDaoImpl"/> <!--scope用于控制bean创建后的对象是否是单列--> <bean id="userDao" class="com.properties.dao.Impl.UserDaoImpl"> <property name="name" value="${username}"/> <property name="pass" value="${password}"/> </bean>
<bean id="userService" class="com.properties.service.Impl.UserServiceImpl"> <property name="userDao" ref="userDao"/> <property name="bookDao" ref="bookDao"/> </bean></beans>
复制代码
团队开发
名称:import
类型:标签
归属:beans 标签
作用:在当前配置文件中导入其他配置文件中的项
格式:
<beans> <import /> </beans>
复制代码
<import resource=“config.xml"/>
复制代码
resource:加载的配置文件名
new ClassPathXmlApplicationContext("config1.xml","config2.xml");
复制代码
ApplicationContext
1.ApplicationContext 是一个接口,提供了访问 spring 容器的 API
2.ClassPathXmlApplicationContext 是一个类,实现了上述功能
3.ApplicationContext 的顶层接口是 BeanFactory
4.BeanFactory 定义了 bean 相关的最基本操作
5.ApplicationContext 在 BeanFactory 基础上追加了若干新功能
对比 BeanFactory
FileSystemXmlApplicationContext
BeanFactory
Resource res = new ClassPathResource("applicationContext.xml");BeanFactory bf = new XmlBeanFactory(res);UserService userService = (UserService)bf.getBean("userService");
复制代码
第三方资源配置--->Druid
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/spring_ioc"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean>
复制代码
评论