1.什么是spring
百度百科:https://baike.baidu.com/item/spring/85061?fr=aladdin
Spring为简化企业级开发而生,使用Spring开发可以将Bean对象,Dao组件对象,Service组件对象等交给Spring容器来管理,这样使得很多复杂的代码在Spring中开发却变得非常的优雅和简洁,有效的降低代码的耦合度,极大的方便项目的后期维护、升级和扩展。
spring5的框架图
2.框架搭建
1.下载jar包
下面开始搭建一个最基本的spring的demo:
下载jar包,到官网查看那些版本是稳定版本: https://spring.io/
去这个地址下载jar包
https://repo.spring.io/release/org/springframework/spring/
2.新建项目
先导入搭建工厂最基本的四个核心jar:
spring-context-5.2.6.RELEASE.jar
spring-beans-5.2.6.RELEASE.jar
spring-core-5.2.6.RELEASE.jar
spring-expression-5.2.6.RELEASE.jar
也就是框架图中的核心容器部分
idea新建一个java项目,导入jar包,创建一个spring的xml文件
如上一个空的spring架子就搭好了。下面学习spring最核心的两个功能ioc控制反转,和aop面向切面
3.ioc
什么是ioc
ioc控制反转:
1.把对象创建和调用交给spring
2.降低耦合度
3.用到了xml解析,反射、工厂模式
工厂模式:通过工厂来对类的实例化,实现对象和对象之间进行解耦。但是这样工厂和对象的耦合度就很高;下面在使用反射和xml来进一步解耦。
反射和xml的使用
这样进一步降低耦合度,对象修改,改xml即可,工厂可以不用修改
ioc接口
1.ioc的思想基于工厂模式
工厂模式参考: https://blog.csdn.net/u014692224/article/details/109532711
2.spring提供ioc容器实现两种方式
- BeanFactory,IOC容器基本实现,是spring尼日不使用接口,不提供给工开发人员进行使用
- applicationContex ,是beanFactory接口的子接口,提供强大的功能,一般由开发人员进行使用
IOC操作的bean管理操作
1.什么是bean管理操作
spring创建对象
spring注入属性
2.bean操作管理有两种方式
1.基于xml方式
在spring的配置文件中配置bean标签
关于bean标签
id属性:class对象的一个标识
class属性:创建的类的全路径
name属性:和id属性差不多,只是能用特殊符号,很少用了
创建对象的时候,默认也是执行无参构造的方法来完成创建对象。
基于xml的注入属性
1.DI:依赖注入,注入属性
第一种:使用set方式注入
第二种:通过有参构造
2.基于注解方式
ioc操作bean管理(factoryBean)
1.Spring有两种bean,一种是普通bean,
普通bean:定义的类型就是bean的类型
工厂bean:定义类型和返回bean可以不一样
bean 的生命周期
- 通过构造器创建bean 的实列(无参数构造)
- 为bean的属性设置值和对其他bean引用(调用set方法)
- 把bean实列传递bean后置处理器的方法
- 低啊用bean的初始化的方法(需要进行配置 初始化的方法)
- bean可以使用(对象获取到了)
- 把容器关闭的时候,调用bean的摧毁的方法(需要进行配置摧毁的方法)
代码
对象的构造函数有参无参的xml注入方式和对象调用注入方式
bean1.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<util:list id="booklist">
<value>喵喵琳</value>
<value>小宝宝</value>
<value>小卡喵</value>
</util:list>
<!--新建一个bean-->
<bean id="user" class="com.changxiong.spring.entity.User"></bean>
<!--无参(默认):set注入对象的属性-->
<bean id="book" class="com.changxiong.spring.entity.Book">
<property name="name" value="小玲"></property>
<property name="address">
<value><![CDATA[《南京》]]></value>
</property>
<!--value为空的情况-->
<property name="age">
<null></null>
</property>
<property name="lists">
<ref bean="booklist"></ref>
</property>
</bean>
<!--有参构造,注入对象属性-->
<bean id="order" class="com.changxiong.spring.entity.Order">
<constructor-arg name="name" value="小玲"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
</bean>
<!--对象调用-->
<bean id="service" class="com.changxiong.spring.serice.iocService" >
<property name="iocDao" ref="daoImpl"></property>
</bean>
<bean id="daoImpl" class="com.changxiong.spring.dao.iocDao"></bean>
</beans>
Book.java
public class Book {
private String name;
private String address;
private String age;
private List<String> lists;
public void setLists(List<String> lists) {
this.lists = lists;
}
public void setAge(String age) {
this.age = age;
}
public void setAddress(String address) {
this.address = address;
}
public void setName(String name) {
this.name = name;
}
public void test() {
System.out.println("------"+name+" :: "+address+"::"+age+":"+lists);
}
@Override
public String toString() {
return name+":"+address+":"+age;
}
}
iocIService
package com.changxiong.spring.serice;
/**
* @author changxiong
* @create 2020-12-29-5:19 PM
*/
public interface iocIService {
void add();
}
iocService
package com.changxiong.spring.serice;
import com.changxiong.spring.dao.iocDao;
/**
* @author changxiong
* @create 2020-12-29-5:20 PM
*/
public class iocService implements iocIService {
private iocDao iocDao;
public void setIocDao(com.changxiong.spring.dao.iocDao iocDao) {
this.iocDao = iocDao;
}
@Override
public void add() {
iocDao.add();
}
}
iocIDao
package com.changxiong.spring.dao;
/**
* @author changxiong
* @create 2020-12-29-5:21 PM
*/
public interface iocIDao {
void add();
}
iocDao
package com.changxiong.spring.dao;
/**
* @author changxiong
* @create 2020-12-29-5:22 PM
*/
public class iocDao implements iocIDao {
@Override
public void add() {
System.out.println("add.......");
}
}
testDemo.java
package com.changxiong.spring.test;
import com.changxiong.spring.bean.Emp;
import com.changxiong.spring.entity.Book;
import com.changxiong.spring.entity.Order;
import com.changxiong.spring.entity.User;
import com.changxiong.spring.entity.stu;
import com.changxiong.spring.serice.iocService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author changxiong
* @create 2020-12-29-11:55 AM
*/
public class iocTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
User user = context.getBean("user", User.class);
System.out.println(user);
}
@Test
public void testSet() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
Book book = context.getBean("book", Book.class);
System.out.println(book);
book.test();
}
@Test
public void testConstr() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
Order order = context.getBean("order", Order.class);
System.out.println(order);
order.test();
}
@Test
public void testDispatch() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
iocService iocService = context.getBean("service", iocService.class);
System.out.println(iocService);
iocService.add();
}
}
自动装配
- 根据指定的装配,spring自动将匹配的属性值进行注入
Dept
package com.changxiong.spring.bean;
/**
* @author changxiong
* @create 2020-12-29-5:46 PM
*/
public class Dept {
private String id;
private String name;
public void setName(String name) {
this.name = name;
}
public void setId(String id) {
this.id = id;
}
public void test() {
System.out.println(id+"::"+name);
}
}
Emp
package com.changxiong.spring.bean;
/**
* @author changxiong
* @create 2020-12-29-5:48 PM
*/
public class Emp {
private String name;
private String age;
private Dept dept;
public void setName(String name) {
this.name = name;
}
public void setAge(String age) {
this.age = age;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public void test() {
System.out.println(name+"::"+age+"::"+dept);
}
}
bean2.xml
<?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">
<bean id="emp" class="com.changxiong.spring.bean.Emp">
<property name="name" value="小玲"></property>
<property name="age" value="18"></property>
<property name="dept" ref="dept"></property>
</bean>
<bean id="dept" class="com.changxiong.spring.bean.Dept">
<property name="name" value="大猫王"></property>
<property name="id" value="19"></property>
</bean>
</beans>
test
@Test
public void testEleInEle() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
Emp emp = context.getBean("emp", Emp.class);
System.out.println(emp);
emp.test();
}
Order
package com.changxiong.spring.entity;
/**
* @author changxiong
* @create 2020-12-29-4:51 PM
*/
public class Order {
private String name;
private String age;
public Order(String name, String age) {
this.name = name;
this.age = age;
}
public void test() {
System.out.println("----"+name+"::"+age);
}
}
stu
package com.changxiong.spring.entity;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author changxiong
* @create 2020-12-29-7:03 PM
*/
public class stu {
private String[] arr;
private List<String> listStr;
private Map<String,String> maps;
private Set<String> sets;
private List<Book> books;
public void setBooks(List<Book> books) {
this.books = books;
}
public void setSets(Set<String> sets) {
this.sets = sets;
}
public void setArr(String[] arr) {
this.arr = arr;
}
public void setListStr(List<String> listStr) {
this.listStr = listStr;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public void test() {
System.out.println(Arrays.toString(arr));
System.out.println(listStr);
System.out.println(maps);
System.out.println(sets);
System.out.println(books);
}
}
注入集合对象的操作
bean3.xml
<?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">
<bean id="stu" class="com.changxiong.spring.entity.stu">
<!--数组-->
<property name="arr">
<array>
<value>小玲</value>
<value>小肖</value>
<value>小喵</value>
</array>
</property>
<!--list集合-->
<property name="listStr">
<list>
<value>喵喵琳</value>
<value>小宝宝</value>
<value>小卡喵</value>
</list>
</property>
<!--map集合-->
<property name="maps">
<map>
<entry key="1" value="肖肖"></entry>
<entry key="2" value="琳琳"></entry>
<entry key="3" value="玲玲"></entry>
</map>
</property>
<!--set集合-->
<property name="sets">
<set>
<value>宝宝1</value>
<value>宝宝2</value>
<value>宝宝3</value>
</set>
</property>
<property name="books">
<list>
<ref bean="book1"></ref>
<ref bean="book2"></ref>
</list>
</property>
</bean>
<bean id="book1" class="com.changxiong.spring.entity.Book">
<property name="name" value="小玲"></property>
<property name="address" value="newyork"></property>
<!--value为空的情况-->
<property name="age">
<null></null>
</property>
</bean>
<bean id="book2" class="com.changxiong.spring.entity.Book">
<property name="name" value="小喵"></property>
<property name="address" value="南京"></property>
<!--value为空的情况-->
<property name="age">
<null></null>
</property>
</bean>
</beans>
test
//测试集合
@Test
public void testArrays() {
ApplicationContext context = new ClassPathXmlApplicationContext("baen3.xml");
stu stus = context.getBean("stu", stu.class);
System.out.println(stus);
stus.test();
}
//测试sprng默认是单实列
@Test
public void test1() {
ApplicationContext context = new ClassPathXmlApplicationContext("baen3.xml");
stu stus1= context.getBean("stu", stu.class);
stu stus2= context.getBean("stu", stu.class);
System.out.println(stus1);
System.out.println(stus2);
}
ioc操作bean管理(factoryBean)
ioc操作bean管理(factoryBean)
1.Spring有两种bean,一种是普通bean,
普通bean:定义的类型就是bean的类型
工厂bean:定义类型和返回bean可以不一样
<?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">
<bean id="myBean" class="com.changxiong.spring.factoryBean.MyBean"></bean>
</beans>
course
package com.changxiong.spring.factoryBean;
/**
* @author changxiong
* @create 2020-12-29-9:04 PM
*/
public class course {
}
public class factoryTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
course myBean = context.getBean("myBean", course.class);
System.out.println(myBean);
}
}
bean 的生命周期
- 通过构造器创建bean 的实列(无参数构造)
- 为bean的属性设置值和对其他bean引用(调用set方法)
- 把bean实列传递bean后置处理器的方法
- 低啊用bean的初始化的方法(需要进行配置 初始化的方法)
- bean可以使用(对象获取到了)
- 把容器关闭的时候,调用bean的摧毁的方法(需要进行配置摧毁的方法)
bean5.xml
<?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">
<bean id="user" class="com.changxiong.spring.beanLive.User" init-method="initMethod" destroy-method="destoryMethod">
<property name="name" value="肖肖"></property>
</bean>
<bean id="myBeanPost" class="com.changxiong.spring.beanLive.myBeanPost"></bean>
</beans>
User
package com.changxiong.spring.beanLive;
/**
* @author changxiong
* @create 2020-12-29-9:40 PM
*/
public class User {
private String name;
public User() {
System.out.println("bean实列创建生命周期:");
System.out.println("第一步,无参构造创建bean实列");
}
public void setName(String name) {
System.out.println("第二步:set方法创建属性");
this.name = name;
}
//第三步:调用初始化的方法
public void initMethod() {
System.out.println("第三步:调用初始化");
}
//第五步:处理结束时候调用的方法
public void destoryMethod() {
System.out.println("这是第五步:结束的时候调用的方法");
}
}
myBeanPost
package com.changxiong.spring.beanLive;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
* @author changxiong
* @create 2020-12-29-9:55 PM
*/
public class myBeanPost implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("初始化之前的方法");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("初始化之后的方法");
return bean;
}
}
test
public class beanLiveTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean5.xml");
User user = context.getBean("user", User.class);
System.out.println("第四步:创建实列");
// System.out.println(user);
((ClassPathXmlApplicationContext) context).close();
}
}
引用数据库和文件内容
jdbc.properties
prop.driverClass=com.mysql.jdbc.Driver
prop.url=jdbc:mysql://localhost:3306/userDb
prop.userName=root
prop.password=root
UserIService
package com.changxiong.spring.serice;
/**
* @author changxiong
* @create 2020-12-30-10:50 AM
*/
public interface UserIService {
void add();
}
UserServiceImpl
package com.changxiong.spring.serice;
import com.changxiong.spring.dao.UserIDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* @author changxiong
* @create 2020-12-30-10:51 AM
*/
@Service
public class UserServiceImpl implements UserIService {
// @Resource
// @Resource(name = "userDaoImpl")
// @Qualifier(value = "userDaoImpl1")
@Autowired
private UserIDao userIDao;
@Override
public void add() {
userIDao.add();
}
}
UserIDao
package com.changxiong.spring.dao;
/**
* @author changxiong
* @create 2020-12-30-10:51 AM
*/
public interface UserIDao {
void add();
}
UserDaoImpl
package com.changxiong.spring.dao;
import org.springframework.stereotype.Repository;
import javax.naming.Name;
/**
* @author changxiong
* @create 2020-12-30-10:52 AM
*/
@Repository(value = "userDaoImpl1")
public class UserDaoImpl implements UserIDao {
@Override
public void add() {
System.out.println("addd.......");
}
}
bean7.xml
<?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:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${prop.driverClass}"></property>
<property name="url" value="${prop.url}"></property>
<property name="username" value="${prop.userName}"></property>
<property name="password" value="${prop.password}"></property>
</bean>
<context:property-placeholder location="jdbc.properties"></context:property-placeholder>
</beans>
test
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean7.xml");
UserServiceImpl userServiceImpl = context.getBean("userServiceImpl", UserServiceImpl.class);
// UserIService userServiceImpl2 = context.getBean("userServiceImpl", UserIService.class);
userServiceImpl.add();
// System.out.println(userServiceImpl);
// System.out.println(userServiceImpl2);
}
注解版
1.什么是注解
2.spring针对bean管理中创建提供注解
- @Component
- @Service
- @Controller
- @Reposity
上面的四个注解都是一个意思,都可以用来创建bean 的实列
2.基于注解方式实现创建对象
第一步:引入jar
5.基于注解方式实现属性的注入
- @Autowired :根据属性类型进行自动装配
- @Qualifier 根据属性名称进行注入 要和@Autowired一起使用
- @Resource 可以更具类型注入,可以根据名称注入(但是不是spring的包下的注解,建议用上面两种)
- @value 注入普通类型属性
springConfig
package com.changxiong.spring.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @author changxiong
* @create 2020-12-30-11:13 AM
*/
@Configuration
@ComponentScan(basePackages = {"com.changxiong.spring"})
public class springConfig {
}