spring Bean管理

一、spring对于配置文件中每个<bean.../>元素默认驱动spring调用该类无参数的构造器来创建实例,并将该实例作为spring容器中的bean.

二、在spring配置文件中配置bean时,class属性的值必须是bean实现类的完整类名,不能是接口,或者抽象类。

三、<bean>的创建分为了两步:

<bean>元素启动spring调用的构造器创建对象。

<property/>子元素驱动spring执行setter方法。

四、ApplicationContext 是spring容器最常用的接口,该接口常用的两个实现类为:

ClassPathXmlApplicationContext:从类加载路径下搜索配置文件,并根据配置文件来创建Spring容器。

FileSystemXmlApplicationContext:从文件系统的相对路径或绝对路径下搜索配置文件。

例如:

   ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

Person p= ctx.getBean('person',Person.class);

五、spring的核心功能有两个:

1、spring容器作为超级大工厂负责创建管理所有的java对象,这些对象被称为bean.

2、spring容器管理容器中的bean之间的依赖关系,spring使用一种被称为“依赖注入”的方式来管理bean之间的依赖关系。

依赖注入是一种优秀的解耦方式,系统自动提供所需实例,无序程序显示获取。

六、简单工厂模式:

1、调用者面向被依赖对象的接口编程。

2、将被依赖对象的创建交给工厂完成。

3、调用者通过工厂来获取被依赖组件。

避免了类层次的硬编码耦合,但还需要主动通过工厂获取被依赖对象。Spring将这种主动变为了被动,所以也成为控制反转。

七、使用spring框架后的两个主要改变:

1、程序无须使用New调用构造器去创建对象。

2、当调用者需要被依赖对象的方法是,调用者无须主动获取被依赖对象,只要等待spring容器注入。

八、Ioc注入两种方式:

1、设置注入:通过User类中的setter方法。

  1.     <bean id="user" class="com.zcl.spring.setterinjection.User">  
  2.         <property name="name" value="Zhao" />  
  3.         <property name="age" value="22" />  
  4.         <property name="country" value="China" />  
  5.     </bean>  
2、构造注入:通过User的构造函数
  1.     <bean id="user" class="com.zcl.spring.setterinjection.User">  
  2.         <constructor-arg value="Zhao" />  
  3.         <constructor-arg value="22" />  
  4.         <constructor-arg value="China" />  
  5.     </bean>  
构造注入通俗讲就是驱动spring在底层以反射方式执行指定参数的构造器,执行时就利用构造器参数对成员变量执行初始化。
在<constructor-arg>中还可以指定一个index属性,用于指定是该构造器的第几个参数。
九、spring有两个核心接口:BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory子接口,他们都代表了Bean容器。spring容器是生产bean实例
的工厂,并管理容器中的bean。
通过接口中的下面方法可以获取bean中:
Object getBean(String name);
Object getBean(String name,class requiredType);
十、ApplicationCOntext的主要功能
1、ApplicationContext 默认预初始化所有的singleton Bean。
2、通过实现MessageSource接口,提供国际化支持。
3、资源访问
4、事件机制
十一、初始化所有singleton bean
1、bean的作用域(singleton、prototype、request、session、global-session)通过scope属性指定@scope指定
http://blog.csdn.net/fengruifang/article/details/6522262
2、bean的生命周期
http://blog.csdn.net/feihong247/article/details/7798474
主要点: < beans   default-lazy-init =”true” … >   延时加载,会在第一次调用时,创建
  1. <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" init-method="init" destroy-method="destroy"/>  
可以指定init-mehtod 和destroy-method 说明初始化和销毁bean是调用的方法。
全局的初始化销毁 在配置上方 指定 default-init-method="" default-destroy-mehtod=""
或者让bean去实现InitializingBean、DisposableBean 的接口中的方法。
3、通过ApplicationContext获取bean
ApplicationContext ctx = new ClassPathXmlApplicationContext(""):
ctx.getBean("name",class).
4、通过bean获取ApplicationContext bean中声ApplicationContext cxt;
一、通过让Bean实现BeanFactoryAware接口 中的setBeanFactory(BeanFactory beanFeatory);
二、通过让Bean实现ApplicationContextAware接口 中的setApplicationContext(ApplicationContext ctx);
三、在属性上用@AutoWired标注。
注意:set方法都是由spring容器自动调用的。
5、bean的自动装配
No:不做任何操作。
byName:根据bean id 装配
byType:根据类型装配
Contructor:构造方法
<bean id="user" class="com.zzj.bean.User" autowire="byName"></bean>  
转链接:http://blog.csdn.net/zhangzeyuaaa/article/details/22588459
十二、通过实现MessageSource接口,提供国际化支持。
配置:
<bean id="messageSource" class="org.springframwork.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>message</value>
</list>
</property>
</bean>
获取message.properties中的"hello"
ctx.getMessage("hello",new String[]{"孙悟空"},Local.getDefault(Locale.Category.FORMAT)) ;
//getMessage(name,object[],LOCAL);
十三、资源访问
引入:http://blog.csdn.net/hehexiaoyou/article/details/27343837
所有的ApplicationContext都实现ResourceLoader接口,可以方便的对资源进行访问。
Resource temp=ctx.getResource("classpath:com/baobaotao/beanfactory/bean.xml");
十四、事件机制
ApplicationContext的事件机制,通过ApplicationEvent类和ApplicationListener接口实现。
*ApplicaitionEvent类:容器事件,必须由Application Context发布
*ApplciationListener类:监听类,可由容器中的任何监听器Bean担当。
过程: 程序 ---》applciationEvent时间---》ApplicationContext---》激发--》AppliccationListener
监听器:实现ApplicationListener接口。
在xml中配置监听器,和其他<bean>没有区别
程序可以主动触发时间:ctx.publishEvent(ele);
------------------注解------------------------------------
一、classpath扫描与组件管理
@Component 是一个通用注解,可注释所有bean.
@Repository 通常用来注释DAO层
@Service 通常用来注释Service层
@Controller通常用来注释Controller层
二、<context:annotation-config/>只用来扫描方法上的成员变量。
三、<context:componet-scan base-package="com.xxx">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
类自动检测及Bean注解。包含了<context:annotation-config/>的内容,所以配置了该内容,就不用再配置<context:annotation-config/>了。
context:inclue-filter 包含哪些filter fcontex:exculde-filter不包含。
一中@显示指定了Bean的名称,则使用指定名称,否则spring自定义bean的名称。
四、@Scope("singleton") ---bean作用域标签。
@Required ---set方法上,指定Bean属性必须在配置时被填充。。不常用。
@AutoWired 可以注解在setter方法上
成员变量
构造器上
@AutoWired(required=false) 标明不是必须的,找不到不会报错。每个类智能有一个构造方法标明@required=true
@Autowired 可以注解依赖性接口,例如BeanFactory,ApplicationContext、Evironment、ResourceLoad、ApplicationEventPublisher、MessageSource
@AutoWired可以添加到数组和集合的属性上。
例如:@Autowired private List<BeanInterface> list;//会将BeanInterface中的所有子类添加进来。
可以通过@order设置数组顺序。
@Qualifier 配合@Autowired使用,类似<bean>中的id, 用来缩小返回。
@Resource 和@Autowired差不多,通常用@Resource,按名称查找。
五、java容器注释
例如:
@Configuration
public class AppConfig{
@Bean
public MyService myService()
{ return new MyServiceImpl();}
===<bean id="myServiceImpl" class="MyServiceImpl"/>的声明。
@Bean(init-method="init") 可以指定初始化方法,类似还有destory-method等。
六、加载资源文件
<context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>
${jdbc.name} ---这样使用
注释:
@Configuration
@ImportResource("classpath:xxxxxx.jdbc");
@Value("${jdbc.name}") private String name;









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值