spring 注解

组件类注解

  1. 被注解的java类当做Bean实例,Bean实例的名称默认是Bean类的首字母小写,其他部分不变。
  2. 指定了某些类可作为Spring Bean类使用后,最好还需要让spring搜索指定路径,在Spring配置文件加入如下配置

@Component
@Repository
@Service
@Controller

装配bean常用注解

  1. @Autowired:属于Spring 的org.springframework.beans.factory.annotation包下,可用于为类的属性、构造器、方法进行注值
  2. @Resource:不属于spring的注解,而是来自于JSR-250位于java.annotation包下,使用该annotation为目标bean指定协作者Bean。
  3. @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
    @Autowired 注解与@Resource 注解的区别

@Component @Configuration @Bean

@Configuration @Bean

Bean注解主要用于方法上,有点类似于工厂方法,当使用了@Bean注解,我们可以连续使用多种定义bean时用到的注解,譬如用@Qualifier注解定义工厂方法的名称,用@Scope注解定义该bean的作用域范围,譬如是singleton还是prototype等。

Spring 中新的 Java 配置支持的核心就是@Configuration 注解的类。这些类主要包括 @Bean 注解的方法来为 Spring 的 IoC 容器管理的对象定义实例,配置和初始化逻辑。

使用@Configuration 来注解类表示类可以被 Spring 的 IoC 容器所使用,作为 bean 定义的资源。这和 Spring 的 XML 文件中的非常类似

@Configuration
public class AppConfig {
    @Bean
    public MyService myService() {
        return new MyServiceImpl();
    }
}

这和 Spring 的 XML 文件中的非常类似

<beans>
    <bean id="myService" class="com.acme.services.MyServiceImpl"/>
beans>

@Bean 注解扮演了和元素相同的角色。

@Configuration 和 @Component 区别

一句话概括就是 @Configuration 中所有带 @Bean 注解的方法都会被动态代理,因此调用该方法返回的都是同一个实例。

mvc注解

  • @Controller:
  • @RequestMapping:
  • @RequestMapping:
  • @RequestParam:
  • @PathVariable:
  • @RequestBody:指方法参数应该被绑定到HTTP请求Body上。
  • @ResponseBody:

你可能感兴趣的:(spring,spring)