java Spring课堂笔记

导入jdbc jar包
spring:导入jar包

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.18</version>
</dependency>
<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.18</version>
</dependency>
spring优点:
1.spring是一个开源的免费框架(容器)!
2.spring是一个轻量级的,非入侵式的框架。
3.控制反转(ioc),面向切面编程(aop)。
4.支持事务的处理,对框架整合的支持。
spring就是一个轻量级的控制反转(ioc)和面向切面编程(aop)的框架!

spring boot:  
   一个快速开发的脚手架
   基于springboot可以快速的开发单个微服务
   约定大于配置
spring cloud
   springboot的是基于springboot实现的

因为现在大多数公司都在使用spring boot快速开发,学习spring boot的
前提,需要完全掌握spring及springMVC承上启下作用。

spring弊端:发展太久配置太多。

暂时不知道有什么用(配置输出)
 
<build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
springbeans配置文件
<?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-3.0.xsd">
</beans>

//通过xml加载就必须写这句话
      ApplicationContext context = new ClassPathXmlApplicationContext();

!注意千万不要在spring pon.xml里乱导包,不然可能会出现问题

4.创建对象的方式
   1.使用无参构造创建对象,默认!
   2.假设我们要使用有参构造创建对象。
      1.下标复制
       <constructor-arg index="0" value="鲁家见"/>
      2.通过类型不建议使用
      <constructor-arg type="java.lang.String" value="你好啊"/>
      3.通过参数名创建对象重点掌握
      <constructor-arg name="name" value="这是通过参数名直接赋值"/>
总结:在配置文件加载的时候,容器中管理的对象就已经初始化了。

5.spring配置
   5.1别名
   <alias name="student" alias="Text1"/>
   通过name也可以取别名,而且还可以取多个别名用逗号分隔
   <bean id="student" class="pojo.Student" name="s1">
   5.2bean的配置

   5.3import
   这个import,一般用于团队开发使用,他可以将多个配置文件,导入
   合并为一个。
   假设,现在项目中有多个人开发,这几个人复制不同的类开发,不同类需要
   注册在不同的bean中,我们可以利用import将所有人的beans.xml合并为一个
   总的。使用的时候,直接使用总的配置就可以了。
   <import resource="beans.xml"/>
        <import resource="beans.xml"/>
        <import resource="beans.xml"/>
6.依赖注入
   6.1构造器注入
   前面说过了
   6.2set方式注入(重点)
   依赖注入
      依赖:bean对象的创建依赖于容器
      注入:bean对象中的所以属性,用容器来注入
   环境搭建
   1.复杂类型
   2.真实测试对象
   6.3其他方式注入
 
<bean id="address" class="pojo.Address"/>
    <bean id="student" class="pojo.Student">
        <!--第一种普通注入value-->
        <property name="name" value="张三"/>
        <!--第二种bean注入ref-->
        <property name="address" ref="address"/>
        <!--数组注入-->
        <property name="books">
            <array>
                <value>平凡的世界</value>
                <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="34242342423423"/>
                <entry key="结果" value="借的"/>
            </map>
        </property>
        <!--set集合-->
        <property name="games">
            <set>
                <value>英雄联盟</value>
                <value>穿越火线</value>
                <value>王者荣耀</value>
            </set>
        </property>
        <!--空值注入null-->
        <property name="wife">
            <null/>
        </property>
        <!--配置文件注入Properties-->
        <property name="info">
            <props>
                <prop key="driver">2019502</prop>
                <prop key="url">男</prop>
                <prop key="username">root</prop>
                <prop key="password">root</prop>
            </props>
        </property>
    </bean>
c和p命名空间注入
p命名就是set方式注入,需要无参构造和set方法,
c命名就是构造器注入,需要有参构造
p和c不能直接使用要导入约束xml文件
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
6.4bean的作用域
   1.单列模式spring默认单例模式
   User bean = (User) context.getBean("user1");
           User bean1 = (User) context.getBean("user1");
           System.out.println(bean==bean1);为真
   2.原型模式每次从容器中get的时候,都会产生一个新对象
      <bean name="user1" class="pojo.User" c:name="鲁家见" c:age="2639" scope="prototype"/>
   3.其余的request,session,application,这些个只能在web开发中使用到。
7.bean的自动装配
   1.自动装配是spring满足bean依赖一种方式
   2.spring会在上下文中自动寻找,并自动给bean装配属性

在spring中有三种装配的方式
   1.在xml中显示的配置
   2.在java中显示配置
   3.隐式的自动装配bean(重要)
7.1测试
   搭建环境(一个人两个宠物)
7.2byName自动装配
   byname会自动在容器上下文中查找,和自己对象set方法后面的值对应
   的beanid!
  
 <bean id="poto" class="pojo.PoTo" autowire="byName">
         <property name="name" value="你是那位"/>
 </bean>
7.3ByType自动装配
   byType会自动在容器上下文中查找,和自己对象属性类型相同的bean
   小结:
   byName的时候,需要保证所以bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致
   byType的时候,需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致
7.4使用注解实现自动装配
   使用注解需知:
      1.导入约束:context约束
      2.配置注解的支持:<context:annotation-config/> 
      3.添加了context命名空间和约束的配置文件

<?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>
@Autowired
直接在属性上使用即可!也可以在set方式上使用!
使用Autowired我们可以不要编写set方法了,前提是你这个自动装配属性在ioc(spring)容器中
存在,且符合名字byName

如果显示定义了Autowired的required属性false说明这个对象可以为null,负责不允许为空

如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解(@Autowired)完成的时候,
我们可以使用@Qualifier(value = "")去配置@Autowired的使用,指定一个唯一的bean对象注入!

小结:
   1.都是用来自动装配的,都可以放在属性字段上
   2.@Autowired默认用byType,找不到再用byName
   3.@Resource默认通过byname的方式实现,如果找不到名字,则通过bytype实现!如果
      两个都找不到的情况下,就报错
使用@Resource需要导入
  
 <dependency>
             <groupId>javax.annotation</groupId>
             <artifactId>javax.annotation-api</artifactId>
             <version>1.2</version>
   </dependency>
8.使用注解开发
在spring4之后使用注解开发,必须保证aop包导入了

<!--指定要扫描的包这个包下的注解就会生效-->
    <context:component-scan base-package="pojo" />

//<bean name="student" class="pojo.Student"/>等价于@Component说明此类被spring管理了

@value("你是那位")相当于<property name="name" value="你是那位"/>

3.衍生的注解
    @Component有几个衍生注解,在我们web开发中,会按照mvc三层架构分层
        dao(@Repository)
        service(@Service)
        controller(@Controller)
        这四个注解的功能都是一样的,都代表将某个类注册spring中,装配到bean中

9.动态代理
需要了解两个类:proxy:代理  InvocationHandler:调用处理程序

自动生成代理工具类

public class PIH implements InvocationHandler {
    //被代理的类
    private Object target;

    public void setTarget(Object target) {
        this.target = target;
    }
    //生成得到代理类
    public Object getProxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
    }

    @Override//处理代理实例并返回结果
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object invoke = method.invoke(target,args);
        return invoke;
    }
}
10.Spring AOP
    使用spring AOP 要导入
   
 <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjlib</artifactId>
        <version>1.6.2</version>
     </dependency>
配置aop导入aop约束
    
<?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:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
        <bean id="studentServiceImp" class="service.StudentServiceImp"/>
    </beans>
11.整合mybatis
    spring连接数据库要用的包 



 <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.13.2</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.28</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.7</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>5.3.18</version>
            </dependency>
            <!--使用spring连接数据库的话还需要一个spring-jdbc-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>5.3.18</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>2.0.7</version>
            </dependency>
        </dependencies>

​​​​​​​

<?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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
        <!--dataSource使用spring的数据源替换mybatis的配置-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/test?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>
    <!--SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--绑定mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="dao/*.xml"/>
    </bean>
    <!--sqlSession这个就是SqlSessionTemplate-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--只能使用构造器注入sqlSessionFactory,因为他没有set方法-->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

    <bean id="studentMapper" class="dao.StudentMapperImpl">
        <property name="sqlSession" ref="sqlSession"/>
    </bean>

</beans>
Spring中的事务管理
    声明式事务aop
    编程式事务,需要在代码中进行事务的管理
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值