百度搜自己的网站,商城app开发要多少钱,网站最新程序策划书,wordpress 登依赖注入#xff08;Dependency Injection#xff09;
它是 Spring 框架核心 IOC 的具体实现。 在编写程序时#xff0c;通过控制反转#xff0c;把对象的创建交给了 Spring#xff0c;但是代码中不可能出现没有依赖的情况。 IOC 解耦只是降低他们的依赖关系#xff0c;…依赖注入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 applicationContextnew ClassPathXmlApplicationContext(applicationContext.xml);service i(service) applicationContext.getBean(service);i.save();}
}java
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean idimpl classcom.ImplDao /bean idservice classcom.serviceconstructor-arg namedao refimpl//bean
/beans②set方法
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean idimpl classcom.ImplDao /bean idservice classcom.serviceproperty namedao refimpl//bean
/beanspublic 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 applicationContextnew ClassPathXmlApplicationContext(applicationContext.xml);service i(service) applicationContext.getBean(service);i.save();}
}
set方法:P命名空间注入 P命名空间注入本质也是set方法注入但比起上述的set方法注入更加方便主要体现在配置文件中如下 首先需要引入P命名空间 xmlns:phttp://www.springframework.org/schema/pbean idimpl classcom.ImplDao /bean idservice classcom.service p:dao-refimpl/bean
/beansBean的依赖注入的数据类型
1普通数据类型的注入
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:phttp://www.springframework.org/schema/pxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean idimpl classcom.ImplDao /bean idservice classcom.service p:dao-refimplproperty namename valueaaa /property nameno value15//bean
/beanspublic 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 applicationContextnew ClassPathXmlApplicationContext(applicationContext.xml);service i(service) applicationContext.getBean(service);i.save();}
}
2集合数据类型List的注入
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:phttp://www.springframework.org/schema/pxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean idimpl classcom.ImplDao /bean idservice classcom.service p:dao-refimplproperty namename valueaaa /property nameno value15/property namelistlistvalueaaa/valuevalueaadasa/valuevaluesada/value/list/property/bean
/beans4集合数据类型 MapString,ImplDao 的注入
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:phttp://www.springframework.org/schema/pxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean idimpl classcom.ImplDao /bean idimpl2 classcom.ImplDao /bean idservice classcom.service p:dao-refimplproperty namename valueaaa /property nameno value15/property namelistlistvalueaaa/valuevalueaadasa/valuevaluesada/value/list/propertyproperty namemapmapentry keyi1 value-refimpl/entry keyi2 value-refimpl2//map/property/bean
/beans5集合数据类型Properties的注入
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:phttp://www.springframework.org/schema/pxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean idimpl classcom.ImplDao /bean idimpl2 classcom.ImplDao /bean idservice classcom.service p:dao-refimplproperty namename valueaaa /property nameno value15/property namelistlistvalueaaa/valuevalueaadasa/valuevaluesada/value/list/propertyproperty namemapmapentry keyi1 value-refimpl/entry keyi2 value-refimpl2//map/propertyproperty namepropertiespropsprop keyaa11/propprop keybb22/prop/props/property/bean
/beans引入其他配置文件分模块开发
实际开发中Spring的配置内容非常多这就导致Spring配置很繁杂且体积很大所以可以将部分配置拆解到其 他配置文件中而在Spring主配置文件通过import标签进行加载 import resourceapplicationContext-dao.xml /