Spring 使用详解

简介: Spring 使用详解


引入spring及测试的依赖

        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.16</version>
    </dependency>
    <!-- org.springframework/spring-test -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.3.16</version>
        <scope>test</scope>
    </dependency>

    <!-- org.junit.jupiter/junit-jupiter-api -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.8.2</version>
        <scope>test</scope>
    </dependency>

    <!-- org.projectlombok/lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.22</version>
        <scope>provided</scope>
    </dependency>

在cn.chenxiejia.entity包下建一个Entity和Student类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Entity {

private Set<Student> set;
private Map<Integer, Student> map;
private List<Student> list;
private String[] s;

}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {

private int sid;
private String name;
public void init() {
    System.out.println("初始化");
}

public void close() {
    System.out.println("执行了销毁");
}

}
然后建一个spring的配置文件,spring的所有配置都在这里进行,当然,也可以使用注解的方式配置随后进行注解方式演示

配置文件applicationContext.xml

这里演示的各种类型属性的配置 配置思路是先进性扫包加载文件 再进行bean的配置 加载文件只加载一次即可

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xmlns:context="http://www.springframework.org/schema/context"
   xmlns="http://www.springframework.org/schema/beans"
   xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">

<!--    扫描包-->
<context:component-scan base-package="cn.chenxiejia.entity"/>
<!--    加载properties文件-->
<context:property-placeholder location="db.properties"/>

<!--创建实例 init-method 初始化执行 destroy-method 销毁时执行-->
<bean id="zhang" class="cn.chenxiejia.entity.Student" init-method="init" destroy-method="close">
    <constructor-arg index="0" value="1"/>
    <constructor-arg index="1" value="张同学"/>
</bean>
<bean id="li" class="cn.chenxiejia.entity.Student">
    <constructor-arg name="name" value="李同学"/>
    <constructor-arg name="sid" value="2"/>

</bean>
<bean id="wang" class="cn.chenxiejia.entity.Student">
    <constructor-arg value="3"/>
    <constructor-arg value="王同学"/>
</bean>
<!-- id name 不能相同 唯一识别-->
<bean id="entity" class="cn.chenxiejia.entity.Entity">
    <property name="set">
        <set>
            <!--ref 为链接对象-->
            <ref bean="zhang"/>
            <ref bean="li"/>
            <ref bean="wang"/>
        </set>
    </property>
    <property name="list">
        <list>
            <ref bean="wang"/>
            <ref bean="zhang"/>
        </list>
    </property>
    <property name="map">
        <map>
            <entry key="1" value-ref="li"/>
            <entry key="2" value-ref="wang"/>
        </map>
    </property>
    <property name="s">
        <array value-type="java.lang.String">
            <value>ss</value>
            <value>dd</value>
            <value>ff</value>
        </array>
    </property>
</bean>
<bean id="entity1" name="entity2" class="cn.chenxiejia.entity.Entity">
    <property name="set">
        <set>
            <ref bean="zhang"/>
            <ref bean="li"/>
            <ref bean="wang"/>
        </set>
    </property>
    <property name="list">
        <list>
            <ref bean="wang"/>
            <ref bean="zhang"/>
        </list>
    </property>
    <property name="map">
        <map>
            <entry key="1" value-ref="li"/>
            <entry key="2" value-ref="wang"/>
        </map>
    </property>
    <property name="s">
        <array value-type="java.lang.String">
            <value>11</value>
            <value>sq</value>
            <value>ee</value>
        </array>
    </property>
</bean>


测试类1演示

//加载xml配置
@SpringJUnitConfig(locations = {"classpath:applicationContext.xml"})
public class Springdemo {

//@Autowired为自动装配 写清楚类型及实例名,实例名必须唯一或者该类型只有一个实例
@Autowired
Student li;
@Autowired
ApplicationContext ac;
@Autowired
Entity entity1;
@Autowired
Entity entity;
@Autowired
Student zhang;

@Test
public void demo() {
    //getDeanDefinitionNames为bean实例化的名称列表
    for (String b : ac.getBeanDefinitionNames()) {
        System.out.println(b);
    }
    System.out.println(li);
}
//@Qualifier("") 将该对象属性赋值给另一个
@Test
public void demo1(@Qualifier("entity1") Entity e) {
    System.out.println(e.getList());
    System.out.println(e.getMap());
    System.out.println(e.getSet());
    for (String s : e.getS()) {
        System.out.println(s);
    }
}

测试类2演示

@SpringJUnitConfig(locations = "classpath:applicationContext.xml")
//@PropertySource("db.properties")
public class SpringDemo1 {

//#用于获取实例属性 $用于获取配置文件内容
@Value("#{'zhang,li1,wang,zhao,li'.split(',')}")
private List<String> list;
@Value("${db.url}")
private String url;
@Value("#{li.name}")
private String map;

@Test
public void demo1() {
    System.out.println(list);
    System.out.println(url);
    System.out.println(map);
}

}
接下来使用注解的形式演示

在刚才的两个实体类上加个注解@Service表示标记为被扫描类

image.png

 然后创建一个配置类相当于之前的applicationContext.xml

里面的@Bean相当于的配置

//标注为配置类
@Configuration
//扫描包
@ComponentScan("cn.chenxiejia.entity")
//加载配置文件
@PropertySource("classpath:db.properties")
public class AppConfig {

@Bean(name = "s1", initMethod = "init", destroyMethod = "close")
public Student Student(@Value("1") int id, @Value("张同学") String name) {
    return new Student(id, name);
}

@Bean(name = "s2", initMethod = "init", destroyMethod = "close")
public Student Student2(@Value("2") int id, @Value("王同学") String name) {
    return new Student(id, name);
}

@Bean("e1")
public Entity e1(@Value("#{{s1,s2}}") Set<Student> set, @Value("#{{3:s1,4:s2}}") Map<Integer, Student> map, @Value("#{{s1,s2}}") List<Student> list, @Value("#{'java,phton,js,c,html'.split(',')}") String[] s) {
    return new Entity(set, map, list, s);
}

@Bean("e2")
public Entity e2(@Value("#{{s1,s2}}") Set<Student> set, @Value("#{{3:s1,4:s2}}") Map<Integer, Student> map, @Value("#{{s1,s2}}") List<Student> list, @Value("#{'aa,bb,cc,dd,ee'.split(',')}") String[] s) {
    return new Entity(set, map, list, s);
}

}
然后简单测试一下

//加载配置类
@SpringJUnitConfig(AppConfig.class)
public class SpringDemo2 {

@Autowired
Student s2;
@Autowired
Entity e1;

@Test
public void demo1() {
    System.out.println(e1);
}

}

目录
相关文章
|
Java Apache 数据库
spring boot 2.0之使用spring boot
spring boot依赖 每一个spring boot的发型版本都包含了所依赖的版本,如果升级spring boot版本,其依赖也会同步更新升级。maven的用户可以通过继承spring-boot-starter-parent。
5579 0
|
6天前
|
SQL 监控 druid
springboot-druid数据源的配置方式及配置后台监控-自定义和导入stater(推荐-简单方便使用)两种方式配置druid数据源
这篇文章介绍了如何在Spring Boot项目中配置和监控Druid数据源,包括自定义配置和使用Spring Boot Starter两种方法。
|
2月前
|
缓存 Java Maven
Java本地高性能缓存实践问题之SpringBoot中引入Caffeine作为缓存库的问题如何解决
Java本地高性能缓存实践问题之SpringBoot中引入Caffeine作为缓存库的问题如何解决
|
3月前
|
Java 测试技术 数据库
Spring Boot中的项目属性配置
本节课主要讲解了 Spring Boot 中如何在业务代码中读取相关配置,包括单一配置和多个配置项,在微服务中,这种情况非常常见,往往会有很多其他微服务需要调用,所以封装一个配置类来接收这些配置是个很好的处理方式。除此之外,例如数据库相关的连接参数等等,也可以放到一个配置类中,其他遇到类似的场景,都可以这么处理。最后介绍了开发环境和生产环境配置的快速切换方式,省去了项目部署时,诸多配置信息的修改。
|
3月前
|
Java 应用服务中间件 开发者
Java面试题:解释Spring Boot的优势及其自动配置原理
Java面试题:解释Spring Boot的优势及其自动配置原理
101 0
|
6天前
|
XML Java 关系型数据库
springboot 集成 mybatis-plus 代码生成器
本文介绍了如何在Spring Boot项目中集成MyBatis-Plus代码生成器,包括导入相关依赖坐标、配置快速代码生成器以及自定义代码生成器模板的步骤和代码示例,旨在提高开发效率,快速生成Entity、Mapper、Mapper XML、Service、Controller等代码。
springboot 集成 mybatis-plus 代码生成器
|
14天前
|
Java 应用服务中间件 开发者
深入探索并实践Spring Boot框架
深入探索并实践Spring Boot框架
25 2
|
6天前
|
前端开发 Java Spring
springboot自定义拦截器的简单使用和一个小例子
本文介绍了如何在Spring Boot中创建和使用自定义拦截器,通过一个登录验证的示例,演示了拦截器在MVC流程中的preHandle、postHandle和afterCompletion三个环节的作用,并说明了如何在Spring Boot配置类中注册拦截器。
|
7天前
|
Java 网络架构
springboot配合thymeleaf,调用接口不跳转页面只显示文本
springboot配合thymeleaf,调用接口不跳转页面只显示文本
34 0
|
2月前
|
XML Java 数据库连接
Spring Boot集成MyBatis
主要系统的讲解了 Spring Boot 集成 MyBatis 的过程,分为基于 xml 形式和基于注解的形式来讲解,通过实际配置手把手讲解了 Spring Boot 中 MyBatis 的使用方式,并针对注解方式,讲解了常见的问题已经解决方式,有很强的实战意义。在实际项目中,建议根据实际情况来确定使用哪种方式,一般 xml 和注解都在用。
下一篇
无影云桌面