@Configuration+@Bean & @Component & @Resource/@autowired/@Inject

来源:互联网 发布:bestie软件下载 编辑:程序博客网 时间:2024/04/30 02:39

@Configuration+@Bean

示例代码:

@Configuration@ComponentScan(basePackages = WebConfig.PACKAGE_NAMESPACE)@EnableWebMvcpublic class WebConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {private ApplicationContext applicationContext;
<span style="white-space:pre"></span>....................
@Beanpublic CommonsMultipartResolver multipartResolver() {final CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();commonsMultipartResolver.setMaxUploadSize(MAX_UPLOAD_SIZE);return commonsMultipartResolver;}@Beanpublic TemplateEngine templateEngine() {final SpringTemplateEngine engine = new SpringTemplateEngine();engine.setEnableSpringELCompiler(true);engine.setTemplateResolver(this.templateResolver());return engine;}@Bean(name = "messageSource")public ReloadableResourceBundleMessageSource getMessageSource() {final ReloadableResourceBundleMessageSource resource = new ReloadableResourceBundleMessageSource();resource.setBasenames("classpath:i18n/application-messages");resource.setDefaultEncoding("UTF-8");return resource;}
<span style="white-space:pre"></span>................................
}
@Configuration相当于<beans>标签,所以此处WebConfig.class相当于一个xml配置文件而不是一个bean
@Bean相当于<bean>标签,但我的理解是,它所描述的bean只能是没有引入其他类的简单的bean

@Component


示例代码:
@Service@Transactionalpublic class XXServiceImpl implements XXService{@AutowiredTerminalRepository terminalRepository;@AutowiredProductRepository productRepository;...........

@Component(@Controller  @Service  @Repository) 也相当于<bean>标签,但我的理解是,他们所描述的不是像@Bean那样简单的bean


@Resource/@autowired/@Inject

前面的都是注入的注解,相当于“存”,而这三个就相当于“取”


0 0