Setter Injection
来源:互联网 发布:中石油logo相关数据 编辑:程序博客网 时间:2024/05/29 18:08
在bean中通过提供属性的setter方法,在Spring配置文件中进行注入。实现了一定的松耦合。
步骤一:新建Java工程-SpringDemo。
步骤二:导入Spring JAR包和commons-logging JAR包。
步骤三:编写bean
public class Animal {private String category;private String name;private int age;public String getCategory() {return category;}public String getName() {return name;}public int getAge() {return age;}@Overridepublic String toString() {return "Animal [category=" + category + ", name=" + name + ", age="+ age + "]";}}
步骤四:编写Spring配置文件(位于src目录下)
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="panda" class="cn.zj.hz.Animal" > <property name="category" value="Mammal" /> <property name="name" value="panpan" /> <property name="age" value="3" /></bean></beans>
步骤五:编写测试类:
public class App {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("SpringConfig.xml");Animal panda = (Animal) context.getBean("panda");System.out.println(panda);}}
步骤六:运行,查看结果:
Animal [category=Mammal, name=panpan, age=3]
项目结构:
- Setter Injection
- Setter Injection 简单实现
- Setter-based dependency injection
- Setter-based dependency injection
- Spring学习笔记(二)----Constructor Injection or Setter Injection
- Difference between Setter Injection vs Constructor Injection in Spring framework
- Spring学习笔记(二)----Constructor Injection or Setter Injection
- setter
- Unity Application Block 1.0系列(3): 属性/设值方法注入(Property/Setter Injection)
- getter/setter
- setter,getter
- getter、setter
- setter方法
- getter ,setter
- #计算-setter
- SQL Injection
- Method Injection
- Javascript Injection
- 字符设备驱动程序学习笔记一
- 安卓图片OCR识别技术 tesseract研究文档-20130328-v1
- 黑马程序员-------Java编程基础上
- 寒假练习 1002 Roman numerals
- My First robt TestCase
- Setter Injection
- 连上篇,此片为编辑页
- ViewState的使用
- 有关服务器主程年总结
- 配置文本编辑器
- 错误代码!!!
- 寒假练习 1003 Exchange for Cola
- 搭建SVN服务端
- 判断一个链表是否有环