Spring
体系结构:
特点:
Ioc
控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。其中最常见的方式叫做依赖注入(Dependency Injection,简称DI),还有一种方式叫“依赖查找”(Dependency Lookup)
SpringIOC约束文件头(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">
</beans>
//测试类
//解析XML文件
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//获取AccoutService类
IAccountService as = ac.getBean("accountService",IAccountService.class);
//调用方法
List<Account> list = as.findAllAccount();
SpringIOC注解
@Component @Controller @Service @Repository == <bean id="" class="">
@Autowired @Qualifier @Resource @Value == <property name="" ref="">
<property name="" value="">
@Scope == <bean id="" class="" scope="">
@PostConstruct @PreDestroy == <bean id="" class="" init-method="" destroy-method="" />
新注解
@Configuration 用于指定当前类是一个 spring 配置
@Bean 该注解只能写在方法上,表明使用此方法创建一个对象,并且放入 spring 容器。
@PropertySource 用于加载.properties 文件中的配置。例如我们配置数据源时,可以把连接数据库的信息写到 properties 配置文件中,就可以使用此注解指定 properties 配置文件的位置。
注:1.Bean 缓存池为 HashMap实现
2.singleton:单例模式(多线程下不安全) prototype:原型模式每次使用时创建
AOP
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。
SpringAOP约束文件头(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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
SpringAOP注解
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
JdbcTemplate
(spring-jdbc-5.0.2.RELEASE.jar ,spring-tx-5.0.2.RELEASE.jar)
配置 C3P0 数据源
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///spring_day02"></property>
<property name="user" value="root"></property>
<property name="password" value="1234"></property>
</bean>
配置 DBCP 数据源
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:// /spring_day02"></property>
<property name="username" value="root"></property>
<property name="password" value="1234"></property>
</bean>
配置 spring 内置数据源
spring 框架也提供了一个内置数据源,我们也可以使用 spring 的内置数据源,它就在
spring-jdbc-5.0.2.REEASE.jar 包中:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///spring_day02"></property>
<property name="username" value="root"></property>
<property name="password" value="1234"></property>
</bean>
核心包
常用注解:
第三方结合包:
SpringMVC
流程图
常用注解:
SpringBoot
Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭 建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的 配置。特点:
-
创建独立的 Spring 应用程序
-
嵌入的 Tomcat,无需部署 WAR 文件
-
简化 Maven 配置
-
自动配置 Spring
-
ᨀ供生产就绪型功能,如指标,健康检查和外部配置
-
绝对没有代码生成和对 XML 没有要求配置 [1]