参考官网: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>