重构Spring的IOC配置文件中的XML配置
最编程
2024-01-05 18:57:26
...
Spring Ioc 使用XML配置
1、首先到引入依赖包
<!--引入配置-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
2、创建Spring配置文件,一般取名这三个(任意 无约定) spring-context.xml、applicationContext.xml、beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
</beans>
3、创建一个使用ioc创建一个对象 并且帮他设置好初始值
3.1首先创建好Class 这边有两个类一个是学生类 和 地址类
3.1.1学生类 Student.java 并设置他的get和set 方法要不要xml配置不了属性
//Student.java
package com.ared.entity;
import java.util.List;
import java.util.Properties;
import java.util.Set;
public class Student {
private int id;
private String name;
private List<String> phone;
private Set<String> oldName;
private Properties info;
private Address address;
public Student(){
System.out.println("----Constructor----");
}
public void init(){
System.out.println("-----init------");
}
public void destroy(){
System.out.println("-----destroy-----");
}
public Student(int id, String name, List<String> phone, Set<String> oldName, Properties info, Address address) {
this.id = id;
this.name = name;
this.phone = phone;
this.oldName = oldName;
this.info = info;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getPhone() {
return phone;
}
public void setPhone(List<String> phone) {
this.phone = phone;
}
public Set<String> getOldName() {
return oldName;
}
public void setOldName(Set<String> oldName) {
this.oldName = oldName;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", phone=" + phone +
", oldName=" + oldName +
", info=" + info +
", address=" + address +
'}';
}
}
3.1.2地址类 Address.java
//Address.java
package com.ared.entity;
public class Address {
String homeAddress;
String officeAddress;
public String getHomeAddress() {
return homeAddress;
}
public void setHomeAddress(String homeAddress) {
this.homeAddress = homeAddress;
}
public String getOfficeAddress() {
return officeAddress;
}
public void setOfficeAddress(String officeAddress) {
this.officeAddress = officeAddress;
}
@Override
public String toString() {
return "Address{" +
"homeAddress='" + homeAddress + '\'' +
", officeAddress='" + officeAddress + '\'' +
'}';
}
}
3.1.3配置Spring配置文件 applicationContext.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="address" class="com.ared.entity.Address">
<property name="homeAddress" value="青山湖"/>
<property name="officeAddress" value="高新"/>
</bean>
<bean id="student" class="com.ared.entity.Student">
<property name="id" value="10001"/>
<property name="name" value="码小农"/>
<!-- 添加一个list参数 -->
<property name="phone">
<list>
<value>13711112222</value>
<value>13711112223</value>
</list>
</property>
<!-- 添加一个set参数 -->
<property name="oldName">
<set>
<value>小明</value>
<value>小红</value>
</set>
</property>
<!--添加一个Properties参数-->
<property name="info">
<props>
<prop key="username">admin</prop>
<prop key="password">123456</prop>
</props>
</property>
<!--如果属性有对象的话可以先创建bean 然后使用ref 应用id 得到值-->
<property name="address" ref="address"/>
</bean>
</beans>
3.1.4写一个测试类 IocTest.java
package com.ared.test;
import com.ared.entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class IocTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext.xml");
Student student = (Student) context.getBean("student");
System.out.println(student);
}
}
3.1.5使用 在Bean创建时会可以指定一个 初始方法 和 销毁方法 只需要修改Bean 标签上加一个 init-method 和 destroy-method
<bean id="student" class="com.ared.entity.Student" init-method="init" destroy-method="destroy">
3.1.6 自动装配 如果你的Bean中需要注入其他对象的话 你可以使用 autowire 这个标签注入 他有两种注入方式
1、一种是根据id名字注入
<bean id="student" class="com.ared.entity.Student" autowire="byName">
2、二种是更具类型注入
<bean id="student" class="com.ared.entity.Student" autowire="byType">
3.1.7 单例和多例 scope
1、单例模式:当xml加载到spring中 只要是 scope="singleton" 然后 默认都是单例模式 spring加载的时候就会把这些对象创建好
<bean id="student" class="com.ared.entity.Student" scope="singleton">
2、多例模式: 当每次getBean的时候才创建一个新的对象 初始话的时候不会创建
<bean id="student" class="com.ared.entity.Student" scope="prototype">
上一篇: 20年后,回顾何家劲版《马永贞》中十位演员的现状:白癞痢已辞世
下一篇: RabbitMQ入门教程
推荐阅读
-
分析 Spring 源代码中的配置文件解析、Bean 创建和生命周期
-
Spring 中的 @Configuration 配置类
-
将 spring boot 集成到 knife4j 配置中的页面显示空白
-
找不到 mybatis/mapper/EmployeeMapper.xml 资源-在配置文件路径正确的情况下解决问题
-
Springboot 中的常用配置文件
-
jenkins 插件文件夹中的 jenkins 配置文件详情
-
SSM Java Web 项目因 spring-mvc.xml 配置不正确而带来的一系列问题
-
Java 类加载器的作用 - 简介:类加载器是 Java™ 中一个非常重要的概念。类加载器负责将 Java 类的字节码加载到 Java 虚拟机中。本文首先详细介绍了 Java 类加载器的基本概念,包括代理模型、加载类的具体过程和线程上下文类加载器等。然后介绍了如何开发自己的类加载器,最后介绍了类加载器在 Web 容器和 OSGi™ 中的应用。 类加载器是 Java 语言的一项创新,也是 Java 语言广受欢迎的重要原因之一。它允许将 Java 类动态加载到 Java 虚拟机中并执行。类加载器从 JDK 1.0 开始出现,最初是为了满足 Java Applets 的需求而开发的,Java Applets 需要从远程位置下载 Java 类文件并在浏览器中执行。现在,类加载器已广泛应用于网络容器和 OSGi。一般来说,Java 应用程序的开发人员不需要直接与类加载器交互;Java 虚拟机的默认行为足以应对大多数情况。但是,如果遇到需要与类加载器交互的情况,而您又不太了解类加载器的机制,就很容易花费大量时间调试异常,如 ClassNotFoundException 和 NoClassDefFoundError。本文将详细介绍 Java 的类加载器,帮助读者深入理解 Java 语言中的这一重要概念。下面先介绍一些基本概念。 类加载器的基本概念 顾名思义,类加载器用于将 Java 类加载到 Java 虚拟机中。一般来说,Java 虚拟机以如下方式使用 Java 类:Java 源程序(.java 文件)经 Java 编译器编译后转换为 Java 字节代码(.class 文件)。类加载器负责读取 Java 字节代码并将其转换为 java.lang 实例。每个实例都用来表示一个 Java 类。通过该实例的 newInstance 方法创建该类的对象。实际情况可能更加复杂,例如,Java 字节代码可能是由工具动态生成或通过网络下载的。 基本上,所有类加载器都是 java.lang.ClassLoader 类的实例。下面将详细介绍这个 Java 类。 java.lang.ClassLoader 类简介 java.lang.ClassLoader 类的基本职责是根据给定类的名称为其查找或生成相应的字节码,然后根据这些字节码定义一个 Java 类,即 java.lang.Class 类的实例。除此之外,ClassLoader 还负责加载 Java 应用程序所需的资源,如图像文件和配置文件。不过,本文只讨论它加载类的功能。为了履行加载类的职责,ClassLoader 提供了许多方法,其中比较重要的方法如表 1 所示。下文将详细介绍这些方法。 表 1.与加载类相关的 ClassLoader 方法
-
Spring 负载属性配置文件的详细使用方法
-
标题:一文搞定Redis面试,附Redis面试大纲+常见Redis面试题-一、基础篇 快速上手 ①. 什么是redis ②. 为什么使用redis ③. 安装 ④. 基本使用(常见数据结构的命令) Java操作redis ①. Jedis ②. SpringBoot 启动redis的方式 ①. 配置文件 ②. 生产环境启动方案 二、进阶篇 redis实现session共享 redis缓存的使用 ①. 注解式 ②. Spring Cache 数据库和缓存双写一致性问题——穿透 redis实现附近的人 redis实现计数器 redis事务 redis分布式锁的使用 redis集群 redis实现延时队列 redis实现限流 redis实现布隆过滤器 发布订阅 redis优化 三、原理篇 redis单线程为什么性能好 数据类型的底层实现 持久化机制 过期策略 内存淘汰 redis优化 哨兵模