一、springSecurity基于注解配置
创建一个spring项目
1、pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cjc.crowd</groupId>
<artifactId>contion</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.20.RELEASE</version>
</dependency>
</dependencies>
</project>
package com.cjc.spring;
import com.cjc.entity.Employee;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//表示当前配置类
@Configuration
public class MyAnnotationConfig {
//@Bean相当与xml标签配置,将方法返回值放入到ioc容器
@Bean
public Employee getEmployee() {
return new Employee();
}
}
package com.cjc.entity;
public class Employee {
}
package com.cjc.test;
import com.cjc.entity.Employee;
import com.cjc.spring.MyAnnotationConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SpringTest {
//基于注解配置ioc容器
@SuppressWarnings("resource")
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfig = new AnnotationConfigApplicationContext(MyAnnotationConfig.class);
Employee bean = annotationConfig.getBean(Employee.class);
System.out.println(bean);
annotationConfig.close();
}
}