【Spring】Spring狂神说笔记

简介

文档

Spring Framework 中文文档

Spring Framework 英文文档

导入依赖

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.9</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.3.9</version>
</dependency>


优点

  1. Spring是一个开源免费的框架
  2. Spring是一个轻量级非入侵式的框架
  3. 控制翻转 IOC、面向切面编程AOP
  4. 支持事务处理,对框架整合的支持

缺点

  1. 由于过度集成,配置十分繁琐

Spring是一个轻量级的面向切面、控制翻转的框架

组成

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Dfj8RY1G-1645076688557)(figures/Spring/image-20210815125152603.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lkhbuQyK-1645076688558)(figures/Spring/image-20210815125307226.png)]

IOC

​ 略

第一个Spring程序

源文件

package com.rjx.pojo;

import lombok.Data;

@Data
public class Hello {
    private String str;
}

配置文件

<?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代表一个对象-->
    <bean id="hello" class="com.rjx.pojo.Hello">
        <property name="str" value="Spring"/>
    </bean>


    <!-- more bean definitions go here -->

</beans>

测试文件

package com.rjx.pojo;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloTest {
    @Test
    public void test1(){
        //获取Spring的上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Hello hello = (Hello)context.getBean("hello");
        System.out.println("hello");
    }
}

IOC创建对象的方式

  1. 使用无参构造方法创建对象 默认方法
  2. 使用有参构造,但此时需要在配置中声明

Spring配置

别名

<alias name="origin name" alias="alias"/>

Bean配置

  1. id:bean的唯一标识符
  2. class:bean对象的类名
  3. name:相当于id的别名 可以通过逗号、空格、分号等符号分割取多个别名

作用域

  1. 按对象个数分类
    1. singleton:全局唯一 默认为单例
    2. prototype:每个原型一个
  2. MVC中

Import

​ 一般用于团队开发,可以将多个配置文件导入合并为一个配置文件

语法

<import resource="resource path"/>

依赖注入

实体类

package com.rjx.pojo;

import lombok.Data;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

@Data
public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbies;
    private Map<String,String> card;
    private Set<String> games;
    private Properties info;
    private String wife;
}

package com.rjx.pojo;

import lombok.Data;

@Data
public class Address {
    private String address;

}

测试类

    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student);
    }

构造器注入

​ 见第一个Spring程序

注入方式

值注入

配置文件
        <property name="name" value="passnight"/>

Bean注入

配置文件
    <bean id="address" class="com.rjx.pojo.Address">
        
    </bean>

        <property name="address" ref="address"/>

数组注入

配置文件
        <property name="books">
            <array>
                <value>book1</value>
                <value>book2</value>
                <value>book3</value>
                <value>book4</value>
            </array>
        </property>

List注入

配置文件
        <property name="hobbies">
            <list>
                <value>hobby 1</value>
                <value>hobby 2</value>
                <value>hobby 3</value>
                <value>hobby 4</value>
                <value>hobby 5</value>
            </list>
        </property>

字典注入

配置文件
        <property name="card">
            <map>
                <entry key="card-key1" value="card-value1"/>
                <entry key="card-key2" value="card-value2"/>
                <entry key="card-key3" value="card-value3"/>
                <entry key="card-key4" value="card-value4"/>
            </map>
        </property>

集合注入

配置文件
        <property name="games">
            <set>
                <value>game1</value>
                <value>game2</value>
                <value>game3</value>
                <value>game4</value>
            </set>
        </property>

空值注入

配置文件
        <property name="wife">
            <null/>
        </property>

properties注入

配置文件
        <property name="info">
            <props>
                <prop key="key1">value1</prop>
                <prop key="key2">value2</prop>
                <prop key="key3">value3</prop>
                <prop key="key4">value4</prop>
            </props>
        </property>

c命名注入

通过构造器注入

<?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:P="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 一个bean代表一个对象-->
    <bean id="user" class="com.rjx.pojo.User" C:age="18" C:name="name"/>
<!-- more bean definitions go here -->

</beans>

p命名注入

<?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:P="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 一个bean代表一个对象-->
<bean id="user" class="com.rjx.pojo.User" P:age="18" P:name="name"/>


<!-- more bean definitions go here -->

</beans>

Bean自动装配

  1. 自动装配是Spring满足bean以来的一种方式
  2. Spring会在上下文寻找,并给bean装配属性

三种装配方式

  1. 在xml中显示配置
  2. 在java中显示配置
  3. 隐式自动装配

非自动装配

package com.rjx.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
public class Dog {
    public void shout(){
        System.out.println("a dog shout");
    }
}

package com.rjx.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
public class Cat {
    public void shout(){
        System.out.println("a cat shout");
    }
}
package com.rjx.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
    private Cat cat;
    private Dog dog;
    private String name;
}
<?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="cat" class="com.rjx.pojo.Cat"/>
    <bean id="dog" class="com.rjx.pojo.Dog"/>
    <bean id="person" class="com.rjx.pojo.Person">
        <property name="cat" ref="cat"/>
        <property name="dog" ref="dog"/>
        <property name="name" value="name"/>
    </bean>
</beans>
@Test
public void test3(){
        ApplicationContext context = new ClassPathXmlApplicationContext("animals.xml");
        Person person = context.getBean("person", Person.class);
        System.out.println(person);
        person.getCat().shout();
        person.getDog().shout();
}

自动装配

By Name自动装配
<?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="cat" class="com.rjx.pojo.Cat"/>
    <bean id="dog" class="com.rjx.pojo.Dog"/>
    <bean id="person" class="com.rjx.pojo.Person" autowire="byName">
        <property name="cat" ref="cat"/>
    </bean>
</beans>
By Type自动装配
<?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="cat" class="com.rjx.pojo.Cat"/>
    <bean id="dog" class="com.rjx.pojo.Dog"/>
    <bean id="person" class="com.rjx.pojo.Person" autowire="byType">
        <property name="cat" ref="cat"/>
    </bean>
</beans>
使用注解自动装配
前提
  1. 导入约束
  2. 配置注解支持
使用
package com.rjx.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;
}

<?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:annotation-config/>
    <bean id="dog" class="com.rjx.pojo.Dog"/>
    <bean id="cat" class="com.rjx.pojo.Cat"/>
    <bean id="person" class="com.rjx.pojo.Person"/>
</beans>

@AutoWired可以忽略set方法使用,也可以使用在set方法上, 但只能通过byname实现

找不到name, 可以使用@Qualifier()指定装配id

也可以使用@Resource(name=“name value”, 且同时可以byname和bytype)

使用注解开发

自动装配注解

配置

<?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:annotation-config/>
<!--    指定扫描这个包,这个包下的组件就会生效(@Component自动生效)-->
    <context:component-scan base-package="com.rjx.pojo"/>

</beans>

@Data
//相当于<bean id="小写" class="反射获取"/>
@Component
//设置作用域
@Scope("singleton")
public class User {
    //相当于bean下的property下的value
    @Value("passnight")
    private String name;
}

测试

package com.rjx.pojo;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserTest {
    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = context.getBean("user", User.class);

        System.out.println(user);
    }
}

Dao层的通常使用@Repository代替@Component

Service层的通常使用@Service代替@Component

Controller层的通常使用@Controller代替@Component

基于java的开发

配置

package com.rjx.config;

import com.rjx.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Config {
    @Bean
    public User getUser(){
        return new User();
    }
}

源文件

package com.rjx.pojo;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Data
@Component
public class User {
    @Value("passnight")
    private String name;
}

测试文件

@Test
public void test1(){
    ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
    System.out.println(context.getBean("getUser"));
}

bean id 与配置文件当中@Bean的方法名相同

相关参数

@ComponentScan("path") 相当于<context:component-scan base-package="path"/>
@Import(Config.class) 相当于    <import resource="c.xml"/>

ringframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Data
@Component
public class User {
@Value(“passnight”)
private String name;
}


## 测试文件

```java
@Test
public void test1(){
    ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
    System.out.println(context.getBean("getUser"));
}

bean id 与配置文件当中@Bean的方法名相同

相关参数

@ComponentScan("path") 相当于<context:component-scan base-package="path"/>
@Import(Config.class) 相当于    <import resource="c.xml"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 很抱歉,我是AI语言模型,无法回答关于“狂神说springsecurity笔记”的问题。但我可以告诉你,Spring Security是一个基于Spring框架的安全框架,用于保护Web应用程序。它提供了身份验证、授权、攻击防护等功能,可以帮助开发人员构建安全可靠的Web应用程序。狂神是一位知名的Java技术博主,他在自己的博客和视频中分享了很多关于Spring Security的知识和经验。如果你想学习Spring Security,可以参考他的笔记和教程。 ### 回答2: Spring Security 是一个强大的开源安全框架,用于保护 Spring 应用程序。它提供了一系列的安全服务,例如身份验证(Authentication)、授权(Authorization)、密码加密、会话管理等。 狂神在他的 Spring Security 笔记中详细介绍了该框架的使用方式和相关概念。首先,他解释了身份验证的重要性,提及了不同的身份验证方法,如基于表单的认证、基于记住我(Remember Me)的认证以及基于 HTTP Basic 和 Digest 验证等。他还介绍了如何自定义认证过程和处理认证失败的方法。 其次,狂神介绍了授权的概念和不同的授权方式。他解释了基于角色(Role)的访问控制和基于资源(Resource)的访问控制,并示范了如何在 Spring Security 中实现这些授权方式。他还介绍了如何使用注解和表达式来简化授权配置。 此外,狂神还介绍了 Spring Security 提供的其他功能,如密码加密、会话管理、防止跨站点请求伪造(CSRF)攻击等。他指导读者如何使用这些功能来增强应用程序的安全性。 总的来说,狂神的 Spring Security 笔记对于初学者来说是一个很好的入门指南。他提供了丰富的示例代码和详细解释,让读者可以很容易地理解和使用 Spring Security。无论是开发 Web 应用程序还是企业级应用程序,掌握 Spring Security 都是非常重要的,狂神的笔记提供了一个很好的起点。 ### 回答3: spring security 是一个开源的安全框架,可以为应用程序提供身份验证和授权管理的功能。它基于 JavaEE 标准认证和授权机制,并提供了一套更灵活的安全性管理机制。 首先,Spring Security 实现了用户认证的功能,可以通过多种认证方式来验证用户的身份,包括基于数据库的认证、LDAP 认证、OAuth2 认证等。可以根据具体的需求选择使用不同的认证方式。 其次,Spring Security 提供了授权管理的功能,可以设置不同的权限角色,对不同的用户进行授权。可以通过配置的方式进行权限的控制,通过注解或者编程的方式进行细粒度的控制,可以灵活地满足不同的应用需求。 另外,Spring Security 还提供了许多其他的安全支持功能,如防止 CSRF 攻击、防止点击劫持、防止 SQL 注入等。它还支持与其他框架的集成,如与 Spring Boot、Spring MVC 的集成,可以方便地在现有的应用中集成安全功能。 Spring Security 的架构清晰,易于理解和使用。它的设计模式和扩展机制可以满足不同级别的定制需求,使得我们可以根据实际情况来灵活地进行使用和扩展。同时,Spring Security 还提供了丰富的文档和示例代码,方便开发者学习和使用。 总之,Spring Security 是一个功能强大且灵活的安全框架,可以在应用程序中实现身份验证和授权管理的功能。通过使用 Spring Security,我们可以有效地保护和管理我们的应用程序,提升系统的安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pass night

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值