Sping--注解(一) 常用注解总结

Sping注解是很重要的一块。今天在这里对常用到的注解做个小结,会尽可能说得详细些。

推荐这个英文文档,官方文档当然是最好的。最近发现,学习东西,还是多看官方文档,最权威,最详细。

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Configuration.html

1.@Component @Service @Controller @Repository

Component英文名组件,将它放在类的上方,表明要将这个类实例化到Sping容器之中,后面的三个是它的衍生类,后面的三个分工更加明确,可以立马知道这个类的作用。当不好划分这个类的区分时,可以用Component来修饰。

启用注解的方法:①XML文件:<context:component-scan base-package=”XXX.XXX.XXX(包名)”>

②注解的方式:@ComponentScan(basePackages = {"com.lee.demo.environment"}) 

2.@Configuration  @Bean  @PropertySource

Configuration 这个注解的意思:配置,你可以理解成资源文件,再说得通俗些,可以当成XML的配置文件。

Bean通常和Configuration搭配使用,Bean可以理解成xml配置文件中的<bean .... >

PropertySource作资源文件用,env.getProperty("bean.name")  bean.name 就是在app.properties中定义的。

 1 @Configuration
 2 @PropertySource("classpath:/app.properties")
 3 public class AppConfig {
 4 
 5     @Autowired
 6     Environment env;
 7 
    // 用bean修饰时,默认注入的名字就是方法名myBean 8 @Bean 9 public MyBean myBean() { 10 return new MyBean(env.getProperty("bean.name")); 11 } 12 }
1 public class MyBean {
2 
3     public MyBean(String name) {
4         System.out.println("my bean name is :" + name);
5     }
6 }
 1 @Configuration  
 2 @ComponentScan(basePackages = {"com.lee.demo.environment"})  
 3 public class Test {
 4 
 5     public static void main(String[] args) {  
 6         @SuppressWarnings("resource")
 7         AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(com.lee.demo.environment.AppConfig.class);
 8         MyBean bean = context.getBean(MyBean.class);
 9         
10     }
11 }

app.properties

1 bean.name=hello world

最后执行结果:

1 my bean name is :hello world

3.@Profile

它的作用相当于是分类。比如测试环境时是一套环境的参数设置,开发环境是另一套环境的参数设置,如果单纯的来回替换配置文件很麻烦,所以通过这个注解来解决这个问题。

(这个例子中,我将几个类都贴在一块了。)

public interface SpeakLanguage {
    void doSpeak(); 
}

@Component
public class Person { @Autowired private SpeakLanguage speakLangtage; public void speak() { speakLangtage.doSpeak(); } }
@Configuration
//@Profile("default") default的作用是,当没有制动profile的参数时,会默认执行带有default注解的类。 @Profile("dev") @Component public class Chinese implements SpeakLanguage { @Override public void doSpeak() { // TODO Auto-generated method stub System.out.println("I can speak Chinese"); } } @Configuration @Profile("production") @Component public class EnglishMan implements SpeakLanguage { @Override public void doSpeak() { // TODO Auto-generated method stub System.out.println("I can speak English"); } } @Configuration @ComponentScan(basePackages = {"com.lee.demo.environment"}) public class Test { public static void main(String[] args) { @SuppressWarnings("resource") AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(com.lee.demo.environment.Test.class); Person p = context.getBean(Person.class); p.speak(); } }

执行的时候,需要指定Profile,指定可以通过设置环境变量我尝试了,环境变量设置完之后,重启电脑才好用的,不知道为什么,JVM参数(-Dspring.profiles.active=production),servlet上下文参数定义.
执行结果:I can speak English

4.@Autowired @Qualifier  @Resource

1 public class FooService {
2      
    // Autowired 自动绑定 分为类型匹配和名称匹配 它是Spring的注解
    //Autowired 可以绑定要注册的类,它默认的是按照类型进行匹配,当一个类型有多个实现类,进而无法确定的时候,配合Qualifier指定实现类的名称,来绑定 3 @Autowired 4 @Qualifier("fooFormatter") 5 private Formatter formatter; 6 7 }
 1 @Service  
 2 public class SequenceServiceImpl implements SequenceService {  
 3   
    // 默认是按照名称方式进行Bean的匹配,它是J2EE的注解 4 @Resource 5 private SequenceMapper sequenceMapper; 6 public void generateId(Map<String, String> map) { 7 sequenceMapper.generateId(map); 8 9 } 10 11 @Resource(name = "manImpl")//注意是manImpl不是ManImpl,因为使用@Service,容器为我们创建bean时默认类名首字母小写 12 private Human human; 13 14 }

上述的两个代码段没有关系,单纯的拿出来展示注解用。

5.@PostConstruct @PreDestory

 这部分我偷懒了,看到一边文章写的非常详细,所以贴出地址:

https://blog.csdn.net/wo541075754/article/details/52174900

原文地址:https://www.cnblogs.com/lihao007/p/9043662.html