Spring-介绍和搭建

Spring-介绍

一、三层架构中spring位置


二、spring一站式框架

        spring框架属于容器性质,容器中装什么对象就有什么功能,所以是一站式。它不仅不排斥其他框架,还能帮其他框架管理对象。(aop支持,ioc思想,spring jdbc,aop事务,junit测试支持等)


spring搭建

一、导包


1、4个spring包

2、日志包

com.springsource.org.apache.logging-1.1.1.jar

com.springsource.org.apache.log4j-1.2.15.jar        可选


二、创建一个对象
public class User {
	
	public User() {
		System.out.println("User对象空参构造方法!!!!");
	}
	private String name;
	private Integer age;
	private Car car;
	
	public User(String name, Car car) {
		System.out.println("User(String name, Car car)!!");
		this.name = name;
		this.car = car;
	}
	
	public User(Car car,String name) {
		System.out.println("User(Car car,String name)!!");
		this.name = name;
		this.car = car;
	}
	
	public User(Integer name, Car car) {
		System.out.println("User(Integer name, Car car)!!");
		this.name = name+"";
		this.car = car;
	}
	
	public Car getCar() {
		return car;
	}
	public void setCar(Car car) {
		this.car = car;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	
	public void init(){
		System.out.println("我是初始化方法!");
	}
	public void destory(){
		System.out.println("我是销毁方法!");
	}
	@Override
	public String toString() {
		return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
	}
	
}

三、书写配置注册对象到容器

1、位置任意(建议放到src下),配置文件名任意(建议applicationContext.xml)

2、导入约束

<?xml version="1.0" encoding="UTF-8"?>
<beans xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd " xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- 将User对象交给spring容器管理 -->
<bean class="cn.itcast.bean.User" name="user"/></beans>

四、代码测试
public class Demo {
	@Test
	public void fun(){
		//1 创建容器对象
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		/*//2 向容器"要"user对象
		User u = (User) ac.getBean("user");
		//3 打印user对象
		System.out.println(u);*/
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Security 6中搭建授权服务器是一个相对简单的过程。下面我将简要介绍一下搭建的步骤。 首先,我们需要在Spring Boot项目中添加Spring Security和OAuth2相关的依赖。可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-authorization-server</artifactId> </dependency> ``` 接下来,我们需要创建一个配置类来配置授权服务器。在这个类上要添加`@EnableAuthorizationServer`注解,以启用授权服务器功能。在配置类中,我们要配置一些必要的属性,比如授权服务器的端口、授权类型、客户端信息等。 ```java @Configuration @EnableAuthorizationServer public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("client1") .secret("{noop}secret1") .authorizedGrantTypes("authorization_code", "refresh_token") .scopes("read", "write") .redirectUris("http://localhost:8080/login/oauth2/code/custom"); } @Override public void configure(AuthorizationServerSecurityConfigurer security) throws Exception { security.allowFormAuthenticationForClients(); } } ``` 在上面的示例中,我们使用了一个内存存储方式来存储客户端信息。在实际项目中,可以根据需求选择不同的存储方式,比如使用数据库。 最后,我们需要在Spring Boot的主配置类上添加`@EnableWebSecurity`注解,以启用Web安全功能。 ```java @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/oauth/**", "/login/**", "/logout/**") .permitAll() .and().csrf().disable(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("admin") .password("{noop}password") .roles("ADMIN"); } } ``` 通过上述的步骤,我们就完成了在Spring Security 6中搭建授权服务器的过程。其中,配置授权服务器的属性和行为可以根据实际需求进行调整和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值