Spring相关配置及工厂模式

摘要:
--该文件生成的所有对象都被spring--˃˂--分配通过set方法使用值对象类型ref的八种基本类型,从而实现对象之间的依赖性--˃˂--构造函数注入˂!

1.  applicationContext.xml配置  ---->此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"
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">
<!-- 该文件产生的所有对象,被spring放入一个称之为spring ioc的容器中 -->
<!-- id 唯一标识 class 指定文件 -->
<!--
property:该class所代表的类的属性
name;属性名
value:属性值
-->
<bean class="spring.entity.student">
<property name="name" value="zs"></property>
<property name="age" value="21"></property>
</bean>
<!-- 数字工厂的简单应用 -->
<bean class="spring.dao.one"></bean>
<bean class="spring.dao.twe"></bean>
<!-- 老师 课程 -->
<bean p:name="ww" p:age="30">
<!-- <property name="name" value="zs"></property>
<property name="age" value="21"></property>
构造器注入
<constructor-arg value="ls"></constructor-arg>
<constructor-arg value="24"></constructor-arg>
-->

</bean>
<bean p:name="tiyu" p:time="300" p:teacher-ref="teacher"><!-- p注入的时候 普通类型正常写,对象类型用ref
多个p之间要留空格 -->
<!-- set方式的依赖注入 通过set()方法赋值
赋值 默认使用的是set方法
name——>setname() 到路径找这个方法
依赖注入底层通过反射实现 -->
<!-- 通过set方法赋值
<property name="name" value="shuxue"></property>
<property name="time" value="60"></property>
<property name="teacher" ref="teacher"></property>八大基本类型用value
对象类型 ref ——也因此实现了对象与对象之间的依赖关系
-->
<!-- 构造器注入
<constructor-arg value="c"></constructor-arg>
<constructor-arg value="100"></constructor-arg>
<constructor-arg ref="teacher"></constructor-arg>
如果构造函数值位置不同可以根据 index 通过下标确定值 下标默认从零开始
也可以通过name 来确定属性名找到属性值
如果参数类型不同,也可以通过type 来通过类型来确认
这几种可以同时加上
-->


</bean>
<!-- 给集合,数组等类型赋值 -->
<bean class="spring.entity.AllCollectionType">
<property name="list"><!-- 这个list只是一个单纯的属性名,不是专有名 属性名写啥 这个里面就写啥 -->
<list>
<value>足球</value>
<value>篮球</value>
<value>乒乓球</value>
</list>
</property>
<property name="array">
<array>
<value>足球1</value>
<value>篮球1</value>
<value>乒乓球1</value>
</array>
</property>
<property name="set">
<set>
<value>足球2</value>
<value>篮球2</value>
<value>乒乓球2</value>
</set>
</property>
<property name="map">
<map>
<entry>
<key><value>qian</value></key>
<value>铅球</value>
</entry>
</map>
</property>
<property name="pro">
<props>
<prop key="yu">羽毛球</prop>
</props>
</property>
</bean>
</beans>

2.  spring所需要jar包------->本次测试用

Spring相关配置及工厂模式第1张

3.spring   ioc  工厂模式,跟普通工厂对比

首先,因为测试我简单的创建一个接口,以及两个java类用来继承这个接口

接口

Spring相关配置及工厂模式第2张

接口实现类

Spring相关配置及工厂模式第3张

 Spring相关配置及工厂模式第4张

 做一个简单的工厂模式

Spring相关配置及工厂模式第5张

 测试  简单工厂跟Spring工厂对比

Spring相关配置及工厂模式第6张

   注:spring测试用的<bean>在前面的applicationcontext.xml里配置有

   我再截取一遍

Spring相关配置及工厂模式第7张

 class是路径

免责声明:文章转载自《Spring相关配置及工厂模式》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇iOS开发基础知识--碎片27uchar4下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Spring注入方式(1)

      Spring支持3种依赖注入方式,分别为属性注入、构造器注入和工厂方法注入(很少使用,不推荐),下面分别对属性注入和构造器注入详细讲解。 1、常量注入   属性注入是通过setter方法注入Bean的属性值,属性注入使用<property>元素,使用name属性指定Bean的属性名称,使用value属性或者<value>...

SpringBoot初学(4)– JdbcTemplate和Mybatis

前言 github: https://github.com/vergilyn/SpringBootDemo 代码位置: 一、Spring Boot集成JdbcTemplate或NamedParameterJdbcTemplate spring boot中JdbcTemplate与NamedParameterJdbcTemplate都是被自动配置的...

springboot之多数据源配置JdbcTemplate

springboot多数据源配置,代码如下 DataSourceConfig package com.rookie.bigdata.config; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.contex...

SpringCloud之Hystrix Dashbord监控中心+分布式配置中心

HystrixDashbord Hystrix(注意 是单纯的Hystrix) 提供了对于微服务调用状态的监控(信息), 但是,需要结合spring-boot-actuator 模块一起使用。 在包含了 hystrix的项目中, 引入依赖: <dependency>   <groupId>org.springframework.bo...

两个springboot项目如何部署在同一个服务上,两个spring启动应用程序部署在同一台服务器上

现在有两个spring boot的maven项目,需要部署到同一个外部的服务上(tomcat),他们都包含spring-boot-starter-actuator(健康检查)的依赖,但是出现下面的异常 我有多个spring引导应用程序(maven项目),其中包含“spring-boot-starter-actuator”依赖项,用于检查每个应用程序的运行状...

SSH三大框架整合步骤

Struts2:需要整合的第一个框架: 1.创建一个动态web项目 2.导入struts2必须的jar 放到 lib目录下 ,再 build path 添加web工程中 3.配置struts2的核心配置文件:struts.xml 4.在web.xml文件中添加struts2的核心过滤器 5.添加测试页面 6.导入tomcat jar包 对j2e...