依赖注入(Dependency Injection)
它是 Spring 框架核心 IOC 的具体实现。 在编写程序时,通过控制反转,把对象的创建交给了 Spring,但是代码中不可能出现没有依赖的情况。
IOC 解耦只是降低他们的依赖关系,但不会消除。例如:业务层仍会调用持久层的方法。 那这种业务层和持久层的依赖关系,在使用 Spring 之后,就让 Spring 来维护了。
简单的说,就是坐等框架把持久层对象传入业务层,而不用我们自己去获取
Bean的依赖注入方式
①构造方法
创建有参构造
public class service {Dao dao;public service(Dao dao) {this.dao = dao;}public service() {}public void save(){dao.save();}public static void main(String[] args) {ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");service i=(service) applicationContext.getBean("service");i.save();}
}```java
<?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="impl" class="com.ImplDao" /><bean id="service" class="com.service"><constructor-arg name="dao" ref="impl"/></bean>
</beans>
②set方法
<?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="impl" class="com.ImplDao" /><bean id="service" class="com.service"><property name="dao" ref="impl"/></bean>
</beans>
public class service {Dao dao;public service(Dao dao) {this.dao = dao;}public service() {}public void save(){dao.save();}public void setDao(Dao dao) {this.dao = dao;}public static void main(String[] args) {ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");service i=(service) applicationContext.getBean("service");i.save();}
}
set方法:P命名空间注入
P命名空间注入本质也是set方法注入,但比起上述的set方法注入更加方便,主要体现在配置文件中,如下: 首先,需要引入P命名空间:
xmlns:p="http://www.springframework.org/schema/p"
<bean id="impl" class="com.ImplDao" /><bean id="service" class="com.service" p:dao-ref="impl"></bean>
</beans>
Bean的依赖注入的数据类型
(1)普通数据类型的注入
<?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="impl" class="com.ImplDao" /><bean id="service" class="com.service" p:dao-ref="impl"><property name="name" value="aaa" /><property name="no" value="15"/></bean>
</beans>
public class service {Dao dao;int no;String name;public void setNo(int no) {this.no = no;}public void setName(String name) {this.name = name;}public service(Dao dao) {this.dao = dao;}public service() {}public void save(){dao.save();System.out.println(this.toString());}public void setDao(Dao dao) {this.dao = dao;}public static void main(String[] args) {ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");service i=(service) applicationContext.getBean("service");i.save();}
}
(2)集合数据类型(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"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="impl" class="com.ImplDao" /><bean id="service" class="com.service" p:dao-ref="impl"><property name="name" value="aaa" /><property name="no" value="15"/><property name="list"><list><value>aaa</value><value>aadasa</value><value>sada</value></list></property></bean>
</beans>
(4)集合数据类型( Map<String,ImplDao> )的注入
<?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="impl" class="com.ImplDao" /><bean id="impl2" class="com.ImplDao" /><bean id="service" class="com.service" p:dao-ref="impl"><property name="name" value="aaa" /><property name="no" value="15"/><property name="list"><list><value>aaa</value><value>aadasa</value><value>sada</value></list></property><property name="map"><map><entry key="i1" value-ref="impl"/><entry key="i2" value-ref="impl2"/></map></property></bean>
</beans>
(5)集合数据类型(Properties)的注入
<?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="impl" class="com.ImplDao" /><bean id="impl2" class="com.ImplDao" /><bean id="service" class="com.service" p:dao-ref="impl"><property name="name" value="aaa" /><property name="no" value="15"/><property name="list"><list><value>aaa</value><value>aadasa</value><value>sada</value></list></property><property name="map"><map><entry key="i1" value-ref="impl"/><entry key="i2" value-ref="impl2"/></map></property><property name="properties"><props><prop key="aa">11</prop><prop key="bb">22</prop></props></property></bean>
</beans>
引入其他配置文件(分模块开发)
实际开发中,Spring的配置内容非常多,这就导致Spring配置很繁杂且体积很大,所以,可以将部分配置拆解到其 他配置文件中,而在Spring主配置文件通过import标签进行加载
<import resource="applicationContext-dao.xml" />