Home  >  Article  >  Java  >  Detailed introduction to the annotation function in java

Detailed introduction to the annotation function in java

零下一度
零下一度Original
2017-06-30 11:18:541810browse

1. Description

The functions of the @Component annotation are the same, but there are three annotations with different meanings:

1) @Repository: annotation on the Dao implementation class
2) @Service: Annotated on the Service implementation class
3) @Controller: Annotated on the SpringMVC processor

Bean scope:
@Scope("prototype"): Used to specify object creation Mode, can be singleton mode or prototype mode, the default is singleton

Basic type attribute injection:
@Value

@Autowired: byType annotation injection, that is, based on type annotation
@Qualifier("mySchool"): byName annotation injection, must be used in conjunction with @Autowired when using @Qualifier

Domain attribute annotation:
@Resource: Without the name attribute Annotation injection in byType mode, but the premise is that the injected object can only have one
@Resource(name="mySchool"): annotation injection in byName mode

The life of the Bean:
@PostConstruct: The current Bean has just been initialized
@PreDestroy: The current Bean is about to be destroyed

@Configuration: Indicates that the current class acts as a Spring container, that is, all beans will be created by this class

Note:

Several issues should be stated before giving an example:

1. Annotations need to depend on the spring-aop-4.3.9.RELEASE.jar package, so they need to be imported. Dependency package.

## 2. Inject using annotation, the configuration file needs to be added Constraint header file:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context/spring-context.xsd">
You can also find this header file yourself from Spring’s documentation:

 3. If SpringJUnit4 test is used, you also need to import the spring-test-4.3.9.RELEASE.jar package

2. Example

1. First create a School class:

package com.ietree.spring.basic.annotation.demo1;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;

@Component("mySchool")public class School {

    @Value(value = "清华大学")private String name;public School() {super();
    }public School(String name) {super();this.name = name;
    }public void setName(String name) {this.name = name;
    }

    @Overridepublic String toString() {return "School [name=" + name + "]";
    }
}
Create Student class:

package com.ietree.spring.basic.annotation.demo1;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;/**
 * 说明:
 * 与@Component注解功能相同,但意义不同的注解还有三个:
 * 1)@Repository:注解在Dao实现类上
 * 2)@Service:注解在Service实现类上
 * 3)@Controller:注解在SpringMVC的处理器上
 * 
 * Bean作用域:
 * @Scope("prototype"):用于指定对象创建模式,可以是单例模式或者原型模式,默认是singleton
 * 
 * 基本类型属性注入:
 * @Value
 * 
 * @Autowired:byType方式的注解式注入,即根据类型注解
 * @Qualifier("mySchool"):byName方式的注解式注入,在使用@Qualifier时必须与@Autowired联合使用
 * 
 * 域属性注解:
 * @Resource:不加name属性则为byType方式的注解式注入,但前提是注入的对象只能有一个
 * @Resource(name="mySchool"):byName方式的注解式注入
 * 
 * Bean的生命始末:
 * @PostConstruct:当前Bean初始化刚完毕
 * @PreDestroy:当前Bean即将被销毁 *///@Scope("prototype")@Component("myStudent")public class Student {

    @Value(value = "小明")private String name;
    
    @Value(value = "25")private int age;    //    @Autowired//    @Qualifier("mySchool")//    @Resource(name="mySchool")    @Resourceprivate School school;// 对象属性,也叫做域属性public Student() {super();
    }    public Student(String name, int age) {super();this.name = name;this.age = age;
    }public void setName(String name) {
        System.out.println("执行setName()");this.name = name;
    }public void setAge(int age) {
        System.out.println("执行setAge()");this.age = age;
    }public void setSchool(School school) {this.school = school;
    }

    @Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + ", school=" + school + "]";
    }
    
    @PostConstructpublic void initAfter(){
        System.out.println("当前Bean初始化刚完毕");
    }
    
    @PreDestroypublic void preDestroy(){
        System.out.println("当前Bean即将被销毁");
    }
}
Create MyJavaConfig class:

package com.ietree.spring.basic.annotation.demo1;import org.springframework.beans.factory.annotation.Autowire;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/**
 * @Configuration:表示当前类充当Spring容器,即所有的Bean将由这个类来创建 */@Configurationpublic class MyJavaConfig {
    
    @Bean(name="mySchool")public School mySchoolCreator(){return new School("清华大学");
    }    // autowire=Autowire.BY_TYPE:指从当前类这个容器中查找与域属性的类型具有is-a关系的Bean// autowire=Autowire.BY_NAME:指从当前类这个容器中查找与域属性同名的Bean@Bean(name="myStudent", autowire=Autowire.BY_TYPE)public Student myStudentCreator(){return new Student("小明", 25);
    }
}
Create configuration file:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context/spring-context.xsd">
Create test class:

package com.ietree.spring.basic.annotation.demo1;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:com/ietree/spring/basic/annotation/demo1/applicationContext.xml")public class MyTest {
    
    @Autowiredprivate Student student;
    
    @Testpublic void test01() {

        String resource = "com/ietree/spring/basic/annotation/demo1/applicationContext.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(resource);

        School school = (School) ctx.getBean("mySchool");
        System.out.println(school);

        Student student = (Student) ctx.getBean("myStudent");
        System.out.println(student);
        
        ((ClassPathXmlApplicationContext)ctx).close();
    }    public void test02(){
        System.out.println(student);
    }
    
}

The above is the detailed content of Detailed introduction to the annotation function in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn