IT评测·应用市场-qidao123.com

标题: Spring笔记 [打印本页]

作者: 千千梦丶琪    时间: 2023-9-17 13:43
标题: Spring笔记
1.ioc

1 pom导包spring-mvc
2 创建资源文件xml、pojo对象()
3 资源文件中配置bean,对pojo对象属性
4 测试中直接getBean获取。
1.1 一些不重要的

取别名:在资源文件中取别名,一种是直接在bean标签中用name,另一种是单独设置标签alias
合并配置:在资源配置文件中标签import可以导入合并
1.2 依赖(属性)注入

1.2.1 构造器注入



1.2.2 set注入

这是在默认无参的情况下。各种类型情况下。
value是直接命名值,ref表示引用值。
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  5.         https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
  6.     <bean id="address" >
  7.         <property name="address" value="河南"/>
  8.     </bean>
  9.    
  10.     <bean id="student" >
  11.         
  12.         <property name="name" value="houshuaixin"/>
  13.         
  14.         <property name="address" ref="address"/>
  15.         
  16.         <property name="books">
  17.             <array>
  18.                 <value>红楼梦</value>
  19.                 <value>西游记</value>
  20.                 <value>水浒传</value>
  21.                 <value>三国演义</value>
  22.             </array>
  23.         </property>
  24.         
  25.         <property name="hobbys">
  26.             <list>
  27.                 <value>听歌</value>
  28.                 <value>敲代码</value>
  29.                 <value>看电影</value>
  30.             </list>
  31.         </property>
  32.         
  33.         <property name="card">
  34.             <map>
  35.                 <entry key="身份证" value="1111111"/>
  36.                 <entry key="银行卡" value="2222222"/>
  37.             </map>
  38.         </property>
  39.         
  40.         <property name="games">
  41.             <set>
  42.                 <value>CF</value>
  43.                 <value>LOL</value>
  44.                 <value>BOB</value>
  45.             </set>
  46.         </property>
  47.         
  48.         <property name="wife">
  49.             <null></null>
  50.         </property>
  51.         
  52.         <property name="info">
  53.             <props>
  54.                 <prop key="学号">215151</prop>
  55.                 <prop key="性别">男</prop>
  56.             </props>
  57.         </property>
  58.     </bean>
  59.    
  60. </beans>
复制代码
1.2.3 拓展方式注入(p、c命名空间)

头文件取xmlns到schema/p或者c
  1. xmlns:p="http://www.springframework.org/schema/p"
  2.        xmlns:c="http://www.springframework.org/schema/c"
复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  4.        xmlns:p="http://www.springframework.org/schema/p"
  5.        xmlns:c="http://www.springframework.org/schema/c"
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  7.         https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
  8.    
  9.     <bean id="user"  p:name="狂神" p:age="32"/>
  10.    
  11.     <bean id="user2"  c:age="18" c:name="狂神"/>
  12.    
  13. </beans>
复制代码
1.3 Bean的作用域

在配置bean时,有一个标签scope
sington:默认的单例模式,前后getBean相同。
prototype:原型模式,每次getBean都会产生一个新对象。
1.4 Bean的自动装配方式

1.4.1 在xml中显式的自动装配

类People中属性依赖了一个类Cat,手动配置是在bean中用指定,自动配置是在标签bean中添加属性autowire=""
1.4.2 使用spring注解的自动装配 @Autowired

在资源配置文件中开启注解支持
@Autowired:可以用在属性,也可以用在set方法。
1.4.3 使用javaee提供的注解  @Resource

先开启注解支持
然后用法和autowired相同,不同的是:
1.5 使用注解开发

再配置文件中配置:
注解


@component注解相当于一个的注册。
@Value()相当于一个




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4