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

spring(二)

一、为对象类型属性赋值

方式一:(引用外部bean)

        1.创建班级类Clazz

package com.spring.beanpublic class Clazz {private Integer clazzId;private String clazzName;public Integer getClazzId() {return clazzId;}public void setClazzId(Integer clazzId) {this.clazzId = clazzId;}public String getClazzName() {return clazzName;}public void setClazzName(String clazzName) {this.clazzName = clazzName;}@Overridepublic String toString() {return "Clazz{" +"clazzId=" + clazzId +", clazzName='" + clazzName + '\'' +'}';}public Clazz() {}public Clazz(Integer clazzId, String clazzName) {this.clazzId = clazzId;this.clazzName = clazzName;}
}

        2.修改Student类

//在Student类中添加以下代码:
private Clazz clazz;public Clazz getClazz() {return clazz;
}public void setClazz(Clazz clazz) {this.clazz = clazz;
}

        3.引用外部bean:

//配置Clazz类型的bean
<bean id="clazzOne" class="com.spring.bean.Clazz"><property name="clazzId" value="001"></property><property name="clazzName" value="一般班"></property>
</bean>//为Student中的clazz属性赋值
<bean id="studentFour" class="com.spring.bean.Student"><property name="id" value="004"></property><property name="name" value="李寒衣"></property><property name="age" value="26"></property><property name="sex" value="女"></property><!-- ref属性:引用IOC容器中某个bean的id,将所对应的bean为属性赋值 --><property name="clazz" ref="clazzOne"></property>
</bean>

方式二:(内部bean)

<bean id="studentFour" class="com.spring.bean.Student"><property name="id" value="1004"></property><property name="name" value="赵六"></property><property name="age" value="26"></property><property name="sex" value="女"></property><property name="clazz"><!-- 在一个bean中再声明一个bean就是内部bean --><!-- 内部bean只能用于给属性赋值,不能在外部通过IOC容器获取,因此可以省略id属性 --><bean id="clazzInner" class="com.spring.bean.Clazz"><property name="clazzId" value="002"></property><property name="clazzName" value="无心班"></property></bean></property>
</bean>

 方式三:(级联属性赋值)

<bean id="studentFour" class="com.spring.bean.Student"><property name="id" value="1004"></property><property name="name" value="赵六"></property><property name="age" value="26"></property><property name="sex" value="女"></property><property name="clazz" ref="clazzOne"></property><property name="clazz.clazzId" value="003"></property><property name="clazz.clazzName" value="天下第一班"></property>
</bean>

二、为数组类型属性赋值

1.在Student类中添加以下代码:

private String[] hobbies;public String[] getHobbies() {return hobbies;
}public void setHobbies(String[] hobbies) {this.hobbies = hobbies;
}

2.配置bean:

<bean id="studentFour" class="com.spring.bean.Student"><property name="id" value="1004"></property><property name="name" value="赵六"></property><property name="age" value="26"></property><property name="sex" value="女"></property><!-- ref属性:引用IOC容器中某个bean的id,将所对应的bean为属性赋值 --><property name="clazz" ref="clazzOne"></property><property name="hobbies"><array><value>抽烟</value><value>喝酒</value><value>烫头</value></array></property>
</bean>

三、为集合类型属性赋值

1.在clazz中添加以下代码:

private List<Student> students;public List<Student> getStudents() {return students;
}public void setStudents(List<Student> students) {this.students = students;
}

2.配置bean

<bean id="clazzTwo" class="com.spring.bean.Clazz"><property name="clazzId" value="004"></property><property name="clazzName" value="Javaee"></property><property name="students"><list><ref bean="studentOne"></ref><ref bean="studentTwo"></ref><ref bean="studentThree"></ref></list></property>
</bean>
<!--若为Set集合类型属性赋值,只需要将其中的list标签改为set标签即可-->

四、为Map集合类型属性赋值

1.创建教师类Teacher:

package com.spring.bean;
public class Teacher {private Integer teacherId;private String teacherName;public Integer getTeacherId() {return teacherId;}public void setTeacherId(Integer teacherId) {this.teacherId = teacherId;}public String getTeacherName() {return teacherName;}public void setTeacherName(String teacherName) {this.teacherName = teacherName;}public Teacher(Integer teacherId, String teacherName) {this.teacherId = teacherId;this.teacherName = teacherName;}public Teacher() {}@Overridepublic String toString() {return "Teacher{" +"teacherId=" + teacherId +", teacherName='" + teacherName + '\'' +'}';}
}

2.在Student类中添加以下代码:

private Map<String, Teacher> teacherMap;public Map<String, Teacher> getTeacherMap() {return teacherMap;
}public void setTeacherMap(Map<String, Teacher> teacherMap) {this.teacherMap = teacherMap;
}

3.配置bean:

<bean id="teacherOne" class="com.spring.bean.Teacher"><property name="teacherId" value="10010"></property><property name="teacherName" value="沈眉庄"></property>
</bean><bean id="teacherTwo" class="com.atguigu.spring6.bean.Teacher"><property name="teacherId" value="10086"></property><property name="teacherName" value="陆沉"></property>
</bean><bean id="studentFour" class="com.spring.bean.Student"><property name="id" value="004"></property><property name="name" value="李寒衣"></property><property name="age" value="26"></property><property name="sex" value="女"></property><!-- ref属性:引用IOC容器中某个bean的id,将所对应的bean为属性赋值 --><property name="clazz" ref="clazzOne"></property><property name="hobbies"><array><value>抽烟</value><value>喝酒</value><value>烫头</value></array></property><property name="teacherMap"><map><entry><key><value>10010</value></key><ref bean="teacherOne"></ref></entry><entry><key><value>10086</value></key><ref bean="teacherTwo"></ref></entry></map></property>
</bean>

4.引用集合类型的bean

<!--list集合类型的bean-->
<util:list id="students"><ref bean="studentOne"></ref><ref bean="studentTwo"></ref><ref bean="studentThree"></ref>
</util:list>
<!--map集合类型的bean-->
<util:map id="teacherMap"><entry><key><value>10010</value></key><ref bean="teacherOne"></ref></entry><entry><key><value>10086</value></key><ref bean="teacherTwo"></ref></entry>
</util:map>
<bean id="clazzTwo" class="com.spring.bean.Clazz"><property name="clazzId" value="004"></property><property name="clazzName" value="Javaee"></property><property name="students" ref="students"></property>
</bean>
<bean id="studentFour" class="com.pring.bean.Student"><property name="id" value="004"></property><property name="name" value="李寒衣"></property><property name="age" value="26"></property><property name="sex" value="女"></property><!-- ref属性:引用IOC容器中某个bean的id,将所对应的bean为属性赋值 --><property name="clazz" ref="clazzOne"></property><property name="hobbies"><array><value>抽烟</value><value>喝酒</value><value>烫头</value></array></property><property name="teacherMap" ref="teacherMap"></property>
</bean>

使用util:list、util:map标签必须引入相应的命名空间

<?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:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">

五、p命名空间

引入p命名空间

<?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:util="http://www.springframework.org/schema/util"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">

引入p命名空间后,可以通过以下方式为bean的各个属性赋值

<bean id="studentSix" class="com.spring.bean.Student"p:id="006" p:name="小明" p:clazz-ref="clazzOne" p:teacherMap-ref="teacherMap"></bean>

六、引入外部属性文件

1.加入依赖

<!-- MySQL驱动 -->
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.30</version>
</dependency><!-- 数据源 -->
<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.15</version>
</dependency>

2.创建外部属性文件

<!--properties-->
jdbc.user=root
jdbc.password=root
jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
jdbc.driver=com.mysql.cj.jdbc.Driver

3.引入属性文件

<!--xml-->
<!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>

4.配置bean

<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="url" value="${jdbc.url}"/><property name="driverClassName" value="${jdbc.driver}"/><property name="username" value="${jdbc.user}"/><property name="password" value="${jdbc.password}"/>
</bean>

5.测试

@Test
public void testDataSource() throws SQLException {ApplicationContext ac = new ClassPathXmlApplicationContext("spring-datasource.xml");DataSource dataSource = ac.getBean(DataSource.class);Connection connection = dataSource.getConnection();System.out.println(connection);
}

七、bean的作用域

取值含义创建对象的时机
singleton(默认)在IOC容器中,这个bean的对象始终为单实例IOC容器初始化时
prototype这个bean在IOC容器中有多个实例获取bean

如果是在WebApplicationContext环境下还会有另外几个作用域(但不常用):

取值含义
request在一个请求范围内有效
session在一个会话范围内有效

1、创建类User

package com.spring.bean;
public class User {private Integer id;private String username;private String password;private Integer age;public User() {}public User(Integer id, String username, String password, Integer age) {this.id = id;this.username = username;this.password = password;this.age = age;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "User{" +"id=" + id +", username='" + username + '\'' +", password='" + password + '\'' +", age=" + age +'}';}
}

2.配置bean

<!-- scope属性:取值singleton(默认值),bean在IOC容器中只有一个实例,IOC容器初始化时创建对象 -->
<!-- scope属性:取值prototype,bean在IOC容器中可以有多个实例,getBean()时创建对象 -->
<bean class="com.spring.bean.User" scope="prototype"></bean>

3.测试

@Test
public void testBeanScope(){ApplicationContext ac = new ClassPathXmlApplicationContext("spring-scope.xml");User user1 = ac.getBean(User.class);User user2 = ac.getBean(User.class);System.out.println(user1==user2);
}

八、bean生命周期

1.具体生命周期过程

  • bean对象创建
  • 给bean对象设置属性
  • bean的后置处理器(初始化之前)
  • bean对象初始化(需要在配置bean时指定初始化方法)
  • bean的后置处理器(bean的后置处理器会在生命周期的初始化前后添加额外的操作,需要实现BeanPostProcessor接口,且配置到IOC容器中,需要注意的是,bean后置处理器不是单独针对某一个bean生效,而是针对IOC容器中所有bean都会执行)
  • bean对象就绪可以使用
  • bean对象销毁
  • IOC容器关闭

九、FactoryBean

        FactoryBean是Spring提供的一种整合第三方框架的常用机制。和普通bean不同,配置一个FactoryBean类型的bean,在获取bean的时候得到的并不是class属性中配置的这个类的对象,而是getObject()方法的返回值。通过这种机制,Spring可以帮我们把复杂组件创建的详细过程和繁琐细节都屏蔽起来,只把最简洁的使用界面展示给我们。

        将来我们整合Mybatis时,Spring就是通过FactoryBean机制来帮我们创建SqlSessionFactory对象的。

1.创建类UserFactoryBean

package com.spring.bean;
public class UserFactoryBean implements FactoryBean<User> {@Overridepublic User getObject() throws Exception {return new User();}@Overridepublic Class<?> getObjectType() {return User.class;}
}

2.配置bean

<bean id="user" class="com.spring.bean.UserFactoryBean"></bean>

3.测试

@Test
public void testUserFactoryBean(){//获取IOC容器ApplicationContext ac = new ClassPathXmlApplicationContext("spring-factorybean.xml");User user = (User) ac.getBean("user");System.out.println(user);
}
http://www.lryc.cn/news/404621.html

相关文章:

  • MAC 数据恢复软件: STELLAR Data Recovery For MAC V. 12.1 更多增强功能
  • 初识godot游戏引擎并安装
  • Windows配置Qt+VLC
  • 本地部署 mistralai/Mistral-Nemo-Instruct-2407
  • 2月科研——arcgis计算植被差异
  • 深入理解Android中的缓存与文件存储目录
  • Linux_生产消费者模型
  • 【Vue】`v-if` 指令详解:条件渲染的高效实现
  • junit mockito Base基类
  • 朋友圈运营分享干货2
  • linux中创建一个名为“thread1“,堆栈大小为1024,优先级为2的线程
  • 架构以及架构中的组件
  • Docker启动PostgreSql并设置时间与主机同步
  • 提升无线网络安全:用Python脚本发现并修复WiFi安全问题
  • #三元运算符(python/java/c)
  • 探索Python自然语言处理的新篇章:jionlp库介绍
  • Deepin系统,中盛科技温湿度模块读温度纯c程序(备份)
  • 文件包含漏洞: 函数,实例[pikachu_file_inclusion_local]
  • 学习计划2024下半年
  • RabbitMQ的学习和模拟实现|sqlite轻量级数据库的介绍和简单使用
  • AI批量剪辑,批量发布大模型矩阵系统搭建开发
  • SpringMVC源码深度解析(中)
  • Mojo模型动态批处理:智能预测的终极武器
  • 人、智能、机器人……
  • SpringCloud------Sentinel(微服务保护)
  • 【无标题】Elasticsearch for windows
  • Yolo-World网络模型结构及原理分析(一)——YOLO检测器
  • WEB前端06-BOM对象
  • Android11 framework 禁止三方应用开机自启动
  • Java | Leetcode Java题解之第263题丑数