spring 中bean

一、bean的定义和应用

1、 bean 形象上类似于getXX()和setXX()的一种。

2、 由于java是面向对象的,类的方法和属性在使用中需要实例化。

3、 规律:凡是子类及带有方法或者属性的类都要加上注册bean。

4、 bean类似于类的代理人.

5、 带有@的字符,spring都会自动扫描一下,看一下是啥。

二、 注解

1、 xml配置文件中自带的bean。@autowired @resource,在spring中通过byType和ByName来使用

2、注册bean。 在@component、@Control、@Service、@Configuration、@Repository中,这些注解会把你要实例化的对象化成一个bean。

三、 例子

@Configuration

public class Connection {

public @Bean HttpClientConfig httpClientConfig() {

String connectionUrl = "http://192.168.1.13:9200";

HttpClientConfig httpClientConfig = new HttpClientConfig.Builder(connectionUrl).multiThreaded(true).build();

return httpClientConfig;

}

@Bean(name="jestClient")

public synchronized static JestClient jestClient() {

JestClientFactory factory = new JestClientFactory();

return factory.getObject();

}

}

@Configuration中已经注册了bean,在方法中加入了@bean方法,我在另外一个类的引用的时候如下:

@Autowired

private JestClient jestClient;

此句的生命在spring中扫描的时候会自动初始化jestclient这个对象,并给此对象赋值。

这个是我学习spring mvc做的笔记,记录一下!

最新文章

  1. OPENVPN
  2. Linux学习心得之 LVM管理与Linux系统安装
  3. 由csdn开源项目评选中闹出刷票问题想到投票程序的设计
  4. Chrome extension
  5. 那些容易忽略的事4-(正则表达式反向引用\n)
  6. Delphi Socket的最好项目——FastMsg IM(还有一些IM控件),RTC,RO,Sparkle等等,FileZilla Client/Server,wireshark,NSClient
  7. [TPYBoard-Micropython之会python就能做硬件 7] 学习使用蓝牙模块及舵机
  8. gzip 与 gunzip 语法与示例
  9. python web -- flask
  10. php-fpm开机启动
  11. 全球第一免费开源ERP Odoo Ubuntu最佳开发环境独家首发分享
  12. yum install mariadb安装数据库开启不了
  13. ?:,reverse,vector的基本小结
  14. Bootstrap 分页、标签、徽章、超大屏幕、页面标题
  15. 2019-04-09-day028-OSI七层模型
  16. tomcat Error:NB:JAVA_HOME should point to a JDK not a JRE 解决方法
  17. 模块math和cmath
  18. javascript的基础知识整理
  19. django 错误之 ImportError: No module named **
  20. MVC001之权限校验

热门文章

  1. monkeyRunner
  2. python模块datetime
  3. 《剑指offer》面试题16—反转链表
  4. 怎样让自定义Cell的图片和文本自适应高度
  5. 如何在html中引入jsx文件
  6. [Xcode 实际操作]八、网络与多线程-(22)使用GCD多线程技术异步下载图片
  7. hyperledger fabric 1.0.5 分布式部署 (二)
  8. perl C/C++ 扩展(一)
  9. [软件工程基础]2017.10.27 第二次 Scrum 会议
  10. AU3中BitAnd与Win32汇编中的&按位进行与操作的本质思考