当前位置: 首页 > 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://www.dtcms.com/wzjs/472197.html

相关文章:

  • 一个服务器可以放多少网站专业网页设计和网站制作公司
  • 汕头政府网站建设dw友情链接怎么设置
  • 电子商务网站建设的答案西安seo站内优化
  • wordpress 标签 图片 altseo网站关键词优化费用
  • 网站推广哪种方法最seo广州工作好吗
  • 国内免费的建网站平台东莞做网站最好的是哪家
  • 沧州市有哪些网络公司网站优化推广公司
  • 吉林电商网站建设公司哪家好seo基础知识培训
  • 工程设计东莞网站建设技术支持一站式网络推广服务
  • 公司申请注册流程百度seo优化排名软件
  • 360任意看地图网站在线seo工具
  • 邢台专业网站建设源码百度竞价排名什么意思
  • 可靠的铁岭做网站公司码迷seo
  • 专门做图表的网站中国站长之家网站
  • 服装网站建设公司好吗seo运营工作内容
  • 百兆独享 做资源网站培训心得体会怎么写
  • 网站建设 博采企业培训内容
  • 吴忠市利通区建设局网站最知名的网站推广公司
  • 10m光纤做网站学技术的培训学校
  • java可以做网站开发吗做外贸网站哪家公司好
  • 如何用api做网站网络营销的概念及特点
  • 生态农庄网站建设友情链接出售平台
  • 麻豆精产三产区区别seo公司排行
  • 山东住房和城乡建设部网站seo兼职工资一般多少
  • 做个动态网站要多钱优化设计电子课本下载
  • 福州网站制作公司营销怎么做好网络推广销售
  • 公司网站建站模板seo sem关键词优化
  • 做美食介绍的网站徐州seo推广优化
  • 开源镜像网站开发千万不要学网络营销
  • 北京web网站开发培训班如何自己做一个软件