入门Spring(上)

一、Spring理念

Spring官网连接:https://docs.spring.io/spring-framework/docs/5.2.0.RELEASE/spring-framework-reference/core.html#beans

  • Spring的理念是使现有的技术更容易使用,整合了现有的技术框架

  • SSH:Struct2+Spring+Hibernate(早期的JAVA开发)

  • SSM:SpringMVC+Spring+Mybatis

  • Spring是一个开源的,非侵入式的,控制反转(IOC)和面向切面(AOP)的框架(容器)
    拓展springboot
    springboot与maven一样,约定大于配置,基于springboot可以开发单个微服务模块

二、IOC

1.IOC原型

每一次因为用户的请求,我们都需要手动修改源代码(还需修改service层),程序适应不了用户需求的变更,如果程序代码量十分大,那修改一次代码的成本代价十分昂贵,使用set注入,将程序的主动创建对象由程序员(new)交到了客户的手上(set),因此程序员可以专注扩展业务层了。
封装(属性私有,get/set)是IOC思想的体现

2.IOC本质

  • 控制反转是一种设计思想,原来创建对象由程序员自己new,现在是spring容器帮我们new,将对象创建的权力交给了spring容器,对象都交给Spring管理了,实现了解耦。
    beans.xml
 <bean id="user" class="com.sun.pojo.User">//在xml文件中,用spring来创建对象,相当与User user=new User();
        <property name="name" value="呵呵呵呵呵呵"></property>//给属性设置值,也是交给spring
    </bean>

测试类

 //拿到spring容器
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //从容器里getuser
        User user = (User) context.getBean("user");
        System.out.println(user);

只要在beans.xml里注册了bean就交给spring托管了
这个过程就叫控制反转:

  • 传统的对象由程序本身创建的,而现在由spring容器来创建
  • 装配值也交给了spring
  • 总结一句话:对象交给spring来创建,管理,装配

3.IOC创建对象bean的方式

  • 使用无参构造创建,set方法注入(默认)
  • 使用有参构造构造(下标赋值,类型赋值,参数名赋值)
//下标赋值
 <bean id="user" class="com.sun.pojo.User">
        <constructor-arg index="0" value="dfghjksdfg呵呵呵"></constructor-arg>
    </bean>
//类型赋值
 <bean id="user" class="com.sun.pojo.User">
        <constructor-arg type="java.lang.String" value="dfghjksdfg呵呵呵"></constructor-arg>
    </bean>
//参数名赋值
 <bean id="user" class="com.sun.pojo.User">
        <constructor-arg name="name" value="dfghjksdfg呵呵呵"></constructor-arg>
    </bean>
  • 使用@Bean注解

4.配置文件import

import一般适用于合作开发,将多个配置文件放到一个里面,但是配置文件名不可以重名。

    //总的配置文件ApplicationContext.xml
    <import resource="beans1.xml"/>
    <import resource="beans2.xml"/>

三、Spring的七大模块

在这里插入图片描述
Spring Core:Spring框架的核心容器,这个模块中最主要的组件是Bean工厂,用工厂模式来创建对象。
Spring Web:集成了一些web框架。
Spring AOP:面向切面编程
Spring Dao:对JDBC和Dao的支持

四、DI注入方式

1.set注入

  1. 依赖注入本质是set注入
  2. 依赖:bean的创建都依赖于spring容器
  3. 注入:bean中的属性都由spring容器注入
  4. 必须要有set和有参构造
    不同类型的属性注入,会有一个外部标签property
  <bean id="address" class="com.sun.pojo.Address">
        <property name="address" value="北京市朝阳区清河街道101室"></property>
    </bean>
    
    <bean id="student" class="com.sun.pojo.Student">
        <!--常量值注入value-->
        <property name="name" value="siro"/>
        <!--引用注入ref-->
        <property name="address" ref="address"></property>
       
        <!--数组注入-->
        <property name="books">
            <array>
                <value>三国</value>
                <value>红楼</value>
                <value>西游记</value>
            </array>
        </property>
        
        <!--List注入-->
        <property name="hobbys">
            <list>
                <value>打球</value>
                <value>游泳</value>
                <value>敲代码</value>
            </list>
        </property>
        
        <!--map注入-->
        <property name="card">
            <map>
                <entry key="身份证" value="1236595623956"></entry>
                <entry key="手机号" value="45128451284561285"></entry>
            </map>
        </property>
        
        <!--set注入-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>COC</value>
                <value>BOB</value>
            </set>
        </property>
        
        <!--空值注入-->
        <property name="wife">
            <null/>
        </property>
        
      <!-- Properties特殊类型注入-->
        <property name="info">
            <props>
                <prop key="学号">20180002</prop>
                <prop key="性别"></prop>
                <prop key="姓名">小明</prop>
            </props>
        </property>
    </bean>

2.构造器注入

见IOC创建对象的方式
必须有有参构造器

3.p.c命名空间注入

  • 官网截图

在这里插入图片描述

  • xml配置文件
 <!--p相当于properties,相当于set注入-->
    <bean id="user" class="com.sun.pojo.User" p:name="小孙孙" p:age="56"/>
    <!--c相当于有参构造器注入-->
    <bean id="user" class="com.sun.pojo.User" c:name="小筛子" c:age="12345"></bean>
  • 测试类
 @Test
    public void test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
        User user = (User) context.getBean("user");
        System.out.println(user);
    }

命名空间一定要引入第三方命名约束才能使用
xmlns:p=“http://www.springframework.org/schema/p”
xmlns:c=“http://www.springframework.org/schema/c”

五、bean的作用域

官网截图(6种)
在这里插入图片描述

  1. spring默认是单例模式singleton,每次从容器中getbean的时候都是原来的对象,一直被复用到应用结束。

     User user = (User) context.getBean("user");
     User user2 = (User) context.getBean("user");
     System.out.println(user==user2);//结果为true
    

    在这里插入图片描述

  2. 原型模式 propotype ,每次都容器中getbean的是都是新的对象
    在这里插入图片描述

     User user = (User) context.getBean("user");
     User user2 = (User) context.getBean("user");
    System.out.println(user==user2);//结果为false
    

六、spring的3种装配方式

6.1 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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>

6.2 JavaConfig配置

完全不再使用spring的xml配置了,全权交给java来做
见7.2JavaConfig

6.3 自动装配bean(xml)

//byName:在spring上下文自动查找与set方法后面值对应的beanid,名字一定要唯一
//byType:在spring上下文自动查找与对象属性类型相同的class,类型一定要唯一
 <bean id="cat" class="com.sun.pojo.Cat"></bean>
 <bean id="dog" class="com.sun.pojo.Dog"></bean>
 <bean id="people" class="com.sun.pojo.People" autowire="byName"></bean>

6.4 自动装配bean(注解)

前提:
1.在xml文件中导入第三方context约束 xmlns:context=“http://www.springframework.org/schema/context”
2.开启注解支持 <context:annotation-config/>
3.注解扫描包<context:component-scan base-package=“com.sun”/>

  • @AutoWired是用来自动装配的,用在属性字段上,通过byType来实现的
  • @Qualifier(value=“小蜜瓜”)与@AutoWired是配合使用的
  • @Resources(name=“小蜜瓜”)是用来自动装配的,先通过byName再通过ByType来实现。

七、注解开发

  • @Component:放在类上,说明这个类被spring管理了,相当于注册bean标签,只能在类上写一次
  • @Value(“小明”):bean中属性注入
  • @Repository,@Service,@Controller都是@Component的衍生注解,跟@Component一个意思,都是把类注册到spring里面,相当于一个bean
  • @ Configuration交给spring托管,相当于beans.xml
  • @bean一般用在方法上,相当于之前的注册的bean标签,可以有多个bean
  • @Import(MyConfig.class)是将一个配置类导入到这个配置类中
  • 所以一般,@Component+@Value @Configuration+@bean
  • @Qualifier("")是spring无法判断装配哪个bean,因此需要指定,一般与@AutoWired一起使用
//在测试类中
@SpringBootTest
class TestdemoApplicationTests {
    @Autowired
    //当有多个bean时,指定装配bean
    @Qualifier("person")
    Person person;
    @Test
    void contextLoads() {
        System.out.println("嘿嘿"+person);
    }
}

7.1 非零xml配置

因为还没有完全脱离xml配置文件,因此在test测试类中加载配置文件
ApplicationContext context = new ClassPathXmlApplicationContext(“applicationContext.xml”);

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"
       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="com.sun"/>
    <!--注解驱动-->
    <context:annotation-config/>
</beans>

pojo包下的user类

@Component
//相当于<bean id="name" class="com.sun.pojo">
public class User {
    @Value("小孙孙")
//@Value相当于<property name="name" value="小孙孙"/>
    public String name;
}

Dao包下的UserDao

@Repository//@Component的衍生注解,跟@Component的作用一样,都是注册到spring里面,相当于一个bean
public class UserDao {
}

7.2 零xml配置(JavaConfig)

已经完全脱离了xml配置文件,
ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
User user = (User) context.getBean(“user”);//这个user是@bean下的方法名

根据官网的例子

@Configuration//等价于beans
//扫描包
@ComponentScan("com.sun")
//引入另一个配置文件
@Import(MyConfig2.class)
public class MyConfig {

    @Bean//等价于
    //方法名相当于id
    //方法的返回值相当于class
    public User user(){
        return new User();
    }
}

八、spring中bean的线程安全问题

spring中bean默认是单例模式的,IOC容器一个类只会存一个对象,那如何保证线程是安全的?

  • 在spring中,bean都是无状态的(无状态就是自身没有状态的对象,因此在线程交替时就不会破坏自身的状态),只要是无状态对象,不管单例原型线程都是安全。
  • 无状态的bean适合单例模式,有状态的bean多线程下不安全,适合原型模式。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值