#spring
spring框架的核心思想就是Ioc(控制反转)和DI(依赖注入),最初就是由Java中的Ioc框架发展而来。
Ioc控制反转:把创建对象,设置依赖的权力转移到了无关的第三方类。
使用 IoC 可以降低类和类之间的耦合度,使得代码更容易维护,是企业项目开发的标配
spring的使用:
1,导入jar包
2,创建类,所有属性必须设置 getter && setter 方法
3. 在 src 下创建 spring.xml 文件用于 spring 的配置
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
</beans>
4.配置需要创建的对象和对象之间的依赖关系
配置方案一:
在spring.xml文件中使用bean标签和property标签创建对象并设置属性的值
id是对象的唯一标识,class是对象的全路径
如果没有id默认值是类的首字母小写
value 用于基本类型和 String 类型属性值
ref 用于引用类型属性值
配置方案二:
2.1.在类上使用注解创建对象
@Repository用于dao层
@Service用于service层
@Controller用于controller层
@Component用于其他层
2.2. 在属性上使用以下注解设置属性的值
@Value:为基本类型和 String 类型属性赋值
@Autowired 和 @Resource:为引用类型属性赋值
@Autowired基于类型赋值
@Resource基于名称赋值
3. 在 spring.xml 中配置启用哪些包下的注解
<context:component-scan base-package=“包名”/>
4. 创建 Spring 容器对象 ClassPathXmlApplicationContext 并读取解析 spring.xml 文件
5. 使用 getBean 方法获取要使用的对象
参数一是对象的 id,参数二是对象的类型
DI(Dependency Injection/依赖注入):为 A 类的 B 属性赋值
1.setter 方法注入,是 @Autowired 默认采用的方式,最常用
2. 构造方法注入
3. 工厂方法注入
web 项目使用 spring 的步骤:
-
复制 jar 文件到 lib 目录
-
在 WEB-INF 下创建 spring.xml 并进行配置,识别指定包中的 spring 注解
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> < context:component-scan base-package="包名"/> </beans>
-
生成 web.xml 并进行配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>user</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
4.oImpl 增加 @Repository 注解,用于创建对象
5. 为每一个 serviceImpl 增加 @Service 注解,用于创建对象
6. 在 serviceImpl 中使用 @Autowired 注解代替 = new xxxDaoImpl();
7. 在 servlet 中使用以下代码代替 new xxxServiceImpl();
ContextLoader.getCurrentWebApplicationContext().getBean(“userServiceImpl”, UserServiceImpl.class);