当前位置: 首页 > news >正文

做电子政务 网站/灰色词快速排名接单

做电子政务 网站,灰色词快速排名接单,小米应用商店安装下载,html网页设计作品中国传统文化IOC容器: 按照我的理解为:我们需要什么都交给容器来实现 比如原先我们需要得到就要 new 一个实例,有了容器需要实例就让容器创建,需要变量,属性就让容器注入。 简单的接口实例: package com.interface1;…

IOC容器:

按照我的理解为:我们需要什么都交给容器来实现 比如原先我们需要得到就要 new 一个实例,有了容器需要实例就让容器创建,需要变量,属性就让容器注入。

简单的接口实例:

package com.interface1;public interface OneInterface {public void say(String arg);
}

++++++++++++++++++++++++++++++++

package com.interface1;/** OneInterface实现类*/
public class OneInterfaceImpl implements OneInterface {public void say(String arg) {System.out.println("ServiceImpl say: " + arg);}}

++++++++++++++++++++++++++

package com.interface1;public class Main {public static void main(String[] args){/** 简单的面向接口编程:把接口实现类赋值给接口的声明:oif*/OneInterface oif=new OneInterfaceImpl();oif.say("Hello Spring");}
}

++++++++++++++++++++++++++

使用IOC容器来实现(也是上面的接口和接口实现类)

需要junit相关jar包以及spring相关jar包。。。。。。。
工具类:

package com.imooc.test.base;import org.junit.After;
import org.junit.Before;
import org.springframework.beans.BeansException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;public class UnitTestBase {private ClassPathXmlApplicationContext context;private String springXmlpath;public UnitTestBase() {}public UnitTestBase(String springXmlpath) {this.springXmlpath = springXmlpath;}@Beforepublic void before() {if (StringUtils.isEmpty(springXmlpath)) {springXmlpath = "classpath*:spring-*.xml";}try {context = new ClassPathXmlApplicationContext(springXmlpath.split("[,\\s]+"));context.start();} catch (BeansException e) {e.printStackTrace();}}@Afterpublic void after() {context.destroy();}@SuppressWarnings("unchecked")protected <T extends Object> T getBean(String beanId) {try {return (T)context.getBean(beanId);} catch (BeansException e) {e.printStackTrace();return null;}}protected <T extends Object> T getBean(Class<T> clazz) {try {return context.getBean(clazz);} catch (BeansException e) {e.printStackTrace();return null;}}}

package com.interface1;import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;import com.imooc.test.base.UnitTestBase;@RunWith(BlockJUnit4ClassRunner.class)
public class MainTestJunit extends UnitTestBase{public MainTestJunit() {super("classpath*:spring-ioc.xml");}@Testpublic void testSay() {OneInterface oneInterface = super.getBean("oneInterface");oneInterface.say("Hello Boy");}}

Spring配置文件:(spring-ioc.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd" ><bean id="oneInterface" class="com.interface1.OneInterfaceImpl"></bean></beans>

如果像上面的操作似乎比一般的方法更加麻烦

当多个接口有多个对象的时候 就需要一个一个的new了 就很麻烦:
定义两个接口 两个接口实现类
其中InjectionDao为InjectionServiceImpl 的属性

package com.inner;public interface InjectionDao {public void save(String arg);
}
package com.inner;public class InjectionDaoImpl implements InjectionDao {@Overridepublic void save(String arg) {// TODO Auto-generated method stubSystem.out.println("模拟数据库操作:保存数据"+arg);}}
package com.inner;public interface InjectionService {public void save(String arg);
}
如何给InjectionServiceImpl注入值 :两种方法
1:设置注入: propety name 为变量名   要为这个变量设置set方法
2:构造器注入:  需要显式的构造器  构造器的参数名和name后的一致
package com.inner;public class InjectionServiceImpl implements InjectionService{private InjectionDao injectionDAO;//设值注入public void setInjectionDAO(InjectionDao injectionDAO) {this.injectionDAO = injectionDAO;}//构造注入:public InjectionServiceImpl(InjectionDao injectionDAO){this.injectionDAO = injectionDAO;}@Overridepublic void save(String arg) {System.out.println("模拟业务操作:保存数据"+arg);arg=arg+":"+arg.hashCode();injectionDAO.save(arg);}}

+++++++++++++++++++++++++++++++
两种方式在配置文件中得配置:
spring-injection.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd" ><!--  <bean id="injectionService1" class="com.inner.InjectionServiceImpl"> <property name="injectionDAO" ref="injectionDAO"></property></bean>--><bean id="injectionService2" class="com.inner.InjectionServiceImpl"><constructor-arg name="injectionDAO" ref="injectionDAO"></constructor-arg></bean><bean id="injectionDAO" class="com.inner.InjectionDaoImpl"></bean></beans>

测试类:

package com.inner;import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;import com.imooc.test.base.UnitTestBase;@RunWith(BlockJUnit4ClassRunner.class)
public class TestInjection extends UnitTestBase{public TestInjection() {super("classpath*:spring-injection.xml");}//  @Test
//  //设值植入
//  public void testSetter() {
//      InjectionService service = super.getBean("injectionService1");
//      service.save("这是要保存的数据");
//  }//构造植入:@Testpublic void testConstor(){InjectionService service = super.getBean("injectionService2");service.save("这是要保存的数据");}
}

总结:

其中 id为我理解的为该class=“”中类的实例 保存在IOC中 而
name=”injectionDAO” ref=”injectionDAO”>中得name的值为id的属性名 ref的值为<bean id="injectionDAO" class="com.inner.InjectionDaoImpl"></bean>的id值

在xml文件中的类都是实现类 而在InjectionServiceImpl 的实现类中都是接口属性 接口属性使用的该接口实现类来实例化的

问题:

不懂public TestInjection() {
super("classpath*:spring-injection.xml");
}

中的super的classpath加星号和不加星号的区别

这里写图片描述

http://www.lbrq.cn/news/1411687.html

相关文章:

  • 开发论坛网站/厦门网络推广培训
  • 做一家网站需要多少钱/在线crm
  • 电信网络服务商/山西优化公司
  • 购物网站开发 webstorm/国外免费建站网站搭建
  • 网站全景图怎么做/百度指数上多少就算热词
  • 做网站设计需要哪些知识/网络营销考试答案
  • 开设计公司客源哪里找/安卓系统优化软件
  • 成都网站公司/域名购买
  • web网站开发个人主页/百度代发排名
  • 深圳做网站哪家公司好/百度关键词竞价
  • 网站主体负责人/sem竞价广告
  • 网页美工设计与欣赏/seo短期培训班
  • 昆明高端网站建设公司/衡水网站seo
  • 专门做网站的公司 南阳/每日新闻简报
  • 凡科做的网站要收费吗/网站怎么进入
  • h5网站开发模板/百度收录入口提交
  • 中国在数码网站注册域名好 gt/安卓优化大师hd
  • hao123浏览器下载安装/seo页面链接优化
  • 搭建网站怎么做/找平台推广
  • 网站数据库如何建设/seo数据监控平台
  • 网页设计网站建设/爱站小工具
  • 专业网站开发报价/站长seo
  • 自己免费做网站的流程/搜索引擎优化主要包括
  • 做平台的网站有哪些功能/seo是什么软件
  • 如何删除网站后台的文章/网络推广营销方案免费
  • 网站开发 网站建设/上海专业的seo公司
  • wordpress网站显示不全/seo标题优化是什么意思
  • 高端网站建设 选择磐石网络/免费发外链平台
  • wordpress制作客户端/如何点击优化神马关键词排名
  • 网站海外推广哪家好/2022新闻大事件摘抄
  • VLN视觉语言导航(3)——神经网络的构建和优化 2.3
  • 手写MyBatis第24弹:从单条插入到批量处理:MyBatis性能优化的关键技术
  • 决策树学习报告
  • Android Coil3视频封面抽取封面帧存Disk缓存,Kotlin(2)
  • 1. Docker的介绍和安装
  • Makefile介绍(Makefile教程)(C/C++编译构建、自动化构建工具)