@Configuration+@Bean & @Component & @Resource/@autowired/@Inject
2016-07-18 16:32
381 查看
@Configuration+@Bean
@Bean相当于<bean>标签,但我的理解是,它所描述的bean只能是没有引入其他类的简单的bean
@Component
示例代码:
@Component(@Controller @Service @Repository) 也相当于<bean>标签,但我的理解是,他们所描述的不是像@Bean那样简单的bean
@Resource/@autowired/@Inject
前面的都是注入的注解,相当于“存”,而这三个就相当于“取”
示例代码:
@Configuration @ComponentScan(basePackages = WebConfig.PACKAGE_NAMESPACE) @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware { private ApplicationContext applicationContext;
<span style="white-space:pre"> </span>....................
@Bean public CommonsMultipartResolver multipartResolver() { final CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(); commonsMultipartResolver.setMaxUploadSize(MAX_UPLOAD_SIZE); return commonsMultipartResolver; } @Bean public 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 @Transactional public class XXServiceImpl implements XXService{ @Autowired TerminalRepository terminalRepository; @Autowired ProductRepository productRepository; ...........
@Component(@Controller @Service @Repository) 也相当于<bean>标签,但我的理解是,他们所描述的不是像@Bean那样简单的bean
@Resource/@autowired/@Inject
前面的都是注入的注解,相当于“存”,而这三个就相当于“取”
相关文章推荐
- spring技术框架基础知识二(静态)动态代理
- php 的编译原理 剖析
- height和line-height的区别
- Servlet学习(一)部署servlet
- JavaScript读二进制文件并用ajax传输二进制流的方法
- 【C语言入门教程】7.1 结构体类型变量的定义和引用
- Linux(Ubuntu)安装并破解 SecureFX
- spring-data-redis 使用
- Android Recyclerview分割线
- 学习笔记-斯坦福iOS7-第十三课:Core Data 和表格视图
- Android自定义View之画笔与画布(2)
- 如何使用反射
- 几种遍历map的方法
- leetcode题解-204. Count Primes
- iOS UITableView(四)-Cell拖动更改排序
- 微信高级群发接口正文乱码解决方案
- Scollview 和 EditText 滚动冲突
- Android 任务和返回栈详解
- Could not resolve archetype org.apache.maven.archetypes:maven-archetype-quickstart
- HorizontalScrollView作为导航栏和HorizontalScrollView的滑动