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

做网站宣传有用吗/爱站网 关键词挖掘工具站

做网站宣传有用吗,爱站网 关键词挖掘工具站,如何做求婚网站,灯具做外贸的网站有哪些1.准备工作 下载Spring:http://repo.spring.io/libs-release-local/org/springframework/spring/ 选择需要下载的版本 选择dist.zip结尾的下载即可。 下载完后将其解压,进入libs文件夹,libs文件夹下有很多jar文件. 目前阶段只需要将 spring-core-x.x.x.R…

1.准备工作

   下载Spring:http://repo.spring.io/libs-release-local/org/springframework/spring/

    

   选择需要下载的版本

    选择dist.zip结尾的下载即可。

   下载完后将其解压,进入libs文件夹,libs文件夹下有很多jar文件.

   目前阶段只需要将 spring-core-x.x.x.RELEASE.jar 、spring-beans-x.x.x.RELEASE.jar

    spring-context-x.x.x.RELEASE.jar、 spring-expression-x.x.x.RELEASE.jar导入到项目中。

   要是想全部导入也可以。

   下载commons-Logging.jar

   下载地址:http://commons.apache.org/proper/commons-logging/download_logging.cgi

   点击下载。

   将解压后的commons-logging.jar导入项目。

 

    

2.核心容器(BeanFactory,ApplicationContext)

  BeanFactory:BeanFactory是org.springframework.beans.facytory . BeanFactory 接口定义.是基础的IOC(控制反转)容器。

  控制反转即控制权发生了反转,例如创建一个对象,一般都是我们通过new来创建,而发生控制反转后创建由

  容器来创建(容器会根据配置文件进行创建),不用我们显示的new一个对象。

  可以将BeanFactory看作一个管理bena的工厂类,负责初始化等操作。

   

  ApplicationContext:ApplicationContext是BeanFactory的子接口,除了包含BeanFactory中所有功能外还包含

  国际化、资源访问、事件传播等方面的支持。

  ApplicationContext实例创建方法:

    2.1.ClassPathXmlApplication

    ClasspathXmlApplication会从类路径中寻找指定的xml文件并加载。(一般采用这种方式)

ApplicationContext applicationContext = new ClassPathApplicationContext("xxx.xml");

  

    2.2.FileSystemXmlApplicationContext

    FileSystemXmlApplication是指定绝对路径来进行查找,这种方式不灵活使用较少。

ApplicationContext ac = new FileSystemXmlApplicationContext("C:text/.../xxx.xml");

 

    ApplicationContext中有一个方法:Object getBean(String name);通过name(xml文件中的id属性)获取指定类。

 

 

  2.3 小例子

  首先创建一个web项目。

  2.3.1我们创建一个Animal接口,并定义一个say()方法。

public interface Animal {public void say();
}

  

  2.3.2再创建一个类Cat实现Animal接口

public class Cat implements Animal{@Overridepublic void say() {// TODO Auto-generated method stubSystem.out.println("喵喵喵");}
}

  

  2.3.3在src目录下创建beans.xml文件(一般命名为beans.xml,也有叫applicatinContex.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-4.3.xsd"> 
<!--  一将指定类配置给 Spring,让 Spring 创建其对象的实例一 --><bean id = "cat" class = "com.ioc.Cat"> </bean> <!-- 设置id及对应类 -->
</beans>

 

  2.3.4测试IOC容器实例化对象

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");Animal cat = (Animal)ac.getBean("cat");//cat为配置文件中id属性的值,通过配置文件的id获取对应类cat.say();}
}
运行结果:
二月 09, 2019 5:07:47 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@46f7f36a: startup date [Sat Feb 09 17:07:47 CST 2019]; root of context hierarchy
二月 09, 2019 5:07:47 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
喵喵喵

 

上例我们没有显示的new,而是通过容器创建了一个对象,控制发生了反转从自己控制变成了容器控制。

容器创建时默认是单例模式,即每次获取的都是一个对象。要想每次获取的对象都是不同的对象可按如下设置:

<bean id = "cat" class = "com.ioc.Cat" scope = "prototype”> </bean>

 

 

3.依赖注入

  问题引出:假如当前有一个Person类,person要养宠物,于是我们可以这样写;

public Person{Dog dog;Cat cat;......
}        

person每想养一个宠物就要修改代码为其添加一个宠物,这样很麻烦,而且扩展性解耦性都不好。

于是我们首先可以将Dog、Cat改为Animal,这样比之前好一些了,所有宠物都是Animal。

 

但这样在需求有变时还是需要修改一些内容,这样的确比之前的想法方便了一些,但有没有更方便的方法呢?

如果Animal的属性是通过.xml文件中的设置来进行赋值的呢?这样是不是更方便了,当需求改变时在xml修改即可。

 

我们就在原有例子的基础上来进行这个实验:

  3.1创建一个Dog类,实现Animal接口

public class Dog implements Animal{@Overridepublic void say() {// TODO Auto-generated method stubSystem.out.println("汪汪汪");}
}

  

  3.2创建Person类

public class Person {private Animal animal;//养的宠物public Animal getAnimal() {return animal;}public void setAnimal(Animal animal) {//set方法一定要有this.animal = animal;}public void palyAnimal() {//设置一个玩宠物的方法animal.say();//玩宠物宠物发出来对应的声音}
}

 

  3.3beans.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-4.3.xsd"> 
<!--  一将指定类配置给 Spring,让 Spring 创建其对象的实例一 --><bean id = "cat" class = "com.ioc.Cat"> </bean><bean id = "person" class = "com.ioc.Person">  <!--设置Person类及其对应id--><property name="animal" ref="cat"></property> <!--设置属性 name为属性名 ref为将bean定义的实例注入到属性(id = cat 注入)--></bean>
</beans>

 

  3.4测试

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");Person person = (Person)ac.getBean("person");person.palyAnimal();}
}
运行结果:
二月 09, 2019 5:31:01 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@46f7f36a: startup date [Sat Feb 09 17:31:01 CST 2019]; root of context hierarchy 二月 09, 2019 5:31:01 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [beans.xml] 喵喵喵

如果不想养猫了,只需将beans.xml文件中的ref修改下即可。

 

转载于:https://www.cnblogs.com/huang-changfan/p/10357707.html

http://www.jmfq.cn/news/4975291.html

相关文章:

  • 做针对国外的网站/山东做网站
  • 慈溪网站建设哪家好/怎么免费制作网页
  • app界面设计模板一套/苏州网站建设优化
  • 网站做三方登陆需要注册公司不/苏州搜索引擎排名优化商家
  • 网站建设先进材料/网络营销手段有哪些
  • 做类似美团的网站得多少钱/平台推广渠道
  • 1m带宽做网站速度怎么样/seo解释
  • 自己做的网站怎么加入微信支付/aso优化渠道
  • 怡清源在慧聪网网站建设情况/怎样在百度上发帖子
  • 衢州在建高铁站/百度seo排名360
  • 电商眼/阳东网站seo
  • 响应式网站的设计趋势/太原百度搜索排名优化
  • 山东网站优化/快排seo排名软件
  • 郑州的网站建设公司/首页关键词怎么排名靠前
  • 企业网站设置/友情链接互换网站
  • 网站收录代做/如何设置友情链接
  • 赤峰网站开发公司/网店
  • 吴江网站建设/黑帽seo是什么意思
  • 天津市企业网站建设公司/seo做的好的网站
  • 装饰公司响应式网站建设案例/seo实战培训班
  • 陇南市城乡建设局网站/长沙百度搜索排名优化
  • 建设政府门户网站/集合竞价口诀背熟6句
  • 哪个网站开发培训好/福建网络seo关键词优化教程
  • 黄山网站开发jidela/如何网上免费打广告
  • 广州网站建设weeken/seo排名点击
  • 大尺度做爰网站/整合营销传播工具有哪些
  • 网站app怎么做/效果最好的推广软件
  • php对比java做网站/高端网站建设公司
  • 重庆欧勒精细陶瓷有限公司网站策划书/建网站用什么软件
  • 如何做网站推广及优化/国内十大搜索引擎排名