spring入门1-IOC和DI

1.概述

1.1.简介

Spring是分层的 Java SE/EE应用 full-stack 轻量级开源框架,以 IoC(Inverse Of Control:反转控制)和 AOP(Aspect Oriented Programming:面向切面编程)为内核。
提供了展现层 SpringMVC和持久层 Spring JDBCTemplate以及业务层事务管理等众多的企业级应用技术,还能整合开源世界众多著名的第三方框架和类库,逐渐成为使用最多的Java EE 企业应用开源框架

1.2.优势

  • 方便解耦,简化java开发
  • aop编程支持
  • 声明式事务支持
  • 方便程序测试

2.快速入门

2.1.导入spring开发包坐标

<dependencies>
     <!-- spring基本包,包含core,beans,aop-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.5.RELEASE</version>
    </dependency>
    <!--spring测试整合包-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <!-- aspectj的织入,AOP开发需要 -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.13</version>
    </dependency>
</dependencies>

2.2.创建spring核心配置文件

  • 一般创建在resources目录下,文件名为applicationContext.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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>

2.3.创建java类

public class A{}

2.4.在spring配置文件中配置被管理的bean

<bean id="classA" class="A"></bean>

2.5.使用spring api获取bean实例

public class MyTest {
    @Test
    public void t01(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        A a = context.getBean(A.class);
        System.out.println(a!=null);
    }
}

3.使用xml装配bean

3.1.bean标签

作用: 用于配置对象由spring创建,默认使用无参构造.
属性:
  * id,bean实例在spring容器中唯一标识
  * class,bean全限定类名
  * scope,指定该实例的作用范围
  * init-method,指定bean实例化后执行的方法,值为bean类中方法名
  * dmestroy-method,指定bean销毁前执行的方法
scope属性取值:
  * singleton,默认值,单例模式,spring容器创建时实例化bean,存活至spring容器销毁.
  * prototypem,多例模式,每次调用getBean()都会实例化bean,使用时存货,长时间不使用由java垃圾回收器回收
  * request,在web项目中,每被请求一次实例化一次,放入request域中
  * session
  * global session

3.2.bean实例化配置

3.2.1.java

public class A {}
class AFactory{
    // 静态方法返回bean
    public static A getA1(){
        return new A();
    }
    // 非静态方法返回bean
    public A getA2(){
        return new A();
    }
}

3.2.2.spring配置

a.无参构造实例化

<bean id="a1" class="A"/>

b.工厂静态方法实例化

<bean id="a2" factory-method="getA1" class="AFactory"/>

c.工厂实例方法实例化

<bean id="afactory" class="AFactory"/>
<bean id="a3" factory-bean="afactory" factory-method="getA2"/>

3.3.bean依赖注入配置

3.3.1.有参构造

a.注入基本类型和string

<bean id="" class="">
    <!--  name:构造方法形参的名字 value:要注入的值 -->
    <constructor-arg name="" value=""/>
</bean> 

b.注入bean类型

<bean id="" class="">
    <!--  name:构造方法形参的名字 ref:bean实例id -->
    <constructor-arg name="" ref=""></constructor-arg>
</bean>

c.注入list<String>

<bean id="" class="">
    <!--  name:构造方法形参的名字 -->
    <constructor-arg name="">
        <list>
            <value>张三</value>
            <value>李四</value>
        </list>
    </constructor-arg>
</bean>

d.注入list<bean>

<bean id="" class="">
    <!--  name:构造方法形参的名字 -->
    <constructor-arg name="">
        <list>
            <!-- ref 指定容器中的bean实例-->
            <ref bean=""></ref>
            <!-- bean 新建一个bean -->
            <bean class=""></bean>
        </list>
    </constructor-arg>
</bean>

e.注入map<string,string>

<bean id="" class="">
    <constructor-arg name="map">
        <map>
            <!-- key注入键 value注入值 -->
            <entry key="张三" value="25"></entry>
            <entry key="李四" value="19"></entry>
        </map>
    </constructor-arg>
</bean>

f.注入map<string,bean>

<bean id="b4" class="B">
    <constructor-arg name="map">
        <map>
            <!-- key注入键 value-ref指定bean实例id -->
            <entry key="张三" value-ref=""></entry>
            <entry key="李四" value-ref=""></entry>
        </map>
    </constructor-arg>
</bean>

3.3.2.set方法

a.注入基本类型和string类型

<bean id="" class="">
    <!-- name:set方法名   value:注入的值-->
    <property name="" value=""/>
</bean>

b.注入bean

<bean id="" class="">
     <!-- name:set方法名    ref:指定bean实例id-->
    <property name="a" ref="a1"></property>
</bean>

c.注入list<string>

<bean id="" class="">
    <!-- name set方法名-->
    <property name="">
        <list>
            <value>str1</value>
            <value>str2</value>
        </list>
    </property>
</bean>

d.其他集合参考3.3.1

3.3.3.p名称空间

  • 本质是调用set方法
  • 使用前需要引入约束
xmlns:p="http://www.springframework.org/schema/p"

a.注入基本类型和string

<bean id="" class="" p:[set方法名]="值" ></bean>

b.注入bean

<bean id="" class="" p:[set方法名]-ref="bean实例id" ></bean>

3.4.引入其他配置

  • 实际开发中,Spring的配置内容非常多,这就导致Spring配置很繁杂且体积很大,所以,可以将部分配置拆解到其他配置文件中,而在Spring主配置文件通过import标签进行加载
<import resource="applicationContext-xxx.xml"/>

3.5.引入资源文件

a.引入properties文件

<context:property-placeholder location="classpath:xxx.properties"/>

b.在配置文件获取值.格式:${key}

<property name="username" value="${key}">

4.使用注解装配bean

4.1.在java代码中使用注解

注解 说明
@Component 实例化Bean,使用在类上,默认id为类名首字母小写
@Controller 实例化Bean,使用在web层类上
@Service 实例化Bean,使用在service层类上
@Repository 实例化Bean,使用在dao层类上
@Autowired 依赖注入,使用在字段上根据类型注入
@Qualifier 依赖注入,结合@Autowired一起使用根据id注入
@Resource 依赖注入,相当于@Autowired+@Qualifier
@Value 依赖注入,注入普通属性
@Scope 标注Bean的作用范围
@PostConstruct 使用在方法上标注该方法是Bean的初始化方法
@PreDestroy 使用在方法上标注该方法是Bean的销毁方法

4.2.开启注解扫描

<context:component-scan base-package="要扫描的包"></context:component-scan>

5.javabean+注解代替xml配置

注解 说明
@Configuration 用于指定当前类是一个 Spring 配置类,当创建容器时会从该类上加载注解
@ComponentScan 用于指定 Spring 在初始化容器时要扫描的包。作用等于 Spring 的 xml 配置文件中的 <context:component-scan/>
@Bean 使用在方法上,标注将该方法的返回值存储到Spring容器中,id为方法名
@PropertySource 用于加载.properties 文件中的配置
@Import 用于导入其他配置类

6.spring相关api

  • 读取xml配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("xmlconfig.xml");
  • 读取javaConfig
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(javaconfig.class);
  • getBen()
// 根据id获取bean需要强转类型
applicationContext.getBean("");
// 根据class获取bean不用强转类型,但该类在容器中需是单例的
applicationContext.getBean(javabean.class);
posted @ 2020-07-17 13:56  xia-qaq  阅读(109)  评论(0编辑  收藏  举报