Springframework实例化Bean
来源:互联网 发布:肛泰和马应龙 知乎 编辑:程序博客网 时间:2024/06/08 20:05
前段时间接手了一个新的模块,里面到处都是用Spring框架中的getBean("AAADao")实例化Dao对象。跟踪了一下代码,把使用的技术和技巧做个小总结。
代码: ApplicationContext context = ApplicationContextUtil.getContext();
OrderDao ordersDao = (OrderDao) context.getBean("ordersDao");
1 注册 ContextLoaderListener或者这个类的子类,在web.xml中加入
<listener>
<listener-class>org.springframework.web.context.ContextLoadListener</listener-class>
</listener>
加完上面信息,在Web启动时就会自动读取默认路径下的applicationContext.xml这个默认的配置文件,如果想修改配置文件路径和配置文件名字,可以在web.xml中加入下面信息。
<context-param>增加一个上下文参数
<param-name>contextConfigLocation</param-name>参数name是contextConfigLocation,是固定的,不可以修改的。
<param-value>classpath:config/app-context.xml</param-value>
</context-param>
2.在Spring配置文件中加入自动扫描搜索组件设置
<context:component-scan base-package="cn.com.dao"/><!-- component-scan自动搜索@Component , @Controller , @Service , @Repository等标注的类 -->
配置base-package的值为需要自动扫描搜索的包名,包里面的类才会搜索到。经过这样的配置,dao包中的类都会被扫描到了
3.在Dao类中使用@Repository注解,就不用在spring配置文件中逐个配置dao的bean了
@Repository
public class OrdersDao{
}
在Dao包所有类中使用@Repository注解,就不用在spring配置文件中逐个配置dao的bean了,框架自动会把带有此注解的类作为一个Bean装载到容器中。如果@Repository后面没有设置name属性,那么框架自动会使用该类的类名的第一个小写字母当作Bean的Name。如果这个注解不包含 name值或是其他被自定义过滤器发现的组件,默认 Bean 名称会是小写开头的非限定类名。即orderDao.
4.定义一个工具类ApplicationContextUtil ,方便实例化容器中的Bean。
ublic class ApplicationContextUtil implements ApplicationContextAware{
private static ApplicationContext context;//声明一个静态变量保存
@Override
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
this.context=arg0;
}
public static ApplicationContext getContext() {
return context;
}
public static Object getBean(String beanName){
return context.getBean(beanName);
}
}
ApplicationContextUtil 一定要实现ApplicationContextAware接口。这个接口的作用是实现它的类就可以方便获得ApplicationContext中的所有bean。但是我们要实现这个接口的默认方法:
@Override
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
this.context=arg0;
}
spring能够为我们自动执行该方法,当然了,前提是spring要知道有这么一个实现了ApplicationContextAware接口的这么一个类存在,所以我们要在spring的配置文件中把这个类注册为一个普通的bean.
<bean class="cn.com.ApplicationContextUtil"></bean>
那么 此时就可以通过上面的方式获取Dao的Bean了,并且不用每次都实例化。
另外,在spring配置文件中配置一下,读入其它的属性文件。
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:jdbc.properties</value>
</list>
</property>
</bean>
propertyConfigurer是固定的,后面的类也是固定的,可以导入多个。获取属性文件的值时可以使用key来获取了,例如<property name="username" value="${jdbc.username}"/> 其中jdbc是属性文件名,name是属性文件中的一个键,这样就获取到了它的值,用于数据库文件分离化,防止重复代码。
- Springframework实例化Bean
- Inside-springframework-bean-definition
- 预实例化Bean
- Spring 实例化bean
- spring实例化bean
- 实例化bean
- Bean实例化问题
- bean实例化方式
- Spring1 Bean实例化
- Spring实例化bean
- 实例化bean的方式
- SPRING BEAN的实例化
- 实例化bean的方式
- spring bean 实例化问题
- bean 实例化的学习
- bean实例化的时间
- spring中实例化bean
- Spring之实例化Bean
- cdh5.7.1如何开启kerberos
- Linux环境变量与系统编程学习笔记
- Codeforces Round #385 (Div. 2) E
- 《西部世界》观后感
- 虚拟机出现“操作文件.PhysicalDrive1失败”的解决方法
- Springframework实例化Bean
- 1086. Tree Traversals Again (25)
- 数据压缩编码方法
- Android应用开发之所有动画使用详解
- [HDU5996] [BestCoder Round #90] dingyeye loves stone nim游戏
- Ubuntu14.04设置网络代理
- CSS使网页适应不同屏幕大小
- Android Material Design之CoordinatorLayout+AppBarLayout实现上滑隐藏
- View的深入浅出(二)View的滑动