“天生我材必有用,千金散尽还复来!”
----------持续更新Spring入门系列知识点-------------
你的点赞、关注、评论、是我创作的动力!
-------希望我的文章对你有所帮助--------
前言:其实学习编程从来没有捷径,只有靠稳扎稳答的夯实基础知识,以及不断的实践与练习去磨练技术以及加深认知和理解。本文仅仅作为初步了解以及运用Spring的参考,如有不当之处,恳请不吝赐教!
本文以如同我般的初学者的角度,简便的梳理Spring体系结构中的重要知识点以及简单的实例演示!删繁就简!
目录
Spring是基于JavaEE的一个轻量级开源框架,核心理念是IoC(控制反转)和AOP(面向切面编程)。IoC是Spring的基础,它支撑着Spring对Java Bea的管理;AOP是Spring的重要特性,通过预编译和运行期间的动态代理来实现程序的运行,在不违反开闭原则的情况下,对程序进行功能的添加。
1、非侵入式设计
2、降低耦合,方便开发
3、支持AOP编程
4、支持声明事物
5、方便程序测试
6、方便集成各种框架
7、降低JavaEE API的使用难度
代码:
- "1.0" encoding="UTF-8"?>
"http://maven.apache.org/POM/4.0.0" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-
4.0.0 -
-
org.example -
spring_demo -
1.0-SNAPSHOT -
-
-
-
junit -
junit -
4.13 -
-
-
org.springframework -
spring-context -
5.0.2.RELEASE -
-
-
log4j -
log4j -
1.2.9 -
-
-
org.springframework -
spring-test -
5.0.2.RELEASE -
-
-
org.projectlombok -
lombok -
1.18.12 -
provided -
-
本文简单的以HelloSpring类来演示说明,代码如下:
- package com.itheima;
- public class HelloSpring {
- private String Name;
- public void setName(String Name){
- this.Name=Name;
- }
- public void Show(){
- System.out.println(Name+"-Hello Spring!");
- }
- }
每个被注入Spring容器中的Bean(类)都需要在配置文件中进行定义Bean,并且直接注入依赖(赋值)
1、其中property的name为Bean中的成员变量名,value则是具体值。
- "1.0" encoding="UTF-8"?>
"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">
-
"HelloSpring" class="com.itheima.HelloSpring"> -
"Name" value="小辉"> -
测试类里为对于程序的具体实现,需要new 一个applicationContext作为Spring的容器来管理注册的Bean,而不是直接通过常规的new 创造一个实例,这也就是IOC理念,将创建、管理类的权力交给Spring容器!
- package com.itheima;
-
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class TestHelloSpring {
- public static void main(String args[]){
- ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
- HelloSpring helloSpring=(HelloSpring) applicationContext.getBean("HelloSpring");
- helloSpring.Show();
- }
- }

setter依赖注入即在类中通过设定set_XX()、get_XX()方法来定义或者获取成员变量,且在applicationContext中Bean中通过子标签property来进行依赖注入。
如下代码:
public class HelloSpring { private String Name; public void setName(String Name){ this.Name=Name; } public String getName() { return this.Name; } }因此,applicationContext文件中:
"HelloSpring" class="com.itheima.HelloSpring"> "Name" value="小辉">
即通过构造方法对类中的成员变量进行赋值,那么相对应的,Bean中将通过
Constructor-arg标签进行依赖注入。
代码如下:
public class HelloSpring { private String Name; private Integer id; public void HelloSpring(String Name,Integer id){ this.Name=Name; this.id=id; } }applicationContext配置文件:
"HelloSpring" class="com.itheima.HelloSpring"> "Name" value="小辉"> "id" value="0001">
本期Spring入门系列文章主要介绍了IoC反转的基本理念,以及Spring容器对于Bean的管理,两种基本依赖注入方式,以及对应的applicationContext中的标签进行赋值,实现了简单入门实例。
请问你学废了吗?
感谢爱学习的你看到了最后,点个赞、关注支持一下吧!