快速上手Spring
😊😊😊欢迎来到本博客😊😊😊
📫作者简介:阿斯卡码,专注于研究Java框架/Vue,就读于河南中医药大学,刚刚入门项目开发📫
🏆 CSDN编程比赛奖章获得者/Java领域创作者🏆
🔥计划学习:深入学习Spring全家桶,Vue, mybatis,Mysql等领域。(目前涉及不深入)🔥
👍如果此文还不错的话,还请👍关注、点赞、收藏三连支持👍一下博主~
上一期中我们理解了IOC的基本思想,我们现在来看下Spring的应用:
一:HelloSpring
1.导入Jar包(添加依赖)
注 : spring 需要导入commons-logging进行日志记录 . 我们利用maven , 他会自动下载对应的依赖项 .
spring-webmvc依赖
junit依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
加载之后:基本包含了其他它依赖,加这一个即可spring-webmvc
。
2.编写代码
1、编写一个Hello实体类
package com.du.pojo;
public class Hello {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("hello," + this.name);
}
}
2、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.xsd">
<bean id="hello" class="com.du.pojo.Hello">
<property name="name" value="spring"></property>
</bean>
</beans>
3、测试类
import com.du.pojo.Hello;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public static void main(String[] args)
{
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Hello hello = (Hello) context.getBean("hello");
hello.show();
}
}
思考
-
Hello 对象是谁创建的 ?
【Hello对象是由Spring容器创建的】 -
Hello 对象的属性是怎么设置的 ? 【Hello 对象的属性是由Spring容器设置的】
这个过程就叫控制反转 :
-
控制 : 谁来控制对象的创建 , 传统应用程序的对象是由程序本身控制创建的 , 使用Spring后 ,
对象是由Spring来创建的
-
反转 : 程序本身不创建对象 , 而
变成被动的接收对象
.
依赖注入 : 就是利用set方法来进行注入的.
IOC是一种编程思想,由主动的编程变成被动的接收
可以通过newClassPathXmlApplicationContext去浏览一下底层源码 .
3.修改案例一
我们在【Sprnig系列01】中, 新增一个Spring配置文件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.xsd">
<bean id="mysqlimpl" class="com.du.dao.UserDaoMySqlImpl"></bean>
<bean id="oracleimpl" class="com.du.dao.UserDaoOracleImpl"></bean>
<bean id="serviceimpl" class="com.du.service.UserServiceImpl">
<!--注意: 这里的name并不是属性 , 而是set方法后面的那部分 , 首字母小写-->
<!--引用另外一个bean , 不是用value 而是用 ref-->
<property name="userDao" ref="oracleimpl"></property>
</bean>
</beans>
测试!
import com.du.dao.UserDaoMySqlImpl;
import com.du.dao.UserDaoOracleImpl;
import com.du.service.UserService;
import com.du.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args)
{
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserService service = (UserService) context.getBean("serviceimpl");
service.getUser();
}
}
到了现在 , 我们彻底不用再程序中去改动了 , 要实现不同的操作 , 只需要在xml配置文件中进行修改 , 所谓的IoC,一句话搞定 : 对象由Spring 来创建 , 管理 , 装配
!
二:IOC创建对象方式
1.通过无参构造方法来创建
1、User.java
2、beans.xml
3、测试类
结果可以发现,在调用show方法之前,User对象已经通过无参构造初始化了!
2.通过有参构造方法来创建
1、UserT . java
package com.du.pojo;
public class User {
private String name;
private String address;
private Integer age;
public User(String name,String address,Integer age){
this.name = name ;
this.address = address;
this.age = age ;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("name="+this.name+",address="+this.address+",age="+this.age);
}
}
2、beans.xml 有三种方式编写
<!-- index按照顺序 -->
<!-- 第一种根据index参数下标设置 -->
<bean id="user" class="com.du.pojo.User">
<constructor-arg index="0" value="dyh"></constructor-arg>
<constructor-arg index="1" value="濮阳"></constructor-arg>
<constructor-arg index="2" value="12"></constructor-arg>
</bean>
<!-- 通过属性名赋值 -->
<!-- 第二种根据参数名字设置 -->
<bean id="user" class="com.du.pojo.User">
<constructor-arg name="name" value="dyh"></constructor-arg>
<constructor-arg name="address" value="濮阳"></constructor-arg>
<constructor-arg name="age" value="12"></constructor-arg>
</bean>
<!-- 注意:同类型的参数分前后顺序(相对位置) -->
<!-- 第三种根据参数类型设置 -->
<bean id="user" class="com.du.pojo.User">
<constructor-arg type="java.lang.Integer" value="12"></constructor-arg>
<constructor-arg type="java.lang.String" value="dyh"></constructor-arg>
<constructor-arg type="java.lang.String" value="濮阳"></constructor-arg>
</bean>
3、测试
import com.du.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void Test(){
//此时调用的无参构造
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//在执行getBean的时候, user已经创建好了 , 通过无参构造
User user = context.getBean("user",User.class);
//调用对象的方法
user.show();
}
}
结论:在配置文件加载的时候。其中管理的对象都已经初始化了!
三:Spring配置
1.别名
alias 设置别名 , 为bean设置别名 , 可以设置多个别名
<bean id="user" class="com.du.pojo.User">
<constructor-arg name="name" value="dyh"></constructor-arg>
<constructor-arg name="address" value="濮阳"></constructor-arg>
<constructor-arg name="age" value="12"></constructor-arg>
</bean>
<alias name="user" alias="userNew"/>
User user = context.getBean("userNew",User.class);
2.Bean的配置
<!--bean就是java对象,由Spring创建和管理-->
<!--
id 是bean的标识符,要唯一,如果没有配置id,name就是默认标识符
如果配置id,又配置了name,那么name是别名
name可以设置多个别名,可以用逗号,分号,空格隔开
如果不配置id和name,可以根据applicationContext.getBean(.class)获取对象;
class是bean的全限定名=包名+类名
-->
<bean id="hello" name="hello2 h2,h3;h4" class="com.kuang.pojo.Hello">
<property name="name" value="Spring"/>
</bean>
User user = context.getBean("user1",User.class);
3.import
团队的合作通过import来实现 .
全部打入到主xml文件
<import resource="{path}/beans.xml"/>