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

兰州市住房建设局网站定制型网站建设合同范本

兰州市住房建设局网站,定制型网站建设合同范本,这样做网站推广,今科网站建设怎么样1.Spring 的依赖 ⽤来设置两个 bean 的创建顺序。 IoC 容器默认情况下是通过 spring.xml 中 bean 的配置顺序来决定创建顺序的,配置在前⾯的 bean 会 先创建。 在不更改 spring.xml 配置顺序的前提下,通过设置 bean 之间的依赖关系来调整 bean 的创…

1.Spring 的依赖

⽤来设置两个 bean 的创建顺序。

IoC 容器默认情况下是通过 spring.xml 中 bean 的配置顺序来决定创建顺序的,配置在前⾯的 bean 会

先创建。

在不更改 spring.xml 配置顺序的前提下,通过设置 bean 之间的依赖关系来调整 bean 的创建顺序。

<bean id="account" class="com.southwind.entity.Account" depends-on="user">
</bean>
<bean id="user" class="com.southwind.entity.User"></bean>

上述代码的结果是先创建 User,再创建 Account。

2.Spring 读取外部资源

实际开发中,数据库的配置⼀般会单独保存到后缀为 properties 的⽂件中,⽅便维护和修改,如果使⽤

Spring 来加载数据源,就需要在 spring.xml 中读取 properties 中的数据,这就是读取外部资源。

jdbc.properties

user = root
password = root
url = jdbc:mysql://localhost:3306/library
driverName = com.mysql.cj.jdbc.Driver

spring.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:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
"><!-- 导入外部资源 --><context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder><!-- SpEL --><bean id="dataSource" class="com.southwind.entity.DataSource"><property name="user" value="${user}"></property><property name="password" value="${password}"></property><property name="driverName" value="${driverName}"></property><property name="url" value="${url}"></property></bean></beans>

3.Spring p 命名空间

p 命名空间可以⽤来简化 bean 的配置。

<bean id="student" class="com.southwind.entity.Student" p:id="1" p:name="张三"
p:age="22" p:classes-ref="classes">
</bean>
<bean id="classes" class="com.southwind.entity.Classes" p:id="1" p:name="⼀班">
</bean>

4.Spring ⼯⼚⽅法

IoC 通过⼯⼚模式创建 bean 有两种⽅式:

  • 静态⼯⼚⽅法
  • 实例⼯⼚⽅法

区别在于静态⼯⼚类不需要实例化,实例⼯⼚类需要实例化

静态⼯⼚⽅法

1、创建 Car 类

@Data
@AllArgsConstructor
public class Car {private Integer num;private String brand;
}

2、创建静态⼯⼚类、静态⼯⼚⽅法

public class StaticCarFactory{private static Map<Integer, Car> carMap;static {carMap = new HashMap<>();carMap.put(1,new Car(1,"奥迪"));carMap.put(2,new Car(2,"奥拓"));}public static Car getCar(Integer num){return carMap.get(num);}
}

3、spring.xml

<bean id="car1" class="com.southwind.factory.StaticCarFactory" factory-method="getCar"><constructor-arg value="1"></constructor-arg>
</bean>

factory-method 指向静态⽅法

constructor-arg 的 value 属性是调⽤静态⽅法传⼊的参数

实例⼯⼚⽅法

1、创建实例⼯⼚类、⼯⼚⽅法

public class InstanceCarFactory {private Map<Integer, Car> carMap;public InstanceCarFactory(){carMap = new HashMap<>();carMap.put(1,new Car(1,"奥迪"));carMap.put(2,new Car(2,"奥拓"));}public Car getCar(Integer num){return carMap.get(num);}
}

2、spring.xml

<!-- 实例⼯⼚ -->
<bean id="instanceCarFactory" class="com.southwind.factory.InstanceCarFactory"></bean>
<!-- 通过实例⼯⼚获取Car -->
<bean id="car2" factory-bean="instanceCarFactory" factory-method="getCar" ><constructor-arg value="2"></constructor-arg>
</bean>

区别:

静态⼯⼚⽅法创建 Car 对象,不需要实例化⼯⼚对象,因为静态⼯⼚的静态⽅法,不需要创建对象即可

调⽤,spring.xml 中只需要配置⼀个 bean ,即最终的结果 Car 即可。

实例⼯⼚⽅法创建 Car 对象,需要实例化⼯⼚对象,因为 getCar ⽅法是⾮静态的,就必须通过实例化

对象才能调⽤,所以就必须要创建⼯⼚对象,spring.xml 中需要配置两个 bean,⼀个是⼯⼚ bean,⼀

个是 Car bean。

spring.xml 中 class + factory-method 的形式是直接调⽤类中的⼯⼚⽅法

spring.xml 中 factory-bean + factory-method 的形式则是调⽤⼯⼚ bean 中的⼯⼚⽅法,就必须先创

建⼯⼚ bean。

5.Spring IoC ⾃动装载 autowire

⾃动装载是 Spring 提供的⼀种更加简便的⽅式来完成 DI,不需要⼿动配置 property,IoC 容器会⾃动

选择 bean 完成注⼊。

⾃动装载有两种⽅式:

  • byName,通过属性名完成⾃动装载。
  • byType,通过属性对应的数据类型完成⾃动装载。

byName 的操作如下所示。

1、创建 Person 实体类。

@Data
public class Person {private Integer Id;private String name;private Car car;
}

2、在 spring.xml 中配置 Car 和 Person 对应的 bean,并且通过⾃动装载完成依赖注⼊。

<bean id="person" class="com.southwind.entity.Person" autowire="byName"><property name="id" value="1"></property><property name="name" value="张三"></property>
</bean>
<bean id="car" class="com.southwind.entity.Car"><constructor-arg name="num" value="1"></constructor-arg><constructor-arg name="brand" value="奥迪"></constructor-arg>
</bean>

6.Spring IoC 基于注解的开发(重点)

Spring IoC 的作⽤是帮助开发者创建项⽬中所需要的 bean,同时完成 bean 之间的依赖注⼊关系,

DI。

实现该功能有两种⽅式:

  • 基于 XML 配置。
  • 基于注解。

基于注解有两步操作,缺⼀不可:

1、配置⾃动扫包。

2、添加注解。

<context:component-scan base-package="com.southwind.entity">
</context:component-scan>
@Data
@Component
public class Repository {private DataSource dataSource;
}

DI

@Data
@Component
public class DataSource {private String user;private String password;private String url;private String driverName;
}

@Data
@Component(value = "myrepo")
public class Repository {@Autowiredprivate DataSource dataSource;
}

@Autowired 默认是通过 byType 进⾏注⼊的,如果要改为 byName,需要配置 @Qualifier 注解来完成

@Autowired
@Qualifier(value = "ds")
private DataSource dataSource;

表示将 IoC 中 id 为 ds 的 bean 注⼊到 repository 中。

实体类中普通的成员变量(String、包装类等)可以通过 @Value 注解进⾏赋值。


文章转载自:

http://hDVBFZcP.znqxt.cn
http://BiHCaulF.znqxt.cn
http://WD19UXAs.znqxt.cn
http://dP7hXnJx.znqxt.cn
http://vkqh75xn.znqxt.cn
http://UjWSMqrs.znqxt.cn
http://kjBLLQri.znqxt.cn
http://5AnkPYVF.znqxt.cn
http://05hDwJXD.znqxt.cn
http://33KxY7eM.znqxt.cn
http://edHBcG1w.znqxt.cn
http://MrAehCG3.znqxt.cn
http://nGYNktbw.znqxt.cn
http://iGC6wpMb.znqxt.cn
http://JnxbEKjc.znqxt.cn
http://rDSvFdS7.znqxt.cn
http://doplQMrX.znqxt.cn
http://kNYNsJZq.znqxt.cn
http://taqNmCHf.znqxt.cn
http://Yuw56QTT.znqxt.cn
http://PvyxVUdh.znqxt.cn
http://FqsjaZ8c.znqxt.cn
http://14QC56fD.znqxt.cn
http://GoV71d20.znqxt.cn
http://BG6Am0pI.znqxt.cn
http://8lfHV43X.znqxt.cn
http://fbBSkYtQ.znqxt.cn
http://SGAvVmCG.znqxt.cn
http://81qI3vAk.znqxt.cn
http://JXRJf5F2.znqxt.cn
http://www.dtcms.com/wzjs/659034.html

相关文章:

  • 软件网站开发公司学生制作网站建设 维护
  • seo建站公司中山制作网站的公司吗
  • 自己做网站流程网站seo描述
  • 南阳网站抖音搜索引擎推广
  • 太仓网站建设有限公司网站 没有备案 访问不了
  • 桐城市做网站如何查询网站哪个公司做的
  • 东营集团网站建设平面设计创意构图
  • 怎么查询网站的设计公司商城网站建设报价
  • ps网站切图教程北京商场招商信息
  • 咸阳市住房和城乡建设规划局网站百度 手机网站收录
  • 无锡网站搜索引擎优化网站建设的优势何江
  • 网站访问量什么意思软件开发需要哪些软件
  • h5 网站模板做外贸哪些网站可以发布产品
  • ppt成品免费下载的网站网站后台上传图片做难吗
  • 做导师一般去什么网站找素材图书类网站开发的背景
  • 像天猫网站怎么做珠宝网站开发
  • 百度收录排名好的网站建凡网站
  • 网站招生宣传怎么做经典logo设计案例分析
  • 网站开发流程三部分怎么做企业网站一级悬浮菜单
  • 演示动画制作免费网站建设网站询价对比表模板
  • 微营销 网站模板网站建设方案书含合同
  • 招聘网站建设人员企业网页设计论文
  • 义乌购网站做代销怎么样代理一款网页游戏需要多少钱
  • 厦门比较好的网站设计公司做调查挣钱的网站
  • 遂宁网站制作pc端好玩的大型网游
  • 天津网站建设市场传奇辅助网站怎么做
  • 南昌公司做网站汕头企业网站公司
  • 国际新闻最新消息今天2024年网站怎么自己优化
  • 在58同城做网站怎么样广州化妆品网站制作
  • 网站建设首选-云端高科不会编程能建网站