提示:本文章为初学spring笔记
文章目录
前言
本文记录了spring学习的基础内容。
一、什么是spring?
Spring是以IOC(Inverse Of Control 控制反转) 和AOP(Aspect Oriented Programming 面向切面编程)为内核的轻量级、分层、松耦合的开源框架,提供了企业级编程复杂性的一站式解决方案,是整合第三方开源技术的容器。
二、创建对象
1、传统方式创建对象
代码如下(示例):
User user1 = new User();
User user2 = User.class.newInstance();//使用newInstance()方法创建对象
2、spring方式创建对象
代码如下(示例):
//在application.xml中创建bean (一个bean就代表一个对象)
<bean id="user" class="com.hqyj.lls.entity.User"/>
测试打印:
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
Student user = context.getBean("user", User.class);
System.out.println(user);//User{id=null, username=null, password=null}
三、spring - 有参构造的三种创建方式
1、通过参数名称初始化
<bean id="user" class="com.hqyj.lls.entity.User" scope="prototype">
<!--通过构造方法的参数名称初始化-->
<constructor-arg name="id" value="1"/>
<constructor-arg name="username" value="zhangSan"/>
<constructor-arg name="password" value="123456"/>
</bean>
2、通过通过索引初始化(下标从0开始)
<bean id="user" class="com.hqyj.lls.entity.User" scope="prototype">
<!--通过构造方法的参数索引进行初始化-->
<constructor-arg index="0" value="2"/>
<constructor-arg index="1" value="liSi"/>
<constructor-arg index="2" value="456789"/>
</bean>
3、通过参数类型
<bean id="user" class="com.hqyj.lls.entity.User" scope="prototype">
<!--通过构造方法的参数类型进行初始化-->
<constructor-arg type="java.lang.Integer" value="3"/>
<constructor-arg type="java.lang.String" value="test3"/>
<constructor-arg type="java.lang.String" value="765432"/>
</bean>
四、获取对象(bean)的三种方式:
1、强制转换
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
//强制类型转换
User user1 = (User)context.getBean("user");
System.out.println(user1);
2、通过反射获取(推荐)
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
User user2 = context.getBean("user", User.class);
System.out.println(user2);
3、通过类型获取(推荐)
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
//当配置文件中同类型bean有多个这时候不能用类型获取
User user3 = context.getBean(User.class);
System.out.println(user3);
五、依赖注入
1、构造方法注入(确保注入的字段和实体类属性一致)
<bean id="student" class="com.hqyj.lls.entity.Student" scope="prototype">
<constructor-arg name="stuId" value="1"/>
<constructor-arg name="stuName" value="zhangsan"/>
<constructor-arg name="stuAge" value="22"/>
</bean>
2、set注入
<!--bean注入-->
<bean id="date" class="java.util.Date"/>
<!--2.bean注入,对象注入-->
<property name="date" ref="date"/>
<!--非bean注入-->
<!--1.常量注入-->
<property name="id" value="99"/>
<property name="username" value="mk"/>
<property name="password" value="0831"/>
<!--3.null值注入-->
<property name="isWife">
<null/>
</property>
<!--4.数组注入-->
<property name="books">
<array>
<value>《西游记》</value>
<value>《红楼梦》</value>
</array>
</property>
<!--5.List注入-->
<property name="hobby">
<list>
<value>唱跳</value>
<value>rap</value>
<value>篮球</value>
</list>
</property>
<!--6.Set注入-->
<property name="sets">
<set>
<value>1</value>
<value>2</value>
<value>3</value>
</set>
</property>
<!--7.Map注入-->
<property name="maps">
<map>
<entry key="c" value="3"/>
<entry key="d" value="4"/>
<entry key="e" value="5"/>
</map>
</property>
<!--8.properties注入-->
<property name="properties">
<props>
<prop key="userDao">userDaoImpl</prop>
<prop key="userService">userServiceImpl</prop>
</props>
</property>
3、p注入 和 c注入
需要导入约束
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
代码如下(示例):
<!--p空间注入:等价于set注入,不赋值的参数默认为null-->
<bean id="student2" class="com.hqyj.lls.entity.Student" p:stuName="老王"/>
<!--Student(stuId=null, StuName=老王, StuAge=null)-->
<!--c空间注入:等价于构造方法注入,构造方法注入必须为每个参数赋值-->
<bean id="student1" class="com.hqyj.lls.entity.Student" c:stuId="1" c:stuName="zhangsan" c:stuAge="14"/>
<!--Student(stuId=1, StuName=张三, StuAge=14)-->