Spring中的注解

在对Spring系列进行学习时,会在简化xml的同时,遇到一些注解,一开始,我只是对这些注解进行搜索和短暂的记忆,主要在理解程序上,但后来发现,必须对一些注解进行整合理解,下面是我在各网站中搜索到的注解给于的自己的理解。

一、@SpringBootApplication

@SpringBootApplication:申明让spring boot自动给程序进行必要的配置,这个配置等同于:@Configuration ,@EnableAutoConfiguration 和 @ComponentScan 三个配置之和。

@Configuration

等同于spring的XML配置文件

@EnableAutoConfiguration

这个注解是是Springboot根据我们所引入的jar包自动配置的,假设我们有自己的配置类则会覆盖

@ComponentScan

是告诉springboot进行包扫描的,同xml中下行代码,就是对controller进行包扫描。

<context:component-scan base-package="controller"/>

1、搭配 @ImportResource 注解

当有使用到@Configuration或@SpringBootApplication注解时,实际上是用注解代替了xml的加载,若还想加载另外的xml文件时,就可以使用@ImportResource注解,如下代码。

@SpringBootApplication
@RestController
@ImportResource("classpath:consumer.xml")
public class Client {
    @Autowired
    SayService sayService;
    @RequestMapping("/hello")
    public String say(@RequestParam ("name") String name) {
        return sayService.say(name);
    }
    public static void main(String[] args) throws Exception{
        ConfigurableApplicationContext context = SpringApplication.run(Client.class, args);
    }
}

2、搭配 @Bean 注解

spring中,配置bean的时通常会在xml中编写标签,例如下列代码

<beans>
    <bean id="course" class="demo.Course">
        <property name="module" ref="module"/>
    </bean>
    <bean id="module" class="demo.Module">
        <property name="assignment" ref="assignment"/>
    </bean>
    <bean id="assignment" class="demo.Assignment" />
</beans>

代码中定义了三个bean,course中包含参数有module,module中参数有assignment,形成的二重嵌套bean。如果现在我需要将xml中的定义删除,使用configuration注解

那么在configuration注解中的类应该为

@Configuration
public class AppContext {
    @Bean
    public Course course() {
        Course course = new Course();
        course.setModule(module());
        return course;
    }
 
/**
 *实现了bean的嵌套注入
**/
    @Bean
    public Module module() {
        Module module = new Module();
        module.setAssignment(assignment());
        return module;
    }

    @Bean
    public Assignment assignment() {
        return new Assignment();
    }
}

如上代码,我们将@Configuration注解写在类前,再将@Bean注解写在方法前,方法名即为bean的id。

二、@Autowired

该注解可以对标注对象进行自动装配,不仅仅可以对变量,也可以对方法、构造函数进行装配,下代码为对成员变量进行注入例

//接口类
public interface People{
    
    void work();
}
//实现类
@Repository
public class Student implements People{
    @Override
    public void work() {
        System.out.println("do homework");
    }
}
//测试类
@Service
public class UserService {
    @Autowired
    private People people;
    public void work(){
        people.work();
    }
}

@Autowired对接口类people进行自动装配,运行后people会和他的实现类自动装配,控制台上会显示do homework。

1、@Qualifier

正如上@Autowired的例子所示,当接口只有一个实现类的时候,我们可以使用自动装配,那么,如果拥有多个实现类的时候,标注该怎么选择装配呢,那就是使用@Qualifier进行选择了,在上例中,增加多一个people实现接口类,如下代码。

@Repository
public class Teacher implements People{
    @Override
    public void work() {
        System.out.println("do housework");
    }
}

这时,我们只有修改测试类的代码,就能正确的装配teacher类上。

//测试类
@Service
public class UserService {
    @Autowired
    @Qualifier("teacher")
    private People people;
    public void work(){
        people.work();
    }
}

三、bean类注解

1、@Component

2、@Repository

3、@Service

4、@Controller

这四种注解其实都没什么不同,只是各自运用的层不一样,@Component是运用在控制层action,@Service运用在业务层server,@Repository是运用在持久层dao,@Component可注解不确定层。 如果注解中含有name属性@Component("beanname"),就会将这个name值取为bean的名字。

用@Scope注解能定义这个bean的作用域

四、SpringMVC注解

@RequestMapping

表示将该控制器去处理某个url中的请求,将一个地址请求映射到该方法中。

@RequestParam

用在方法的参数前,进行前端参数与后台参数名字不一的映射

五、JPA注解

@Entity

标注实体类

@Table

与Entity联用,说明该bean连接的是数据库的哪张表,@Table(name = "",catalog = "",schema = "" ) 中name填表名,catalog填数据库名,schema填用户名。

@Id

表示该属性为主键。可标注在getter方法前

@Column

映射成员到关系表中的某一列

@GeneratedValue

标注主键的生成策略@GeneratedValue(strategy=)参数可以为

GenrationType.Auto.

GenrationType.identity

GenrationType.sequrnce

GenrationType.table

参考文章

史上最全的springboot注解-随风飘扬

springboot注解大全-浩瀚星空

spring常用注解

@Autowired用法详解

@Autowired的几种注入方式

Spring注解@Repository、@Service、@Controller、@Component

@Id 和 @GeneratedValue 注解详解

# Spring 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×