Spring相关配置及xml配置总结

Spring相关配置比较简单:直接导入maven包就能使用框架

步骤1 添加依赖

	<dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
    </dependencies>

步骤2 编写实体类

public class User {
    private Integer id;
    private String name;
    private String password;
    private String address;

    public User() {
    }

    public User(Integer id, String name, String password, String address) {
        this.id = id;
        this.name = name;
        this.password = password;
        this.address = address;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

步骤3 配置ioc.xml文件

我们使用xml文件配置属性值,也可以使用注解方式将实体类注册到容器中,主要是在xml文件中开启注解扫描,然后在实体类上添加@Repository @Component @Controller @Service 来实现对象注入方式

<?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.xsd">
	<!--开启注解,注解扫描文件夹位置-->
	<!--<context:component-scan base-package="com.jyk"></context:component-scan>-->
	<!--将实体类注册到容器中,当需要的时候容器自动提供实体类,控制反转(思想) :依赖注入,利用反射机制;因此反射是框架的核心思想-->
    <bean id="user" class="com.jyk.bean.User">
        <property name="id" value="1"></property>
        <property name="name" value="root"></property>
        <property name="password" value="root"></property>
        <property name="address" value="北京市"></property>
    </bean>
</beans>

步骤4 测试类

public class Testdemo {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ioc.xml");
        User user = applicationContext.getBean("user", User.class);
        System.out.println(user);
    }
}

测试结果

在这里插入图片描述
此过程完成SpringIOC容器配置功能
可以在ioc.xml中配置更多的属性值

bean标签知识点总结:

(1)id及class
	<!--id:唯一标识符,自定义,在IOC测试中getbean()方法中,填写的标识符名称及其 实体类.class -->
	<!--class:类的完全限定名,路径及其名称-->
	<bean id="user" class="com.jyk.entity.User">
	<!--name:为实体类属性 value:属性的值,相当于我们写实体类的.setName()方法 其他属性标签一样的方式-->
	<!--scope:标签为此类时单例还是多例模式,单例的话,不能new对象,过程中使用的都是同一个对象,可以进行相应的测试,单例时容器创建的时候此对象就已经创建,而多例时什么时候需要对象什么时候进行创建,创建的都是新的对象 scope="singleton" 单例scope="prototype"多例-->
	<property name="name" value="root" scope="singleton"><property>
	</bean>
	
	

(2)属性scope标签测试

(1)单例模式测试结果
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
(2)多例模式测试结果
在这里插入图片描述
在这里插入图片描述

(3)属性init-method及destroy-method标签
init-method=""<!--初始化调用实体类的哪个方法-->
destroy-method=""<!--销毁对象调用实体类的哪个方法-->
(4)属性parent标签

父类对象是哪个类

	<bean id="paruser" class="com.jyk.bean.ParUser">
        <property name="age" value="17"></property>
    </bean>
    <bean id="user" class="com.jyk.bean.User" parent="paruser">
        <property name="id" value="1" ></property>
        <property name="name" value="root"></property>
        <property name="password" value="root"></property>
        <property name="address" value="北京市"></property>
    </bean>
(5)属性abstract标签

此类是否是抽象类无法进行实例化

<bean id="user" class="com.jyk.bean.User" abstract="true">
</bean>
(6)factory-bean及 factory-method

facotry-method:调用静态工厂方法

public class StaticFactory {
    private static User getInstance(String name){
        User user = new User();
        user.setId(12);
        user.setName(name);
        user.setAddress("上海");
        return user;
    }
}
	<bean id="factory" class="com.jyk.bean.StaticFactory" factory-method="getInstance">
        <constructor-arg value="zhangsan"></constructor-arg>
    </bean>
public class Testdemo {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ioc.xml");
        User user = applicationContext.getBean("factory", User.class);
        System.out.println(user);
    }
}

factrory-bean具体工厂的实例:
实例工厂

public class InstanceFactory {
    private User getInstance(String name){
        User user = new User();
        user.setId(12);
        user.setName(name);
        user.setAddress("上海");
        return user;
    }
}

xml配置

<bean id="instancefactory" class="com.jyk.bean.InstanceFactory"></bean>
    <bean id="userinstance" class="com.jyk.bean.User" factory-bean="instancefactory" factory-method="getInstance">
        <constructor-arg value="lisi"></constructor-arg>
    </bean>

测试结果:

public class Testdemo {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ioc.xml");
        User user = applicationContext.getBean("userinstance", User.class);
        System.out.println(user);
    }
}

在这里插入图片描述

(7)autowire装配类型

自动装配类型包括
no:不装配
byName:按照id来自动装配
byType:按照类型自动装配,多个类型会报错
default:不装配
constructor:按照构造器及逆行装配

测试:
no/default:效果一样
在这里插入图片描述
byName:按照名字注入
在这里插入图片描述
byType:按照类型自动注入,但是有多个类型的时候会报错
在这里插入图片描述
在这里插入图片描述
constructor:构造器进行注入,将add属性值在实体类方法生成构造方法后,然后配置xml文件
在这里插入图片描述

加载properties文件

<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>

bean标签加载druid数据库:

maven依赖:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.23</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.20</version>
</dependency>
   <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
        <!--数据库端口,时区需要注意,mysql版本不同,需要添加额外的参数,可以将value信息放到.propertes文件中然后通过加载属性文件,在此标签中使用-->
        <property name="url" value="jdbc:mysql://localhost:3307/demo?serverTimezone=UTC"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>

测试结果:
在这里插入图片描述

总结

xml配置方式比较繁琐,但是相对比较全面实现功能更加强大,而注解实现方式更加简单,在此过程中遇到的问题有
(1)数据库连接池,连接不上,driverClassName及url书写注意,尤其url需要根据数据库版本设置时区为UTC
(2)自动装配,按照构造器进行装配,需要在实体类定义装配类的构造方法,否则为null
(3)单例和多例创建时间,单例是容器初始化创建,自始至终都是一个相同的对象,多例是使用实例化对象的时候创建,而且重新创建一个对象

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring XML配置是一种传统的配置方式,用于配置Spring框架中的Bean和它们之间的依赖关系。通过XML配置文件,我们可以定义和组织应用程序中的各个组件,包括Bean的定义、Bean之间的关系、AOP配置、数据源配置等。 在Spring XML配置中,通常会使用以下几个核心元素进行配置: 1. `<beans>`:作为根元素,用于定义整个配置文件的作用域。 2. `<bean>`:用于定义一个Bean,包括Bean的ID、类名、作用域等属性。 3. `<property>`:用于设置Bean的属性值,可以通过`name`属性指定属性名,或直接使用内联方式设置属性值。 4. `<constructor-arg>`:用于设置Bean的构造函数参数,可以通过`index`或`type`属性指定参数位置或类型。 5. `<import>`:用于导入其他XML配置文件。 6. `<alias>`:用于定义Bean的别名。 7. `<bean>`和`<property>`等元素还可以使用一些命名空间来简化配置,如`<context:component-scan>`用于自动扫描组件等。 除了以上核心元素外,Spring XML配置还支持一些其他的元素和属性,如AOP相关配置、事务管理配置等。需要根据具体的需求和场景来灵活使用和配置。 需要注意的是,随着Spring框架的发展,基于Java配置的方式(如使用注解和Java类进行配置)逐渐取代了XML配置方式,因为它更加简洁、类型安全,并且易于维护。但对于一些遗留的项目和特殊的需求,XML配置仍然是一种常用的配置方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值