Spring使用注解整理

来源:互联网 发布:企业logo免费制作软件 编辑:程序博客网 时间:2024/05/21 07:09

1.声明Bean的注解:
(1)@Component组件,没有明确的角色
(2)@Service在业务逻辑层使用
(3)@Reposity在数据访问层使用
(4)@Controller在展现层使用
(5)@RestController是一个组合注解,组合了@Controller和 @ResponseBody

2.注入Bean的注解,一般情况使用(三者作用等同,可以互换)
(1)@Autowired:Spring使用的注解
(2)@inject:JSR-330提供的注解
(3)@Resource:JSR-250提供的注解

@Configuration 声明当前类是一个配置类
@ComponentScan自动扫描包下所有
其中@WiselyConfiguration组合注解可以替代@Configuration 和@ComponentScan

的:@Service,@Component,@Reposity,@Reposity,用法如下:

@Configuration@ComponentScan("learn.spring4.service")public class Config {}

测试:

public class Testmain {    public static void main(String[] args) {        AnnotationConfigApplicationContext aContext=new AnnotationConfigApplicationContext(Config.class);        UseFunctionService useFunctionService=aContext.getBean(UseFunctionService.class);        System.out.println(useFunctionService.sayHello("My friends"));        aContext.close();    }}

3.@Bean注解的方法上,声明当前方法的返回值为一个Bean

原创粉丝点击