Spring注解大全(示例详解)

目录

@Configuration

@EnableWebSecurity


 

@Configuration

1.@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文)

package com.dxz.demo.configuration;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TestConfiguration {
    public TestConfiguration() {
        System.out.println("TestConfiguration容器启动初始化");
    }
}


package com.dxz.demo.configuration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestMain {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
    }
}

//运行结果:TestConfiguration容器启动初始化

2.@Configuration启动容器+@Bean注册Bean,@Bean下管理bean的生命周期

package com.dxz.demo.configuration;

public class TestBean {

    private String username;
    private String url;
    private String password;
    public void sayHello() {
        System.out.println("TestBean sayHello");
    }
    public String toString() {
        return this.username + this.url + this.password;
    }
    public void start() {
        System.out.println("TestBean 初始化");
    }
    public void cleanUp() {
        System.out.println("TestBean 销毁");
    }
}


package com.dxz.demo.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class TestConfiguration {
    public TestConfiguration() {
        System.out.println("TestConfiguration容器启动初始化");
    }
    @Bean(name="testBean",initMethod="start",destroyMethod="cleanUp")
    @Scope("prototype")
    public TestBean testBean() {
        return new TestBean();
    }
}


package com.dxz.demo.configuration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestMain {
    public static void main(String[] args) {
      ClassPathXmlApplicationContext
        ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
        TestBean tb = (TestBean) context.getBean("testBean");
        tb.sayHello();
    }
}
//运行结果:TestConfiguration容器启动初始化;TestBean 初始化;TestBean sayHello;TestBean 销毁

3.@Configuration启动容器+@Component注册Bean

package com.dxz.demo.configuration;
import org.springframework.stereotype.Component;

@Component
public class TestBean {
    private String username;
    private String url;
    private String password;
    public void sayHello() {
        System.out.println("TestBean sayHello");
    }
    public String toString() {
        return this.username + this.url + this.password;
    }
    public void start() {
        System.out.println("TestBean 初始化");
    }
    public void cleanUp() {
        System.out.println("TestBean 销毁");
    }
}


package com.dxz.demo.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
//添加自动扫描注解,basePackages为TestBean包路径
@ComponentScan(basePackages = "com.dxz.demo.configuration")
public class TestConfiguration {
    public TestConfiguration() {
        System.out.println("TestConfiguration容器启动初始化");
    }
}


package com.dxz.demo.configuration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestMain {
    public static void main(String[] args) {
      ClassPathXmlApplicationContext
        ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
        TestBean tb = (TestBean) context.getBean("testBean");
        tb.sayHello();
    }
}
//运行结果:TestConfiguration容器启动初始化;TestBean sayHello;

4.在@configuration中引入spring的xml配置文件

package com.dxz.demo.configuration2;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource("classpath:applicationContext-configuration.xml")
public class WebConfig {
}


package com.dxz.demo.configuration2;
public class TestBean2 {
    private String username;
    private String url;
    private String password;

    public void sayHello() {
        System.out.println("TestBean2 sayHello");
    }

    public String toString() {
        return this.username + this.url + this.password;
    }

    public void start() {
        System.out.println("TestBean2 初始化");
    }

    public void cleanUp() {
        System.out.println("TestBean2 销毁");
    }
}

package com.dxz.demo.configuration2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestMain2 {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext
        ApplicationContext context = new AnnotationConfigApplicationContext(WebConfig.class);
        TestBean2 tb = (TestBean2) context.getBean("testBean2");
        tb.sayHello();
    }
}

5.在@configuration中引入其它注解配置

package com.dxz.demo.configuration2;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportIResource("classpath:applicationContext-configuration.xml")
@Import(TestConfiguration.class)
public class WebConfig {
}
//后面代码都不一一解说了

@EnableWebSecurity

1.@EnableWebSecurity是Spring Security用于启用Web安全的注解。典型的用法是该注解用在某个Web安全配置类上(实现了接口WebSecurityConfigurer或者继承自WebSecurityConfigurerAdapter)

2.EnableWebSecurity注解是个组合注解,他的注解中,又使用了@EnableGlobalAuthentication注解

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

@Import({WebSecurityConfiguration.class//1, SpringWebMvcImportSelector.class, OAuth2ImportSelector.class}),此处激活了WebSecurityConfiguration配置类,在这个配置类中, 注入了一个非常重要的bean, bean的name为: springSecurityFilterChain,这是Spring Secuity的核心过滤器, 这是请求的认证入口。

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

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值