Spring下如何配置bean
本次讲述项目背景:
创建Service类,Service下用到dao类。通过在Spring中配置bean,实现在项目启动时,自动加载这个类
本次只讲述配置bean的注意事项,故只给出简单实例:
创建Service:
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} public void init(){
……
}
}
在applicationContext.xml下配置bean:
<bean id="userService" class="com.gcs.service.UserService"
init-method="init">
<property name="config" >
<ref local="UserDao" />
</property>
</bean> <bean id="UserDao" class="com.gcs.dao.UserDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
当配置结束后,系统一直报错,报错信息如下:
org.springframework.beans.NotWritablePropertyException:
Invalid property 'config' of bean class []: Bean property 'config' is not writable or has an invalid setter method.Does the parameter type of the setter match the return type of the getter?
经过排查,发现问题原因:
<bean id="userService" class="com.gcs.service.UserService"
init-method="init">
<property name="config" > --此处配置的name,应为userDao;与配置的类的实例名保持一致
<ref local="UserDao" />
</property>
</bean>
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} public void init(){
……
}
}
我的理解是这样的,在Java中从这个类操作另一个类,需要实例化,也就是 UserDao userDao=new UserDao();通过new出来的对象userDao来操作这个类,
而Spring通过配置,省去了去手动new,通过配置bean把dao类注入到service类中,但bean也应该符合Java面向对象的规则。
配置的bean中,<ref local="UserDao" /> 指向的是叫做UserDao的bean,相当于指向UserDao类,而<property name="userDao" > 则相当于获取实例化后的对象。
这样就成功的把dao类注入到service类中 注:这仅仅是我个人的理解,如有不同意见,请告知……
Spring下如何配置bean的更多相关文章
- 5. Spring 通过 XML 配置 bean (进阶)
1. 设置 bean 的作用域 当通过 Spring IOC 容器创建 bean 实例的时候,不仅可以完成 bean 的实例化,也可以为 bean 指定特定的作用域,Spring 支持以下 5 种作用 ...
- Spring入门第一课:Spring基础与配置Bean
1.入门 Spring是简化java开发的一个框架,其中IoC和AOP是Spring的两个重要核心.由于Spring是非侵入性的,通过Ioc容器来管理bean的生命周期,还整合了许多其他的优秀框架,所 ...
- Spring 通过工厂配置Bean
1.通过静态工厂方法配置Bean 要声明通过静态方法创建的 Bean, 需要在 Bean 的 class 属性里指定拥有该工厂的方法的类, 同时在 factory-method 属性里指定工厂方法的名 ...
- 12.Spring通过FactoryBean配置Bean
为啥要使用FactoryBean: 在配置Bean的时候,需要用到IOC容器中的其它Bean,这个时候使用FactoryBean配置最合适. public class Car { private St ...
- Spring通过注解配置Bean
@Component: 基本注解, 标识了一个受 Spring 管理的组件@Repository: 标识持久层组件@Service: 标识服务层(业务层)组件@Controller: 标识表现层组件 ...
- spring 注解方式配置Bean
Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件特定组件包括: @Component:基本注解,标示了一个受Spring管理的Bean组件 @Respository:标识 ...
- Spring IOC实现配置bean和实例
配置 beans.xml文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&quo ...
- Spring 通过FactoryBean配置Bean
1.实现FactoryBean接口 import org.springframework.beans.factory.FactoryBean; public class CarFactoryBean ...
- Spring--通过注解来配置bean【转】
Spring通过注解配置bean 基于注解配置bean 基于注解来配置bean的属性在classpath中扫描组件 组件扫描(component scanning):Spring能够从classpat ...
随机推荐
- Concurrency != Parallelism
前段时间在公司给大家分享GO语言的一些特性,然后讲到了并发概念,大家表示很迷茫,然后分享过程中我拿来了Rob Pike大神的Slides <Concurrency is not Parallel ...
- StringBuffer 的 各种方法
StringBuffer 其实提供了很多有用的方法, 以前用的多是 append, 其实还有: append(double) delete(int, int) deleteCharAt(int) re ...
- MailKit系列之---查询SearchQuery
对于邮件的唯一Id查询,由于MailKit提供了大量的方法,无法完全讲解完全,所以这里只选择几个来介绍. MailKit通过方法folder.Search来查询邮件的唯一Id,参数是一个SearchQ ...
- EntityFramework linq 多条件查询,不定条件查询
一.场景描述: 开发的时候,有些查询功能,往往查询的条件是不确定的,用户没有填的不参与到查询中去. 如图1所示,用户可能只要给根据名称来查询即可,有时候开始时间和结束时间并不需要填写. 图 1 二.解 ...
- SVN:Previous operation has not finished; run 'cleanup' if it was interrupted
异常处理汇总-开发工具 http://www.cnblogs.com/dunitian/p/4522988.html cleanup failed to process the following ...
- 网站就必须用响应式布局吗?MVC视图展现模式之移动布局
本文先引入给读者一个自己研究的机会,下次深入说明一下: 废话不多说,直接上图 新建一个mvc的项目 在视图里面添加一个移动端视图 正常访问一下 Bootstrap自带的响应式的方式(页面代码并没有改变 ...
- android 通讯录实现
最近项目需要,于是自己实现了一个带导航栏的通讯录,上代码! 一.数据准备 (1)bean: public class Friend { private String remark; private S ...
- 前端学PHP之PDO预处理语句
× 目录 [1]定义 [2]准备语句 [3]绑定参数[4]执行查询[5]获取数据[6]大数据对象 前面的话 本来要把预处理语句和前面的基础操作写成一篇的.但是,由于博客园的限制,可能是因为长度超出,保 ...
- Java 哈希表运用-LeetCode 1 Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- MongoDB分组汇总操作,及Spring data mongo的实现
转载请在页首注明作者与出处 一:分组汇总 1.1:SQL样例 分组汇总的应用场景非常多,比如查询每个班级的总分是多少,如果用关系形数据库,那么sql是这样子的 ),class from score g ...