java_spring_依赖注入
2014-03-29 12:23
453 查看
IOC反转控制 PersonService属性 PersonDao personDao接受通过xml注入的对象 PersonDaoBean. Person中save()调用 PersonDaoBean中的add()
package com.PersonDaoBean.test;
public interface PersonDao {
public abstract void add();
}
package com.PersonDaoBean.test;
//依赖注入
public class PersonDaoBean implements PersonDao {
@Override
public void add(){
System.out.println("PersonDaoBean执行。。。。。。。。。。。。。。。。。");
}
}
package com.bean.www;
import com.PersonDaoBean.test.PersonDao;
import com.dao.bean.www.PersonServiceDao;
public class PersonServiceBean implements PersonServiceDao {
//personDao接受PersonDaoBean注入的对象
private PersonDao personDao;
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
public void save() {
personDao.add();
}
}
<?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="personDao" class="com.PersonDaoBean.test.PersonDaoBean"></bean>
<bean id="personService" class="com.bean.www.PersonServiceBean">
<property name="personDao" ref="personDao"></property>
</bean>
</beans>
package com.itcast.www;
import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dao.bean.www.PersonServiceDao;
public class TestCaseDemo {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void instanceSpring() {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
PersonServiceDao personService = (PersonServiceDao) ctx
.getBean("personService");
personService.save();
}
}
package com.PersonDaoBean.test;
public interface PersonDao {
public abstract void add();
}
package com.PersonDaoBean.test;
//依赖注入
public class PersonDaoBean implements PersonDao {
@Override
public void add(){
System.out.println("PersonDaoBean执行。。。。。。。。。。。。。。。。。");
}
}
package com.bean.www;
import com.PersonDaoBean.test.PersonDao;
import com.dao.bean.www.PersonServiceDao;
public class PersonServiceBean implements PersonServiceDao {
//personDao接受PersonDaoBean注入的对象
private PersonDao personDao;
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
public void save() {
personDao.add();
}
}
<?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="personDao" class="com.PersonDaoBean.test.PersonDaoBean"></bean>
<bean id="personService" class="com.bean.www.PersonServiceBean">
<property name="personDao" ref="personDao"></property>
</bean>
</beans>
package com.itcast.www;
import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dao.bean.www.PersonServiceDao;
public class TestCaseDemo {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void instanceSpring() {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
PersonServiceDao personService = (PersonServiceDao) ctx
.getBean("personService");
personService.save();
}
}
相关文章推荐
- 详解Java Spring各种依赖注入注解的区别
- JavaWeb Spring依赖注入深入学习
- 详解Java Spring各种依赖注入注解的区别
- java模拟spring实现依赖注入
- JAVA入门[3]—Spring依赖注入
- JAVA框架当中Spring的依赖注入和控制反转
- spring依赖注入中获取JavaBean
- java学习笔记之Spring依赖注入和控制反转
- java面试题——三大框架的核心:Java反射机制和Spring控制反转和依赖注入
- Java Spring的依赖注入
- 【Java】 Spring依赖注入小试牛刀:编写第一个Spring ApplicationContext Demo
- 11级_Java_曹建波 04.24 Spring的配置、生命周期、依赖注入bean的操作
- 【java】关于spring依赖注入。
- 非web的JAVA应用使用Spring的依赖注入
- Java Spring MVC入门(六)——依赖注入之实践
- java线程中如何使用spring依赖注入
- Java后台框架篇--Spring依赖注入和控制反转
- java简单模拟Spring的依赖注入
- 【JavaWeb-23】spring、IoC控制反转和DI依赖注入入门、基于XML的Bean装配、基于注解的Bean装配
- javaspring中依赖注入原理及举例