Spring
1. Spring概述
Spring 是分层的 Java SE/EE 应用 full-stack 轻量级开源框架,以 IOC(Inverse Of Control:
反转控制)和 AOP(Aspect Oriented Programming:面向切面编程)为内核,提供了展现层 Spring
MVC 和持久层 Spring JDBC 以及业务层事务管理等众多的企业级应用技术,还能整合开源世界众多
著名的第三方框架和类库,逐渐成为使用最多的 Java EE 企业应用开源框架。
2. 搭建第一个Spring工程
第一步:创建maven工程,导入坐标
<dependency><!--Spring IOC-->
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
第二步:创建核心配置文件applicationContext.xml 或者 beans.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-3.0.xsd">
<!--IOC 控制反转-->
<!--DI 依赖注入-->
<bean id="userServlet" class="com.zx.servlet.UserServlet">
<property name="userService" ref="userService"></property>
</bean>
<bean id="userService" class="com.zx.service.impl.UserServiceImpl02">
<property name="userDao" ref="userDao"></property>
<property name="roleDao" ref="roleDao"></property>
</bean>
<bean id="userDao" class="com.zx.dao.impl.UserDaoImpl"></bean>
<bean id="roleDao" class="com.zx.dao.impl.RoleDaoImpl"></bean>
</beans>
第三步:读取配置文件,创建beanFactory
public static void main(String[] args) {
//读取配置文件
BeanFactory beanFactory = new ClassPathXmlApplicationContext("beans.xml");
//由beanFactory获取Spring容器中的对象
UserServlet userServlet = (UserServlet) beanFactory.getBean("userServlet");
userServlet.add();
}
注意事项:
\1. bean中的class写的全限定类名必须是能创建对象的类
\2. 如果想要让bean创建对象成功,spring默认调用被创建对象的空参构造方法
\3. property 代表依赖注入,需要注意被注入的属性,在对象中应该提供相对应的set方法
3. 第一个核心思想IOC(控制反转)
Inversion of Control
控制反转之后需要做依赖注入
XML版本
注解版本
4. IOC创建对象时机
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
或者
BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
加载配置文件结束,默认初始化对象
但是如果想要在加载完配置文件的时候不创建对象,需要在某一个对象的bean标签中追加懒加载配置
<bean id="userServlet" class="com.zx.servlet.UserServlet" lazy-init="true">
Resource resource = new ClassPathResource("beans.xml");
BeanFactory factory = new XmlBeanFactory(resource);
加载配置文件不创建对象,什么时候使用,什么时候创建
5.IOC的三种形式
第一种方式:使用默认无参构造函数
第二种方式:spring 管理静态工厂-----使用静态工厂的方法创建对象
第三种方式:spring 管理实例工厂—使用实例工厂的方法创建对象
6.依赖注入
简单属性注入
<property name="age" value="20"></property>
集合属性注入
自定义对象
-
手动注入形式
第一种:默认使用属性的set方法注入
<bean id="userServlet" class="com.zx.servlet.UserServlet"> <property name="userService" ref="userService"></property> </bean>
public void setUserService(UserService userService) { this.userService = userService; }
<bean id="userService" class="com.zx.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"></property> <property name="roleDao" ref="roleDao"></property> </bean>
第二种:利用构造方法注入
<bean id="userServlet" class="com.zx.servlet.UserServlet"> <constructor-arg name="userService" ref="userService"></constructor-arg> </bean>
public UserServlet(UserService userService) { this.userService = userService; }
<bean id="userService" class="com.zx.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"></property> <property name="roleDao" ref="roleDao"></property> </bean>
-
自动注入形式
<!--自动注入: autowire constructor:使用构造方法注入 byName:通过名称注入 (一般情况下寻找属性名称,但是要知道其实寻找的是set方法) 一定要提供一个名称符合set方法的id标签,跟控制反转的个数没有关系 byType:通过类型注入 (和名称没有关系) 需要提供唯一的一个类型能匹配的id标签 --> <bean id="userServlet" class="com.zx.servlet.UserServlet" autowire="byName"></bean> <bean id="userService" class="com.zx.service.impl.UserServiceImpl" autowire="byName"></bean> <bean id="userDao" class="com.zx.dao.impl.UserDaoImpl"></bean> <bean id="roleDao" class="com.zx.dao.impl.RoleDaoImpl"></bean>
-
注解形式
第一步:删除原有的xml形式的IOC与DI
第二步:在beans.xml中配置注解的命名空间、开启注解以及开启组件扫描
<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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!--开启注解--> <context:annotation-config/> <!--开启组件扫描--> <context:component-scan base-package="com.zx"></context:component-scan>
第三步:在需要做控制反转的类上边标明注解**@Component**
第四步:在需要做依赖注入的set方法上或者属性上标明**@Autowired**
或者
第三步:在需要做控制反转的控制器类上边标明注解**@Controller**
在需要做控制反转的业务层类上边标明注解**@Service**
在需要做控制反转的dao实现类上边标明注解**@Repository**
第四步:在需要做依赖注入的set方法上或者属性上标明**@Resource**
注意实现:@Autowired或者@Resource默认通过byType来注入
需要提供唯一的一个类型能匹配的id标签,但是如果有两个类型都能匹配,那么在注解中可以通过名称来指定注入类
@Autowired @Qualifier(value = "userServiceImpl") private UserService userService;
@Resource(name = "userServiceImpl") private UserService userService;
自己的类使用注解,第三方的使用XML