springboot小结

参考官网:https://spring.io/projects/spring-boot/

参考自己博客:https://blog.csdn.net/zyzn1425077119/article/details/102933405

https://blog.csdn.net/yestar123456/article/details/103716161

https://www.jianshu.com/p/41b7c3fb00e0

 

@Autowired
private StringRedisTemplate stringRedisTemplate;

@Resource(name="stringRedisTemplate")
private ValueOperations<String,String> valOpsStr;

@Autowired
private RedisTemplate<Object, Object> redisTemplate;

@Resource(name="redisTemplate")
private ValueOperations<Object,Object> valOps;

 

1、官网demo  springboot+maven

 配置在src/main/resources。 

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

@SpringBootApplication:申明让spring boot自动给程序进行必要的配置,这个配置等同于:

@Configuration ,@EnableAutoConfiguration 和 @ComponentScan 三个配置。

 

@Repository 只能标注在 DAO 类,该注解的作用不只是将类识别为Bean,同时它还能将所标注的类中抛出的数据访问异常封装为 Spring 的数据访问异常类型。 
@Component 是一个泛化的概念,仅仅表示一个组件 (Bean) ,可以作用在任何层次。
@Service 通常作用在业务层,但是目前该功能与 @Component 相同。
@Constroller 通常作用在控制层,但是目前该功能与 @Component 相同。
 

注解实例:

指示一个类声明一个或多个@Bean方法,并且可以由Spring容器处理,以便在运行时为这些bean生成BeanDefinition和服务请求

一、@Configuration:

指示一个类声明一个或多个@Bean方法,并且可以由Spring容器处理,以便在运行时为这些bean生成BeanDefinition和服务请求

@Configuration
public class AppConfig {

    @Bean
    public MyBean myBean() {
        // instantiate, configure and return bean ...
    }
}

 

二、@EnableWebSecurity

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({WebSecurityConfiguration.class//1, SpringWebMvcImportSelector.class, OAuth2ImportSelector.class})
@EnableGlobalAuthentication //2
@Configuration
public @interface EnableWebSecurity {
    boolean debug() default false;
}

首先,在1处激活了WebSecurityConfiguration配置类,在这个配置类中, 注入了一个非常重要的bean, bean的name为: springSecurityFilterChain,这是Spring Secuity的核心过滤器, 这是请求的认证入口。

在2处又使用了EnableGlobalAuthentication 注解, 注解源码为:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({AuthenticationConfiguration.class})
@Configuration
public @interface EnableGlobalAuthentication {
}

在这个注解中,激活了AuthenticationConfiguration配置类, 这个类是来配置认证相关的核心类, 这个类的主要作用是,向spring容器中注入AuthenticationManagerBuilder, AuthenticationManagerBuilder其实是使用了建造者模式, 他能建造AuthenticationManager, 这个类前面提过,是身份认证的入口。

所以,到这为止,EnableWebSecurity注解有两个作用,1: 加载了WebSecurityConfiguration配置类, 配置安全认证策略。2: 加载了AuthenticationConfiguration, 配置了认证信息。
 

三、@EnableGlobalMethodSecurity详解

1、Spring Security默认是禁用注解的,要想开启注解, 需要在继承WebSecurityConfigurerAdapter的类上加@EnableGlobalMethodSecurity注解, 来判断用户对某个控制层的方法是否具有访问权限 

@Configuration

@EnableWebSecurity

@EnableAutoConfiguration

@EnableGlobalMethodSecurity(prePostEnabled =true)

public class WebSecurityConfigextends WebSecurityConfigurerAdapter {……}

例如下面代码就表示如果用户具有admin角色,就能访问listAllUsers方法,但是如果方法前不加@preAuthorize注解,意味着所有用户都能访问listAllUsers

 @PreAuthorize("hasRole(‘admin‘)")

    @RequestMapping(value = "/user/", method = RequestMethod.GET)

    @ResponseBody

    publicList listAllUsers() {

        ……

    }

3、@EnableGlobalMethodSecurity详解

3.1、@EnableGlobalMethodSecurity(securedEnabled=true) 开启@Secured 注解过滤权限

3.2、@EnableGlobalMethodSecurity(jsr250Enabled=true)开启@RolesAllowed 注解过滤权限 

3.3、@EnableGlobalMethodSecurity(prePostEnabled=true) 使用表达式时间方法级别的安全性         4个注解可用

@PreAuthorize 在方法调用之前,基于表达式的计算结果来限制对方法的访问

@PostAuthorize 允许方法调用,但是如果表达式计算结果为false,将抛出一个安全性异常

@PostFilter 允许方法调用,但必须按照表达式来过滤方法的结果

@PreFilter 允许方法调用,但必须在进入方法之前过滤输入值

四、Mybatis

@Configuration是把bean交给Spring容器进行管理。

@MapperScan指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类

同@Mapper注解,但Mapper只能注解一个:

@Mapper
public interface UserDAO {
   //代码
}

@Configuration
@MapperScan({"com.macro.mall.tiny.mbg.mapper","com.macro.mall.tiny.dao"})
public class MyBatisConfig {
}

包com.macro.mall.tiny.mbg.mapper下接口为:

public interface PmsBrandMapper {
    int countByExample(PmsBrandExample example);

    int deleteByExample(PmsBrandExample example);
。。。
}

 resources下com.macro.mall.tiny.mbg.mapper的PmsBrandMapper.xml文件


<mapper namespace="com.macro.mall.tiny.mbg.mapper.PmsBrandMapper">
  <resultMap id="BaseResultMap" type="com.macro.mall.tiny.mbg.model.PmsBrand">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="first_letter" jdbcType="VARCHAR" property="firstLetter" />
    <result column="sort" jdbcType="INTEGER" property="sort" />
    <result column="factory_status" jdbcType="INTEGER" property="factoryStatus" />
    <result column="show_status" jdbcType="INTEGER" property="showStatus" />
    <result column="product_count" jdbcType="INTEGER" property="productCount" />
    <result column="product_comment_count" jdbcType="INTEGER" property="productCommentCount" />
    <result column="logo" jdbcType="VARCHAR" property="logo" />
    <result column="big_pic" jdbcType="VARCHAR" property="bigPic" />
  </resultMap>
  <resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="com.macro.mall.tiny.mbg.model.PmsBrand">
    <result column="brand_story" jdbcType="LONGVARCHAR" property="brandStory" />
  </resultMap>
  
 <select id="countByExample" parameterType="com.macro.mall.tiny.mbg.model.PmsBrandExample" resultType="java.lang.Integer">
    select count(*) from pms_brand
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
</mapper>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot小程序开发是近年来越来越流行的一种开发模式,尤其是在微信小程序等场景中越来越多的被应用。SpringBoot能够快速搭建轻量级的应用,同时可以将各种功能组件进行快速集成,因此受到了众多开发者的追捧。下面是详细介绍。 首先,SpringBoot小程序开发具有很强的可扩展性和灵活性。它可以支持多种数据库、消息队列、缓存等组件的集成,开发者可以按照自己的项目需求进行选择和组合,以满足不同场景的需求。同时,采用SpringBoot框架的应用具有高度的可测试性,开发者可以通过自动化的测试程序对应用程序进行测试,提高了开发效率和软件质量。 其次,SpringBoot小程序开发也有很好的注重安全性。它提供了多种安全机制,如基于OAuth2的安全机制等,可以满足不同项目的安全需求。 除此之外,SpringBoot小程序开发还支持多种API的开发方式,如RESTful API等,可以按照不同的具体需求来选择。同时,开发者也可以选择使用模版引擎来快速构建前端页面,避免冗长的html/css/js代码编写过程。 总之,SpringBoot小程序开发是一种非常理想的开发模式。它可以快速建立高效的应用程序,提供高度的可扩展性和灵活性,同时也支持不同的API和安全机制。因此,它被广泛应用于各种小程序开发之中,得到了开发者和用户的广泛好评。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值