1、简介
Spring是一个轻量级Java
开发框架,最早有Rod Johnson
创建,目的是为了解决企业级应用开发的业务逻辑层和其他各层的耦合问题。它是一个分层的JavaSE/JavaEE full-stack
(一站式)轻量级开源框架,为开发Java
应用程序提供全面的基础架构支持。Spring
负责基础架构,因此Java
开发者可以专注于应用程序的开发。
1.1、Spring
的几个核心概念
-
IoC
:Inversion of Control
,控制反转 -
DI
:Dependency Injection
,依赖注入 -
AOP
:Aspect Oriented Programming
,面向切片编程
1.2、Spring
下载
jar
包、文档下载地址
2、IoC
2.1、IoC
的基本使用
1、添加Maven
依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
2、创建核心配置文件applicationContext.xml
<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="personDao" class="com.sj.dao.impl.PersonDaoImpl"></bean>
<bean id="personService" class="com.sj.service.impl.PersonServiceImpl">
<!--属性名称-->
<property name="dao" ref="personDao"/>
</bean>
<bean id="personServlet" class="com.sj.servlet.PersonServlet">
<property name="service" ref="personService"/>
</bean>
</beans>
applicationContext.xml
其中
bean
映射对应的类名称,property
对应的bean
类的属性。3、创建对应
bean
类的实现(其中service
未在此写明)PersonServlet
实现
public class PersonServlet {
private PersonService service;
// 设置service属性
public void setService(PersonService service) {
this.service = service;
}
public void remove() {
service.remove(1);
}
public static void main(String[] args) {
// 创建配置文件
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
PersonServlet servlet = (PersonServlet) ctx.getBean("personServlet", PersonServlet.class);
servlet.remove();
}
}
PersonServiceImpl
实现
public class PersonServiceImpl implements PersonService{
private PersonDao dao;
// 设置dao属性
public void setDao(PersonDao dao) {
this.dao = dao;
}
@Override
public boolean remove(Integer id) {
return dao.remove(id);
}
}
PersonDaoImpl
实现
public class PersonDaoImpl implements PersonDao {
@Override
public boolean remove(Integer id) {
System.out.println("PersonDaoImpl ----");
return false;
}
}
3、依赖注入
3.1、常见的注入内容可以分为3大类
1、bean
(自定义类型)
2、基本类型、String
、BigDecimal
3、集合类型(数组、Map
、List
、Set
、Properties
)
3.2、常见的注入方式有2种
1、基于setter
(属性)
<bean id="dog" class="com.sj.domain.Dog" />
<bean id="person" class="com.sj.domain.Person">
<property name="dog">
<ref bean="dog"/>
</property>
</bean>
<bean id="person2" class="com.sj.domain.Person">
<property name="dog">
<bean class="com.sj.domain.Dog"/>
</property>
</bean>
list
<property name="dogs">
<list>
<bean class="com.sj.domain.Dog" />
</list>
</property>
set
<!--LinkedHashSet-->
<property name="phones">
<set>
<value>iPhone</value>
</set>
</property>
map
<!--LinkedHashMap-->
<property name="friends">
<map>
<entry key="Jack" value="南京"/>
<entry key="Rose" value="上海"/>
</map>
</property>
props
<property name="shoes">
<props>
<prop key="anta">安踏</prop>
</props>
</property>
2、基于constructor
(构造方法)
<bean id="dog" class="com.sj.domain.Dog" >
<constructor-arg value="19" type="int"/>
<constructor-arg value="maojao" type="java.lang.String"/>
</bean>
<bean id="dog" class="com.sj.domain.Dog" >
<constructor-arg value="19" index="0"/>
<constructor-arg value="maojao" index="1"/>
</bean>
3、在根标签中加入一个命名空间属性
xmlns:p="http://www.springframework.org/schema/p"
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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="person3" class="com.sj.domain.Person" p:age="18" p:name="ss" p:dog-ref="dog" />
</beans>
3.3、复杂的对象
1、静态工厂调用方法
ConnectionFactory
代码如下
public class ConnectionFactory {
public static Connection getConn() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test_mbatis",
"root",
"shijikl126");
}
}
applicationContext.xml
代码如下
<!--静态工厂方法-->
<bean id="conn" class="com.sj.obj.ConnectionFactory" factory-method="getConn" />
2、实例工厂方法
(好处是:可以将参数转由外部注入,比如:username
、password
)
ConnectionFactory
代码如下
public class ConnectionFactory {
public Connection getConn() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test_mbatis",
"root",
"shijikl126");
}
}
applicationContext.xml
代码如下
<!--实例工厂方法-->
<bean id="factory" class="com.sj.obj.ConnectionFactory" />
<bean id="conn" factory-bean="factory" factory-method="getConn" />
3、FactoryBean
方法
创建ConnectionFactoryBean
文件需要实现FactoryBean
接口的两个方法
public class ConnectionFactoryBean implements FactoryBean<Connection> {
private String driverClass;
private String url;
private String username;
private String password;
public void setDriverClass(String driverClass) {
this.driverClass = driverClass;
}
public void setUrl(String url) {
this.url = url;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public Connection getObject() throws Exception {
Class.forName(driverClass);
return DriverManager.getConnection(url, username, password);
}
@Override
public Class<?> getObjectType() {
return Connection.class;
}
}
applicationContext.xml
代码如下
<bean id="conn" class="com.sj.obj.ConnectionFactoryBean">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test_mbatis" />
<property name="username" value="root" />
<property name="password" value="shijikl126" />
</bean>
3.4、applicationContext.xml
拆分配置文件
applicationContext.xml
引入db.properties
配置文件
applicationContext.xml
中新增如下url
:
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
<beans xmlns="http://www.springframework.org/schema/beans"
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/context
http://www.springframework.org/schema/context/spring-context.xsd ">
<!--默认是带有 classpath:db.properties-->
<context:property-placeholder location="db.properties" />
<bean id="conn" class="com.sj.obj.ConnectionFactoryBean">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
</beans>
3.5、spEL
表达式
通过#{对象}
引用对象,EL
表达式也可以直接调用方法
<bean id="dog" class="com.sj.domain.Dog"></bean>
<bean id="person" class="com.sj.domain.Person" >
<property name="dog" ref="#{dog}" />
<property name="name" ref="#{dog.getName()}" />
<property name="age" value="18" />
</bean>
3.6、scope
1、在默认情况下在创建IoC容器的时候所有的bean
都被创建。
2、可以通过scope
属性控制bean
是否是单例singleton
(在同一个容器中,通 过相同的id
值拿到的对象为同一个对象)。
3、设置lazy-init="true"
在getBean
的时候才创建bean
。
4、prototype
:非单例。每次getBean
的时候创建一次bean
<bean id="dog" class="com.sj.domain.Dog" lazy-init="true" />
<bean id="dog" class="com.sj.domain.Dog" scope="singleton" />
网友评论