helloworld set方法注入
2013-02-25 16:52
375 查看
首先写class
dao
service
再写beans.xml
用property就是是代表着用set方法依赖注入
测试类
test
dao
public class StudentDao { public void save(User u){ System.out.println("save "+u.getName()+" success!"); } }
service
public class StudentService { private StudentDao dao; public StudentDao getDao() { return dao; } public void setDao(StudentDao dao) { this.dao = dao; } public void add(User u){ dao.save(u); } }
再写beans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="S_dao" class="com.sl.dao.StudentDao"></bean> <bean id="S_service" class="com.sl.service.StudentService"> <property name="dao" ref="S_dao"></property> </bean> </beans>
用property就是是代表着用set方法依赖注入
测试类
test
public class test { @Test public void testOne(){ ApplicationContext appl=new ClassPathXmlApplicationContext("beans.xml"); StudentService service=(StudentService)appl.getBean("S_service"); User u=new User("hong"); service.add(u); } }
相关文章推荐
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring 配置bean, 注解方式配置 xml方式要生产set方法方便属性注入,注解方式不需要,利用反射机制注入
- 从头认识Spring-2.3 注解装配-@autowired(2)-通过set方法或者其他方法注入
- 87——SpringIOC中,通过set方法给属性注入值(5条)
- .NET下编译C++代码时出现fatal error C1083: 无法打开包括文件:“xuhk_JNIHelloWorld.h”: No such file or directory错误的解决方法。
- spring注入时bean的set方法为什么不能是static类型的?(为什么要用单例模式而不是静态方法)
- C#实现类似"hello $world"的格式化字符串方法
- L# ForUnity Helloworld的更新方法
- 项目文件.NET下编译C++代码时出现fatal error C1083: 无法打开包括文件:“xuhk_JNIHelloWorld.h”: No such file or directory错误的解决方法。
- 依赖注入1(set方法)
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- 总结Spring Set注入方式,注解在set方法上,及对property标签的理解
- 关于struts2 中action 属性没有 set 方法 依然会注入值的问题(解决)
- 17种方法写HELLO,WORLD
- 编译并连接从helloworld.c生成的汇编代码的方法步骤
- Spring通过set方法注入Bean属性
- 17种方法写HELLO,WORLD
- 从头认识Spring-2.4 基于java的标准注解装配-@Inject(2)-通过set方法或者其他方法注入
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件