<三>Ioc控制反转——bean.xml注入对象、值和集合(附源码)
2014-11-29 11:14
615 查看
在<一>的基础上修改,后面会给出完整代码:
一、对象的注入
1、新建dao层的接口和实现类:
接口类:
2、在service层调用该dao层的方法,PersonServiceImpl代码如下:
3、在bean.xml中注册该类,使得该类在spring容器初始化时便被实例化:
4、测试,SpringTest类
二、值的注入
1、PersonServiceImpl类中,添加name属性,并生成setter和getter方法:
三、集合的注入
1、PersonServiceImpl类中添加一下四个属性并生成setter和getter方法
2、添加接口方法:
3、配置beans.xml:
4、测试,SpringTest类:
控制台显示:
完成。
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
源码下载地址:
点击打开链接
一、对象的注入
1、新建dao层的接口和实现类:
接口类:
package cn.chen.dao; public interface PersonDao { public void add(); }实现类:
package cn.chen.dao.impl; import cn.chen.dao.PersonDao; public class PersonDaoImpl implements PersonDao{ @Override public void add() { // TODO Auto-generated method stub System.out.println("我是dao层的add()方法"); } }
2、在service层调用该dao层的方法,PersonServiceImpl代码如下:
package cn.chen.service.impl; import cn.chen.dao.PersonDao; import cn.chen.service.PersonService; public class PersonServiceImpl implements PersonService{ private PersonDao personDao;//设置属性。要生成setter和getter方法 @Override public void save() { // TODO Auto-generated method stub System.out.println("我是save()方法"); personDao.add(); } // public void init(){ // System.out.println("我是初始化方法"); // } public PersonDao getPersonDao() { return personDao; } public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } }
3、在bean.xml中注册该类,使得该类在spring容器初始化时便被实例化:
<?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-2.5.xsd"> <bean id="chen" class="cn.chen.dao.impl.PersonDaoImpl"></bean> <!--class表示类的路径,id表示由spring容器来创建该类的一个对象--> <bean id="personService" class="cn.chen.service.impl.PersonServiceImpl" > <property name="personDao" ref="chen"></property> </bean> </beans>其中,
<property name="personDao" ref="chen"></property>这一行中,ref的值是和 <bean id="chen" class="cn.chen.dao.impl.PersonDaoImpl"></bean>的id一致,name的值要和<bean id="personService" class="cn.chen.service.impl.PersonServiceImpl" >中的PersonServiceImpl类的参数的名称一样(即private PersonDao personDao;)
4、测试,SpringTest类
package cn.chen.test; import org.junit.*; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.chen.service.PersonService; public class SpringTest { @BeforeClass public static void setUpBeforeClass() throws Exception{ } @Test public void instanceSpring(){ //ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");//读取spring配置文件 AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService) ctx.getBean("personService");//根据id值获得该类PersonService的对象,注意是获得,而不是创建,创建是由spring容器来完成的 personService.save(); // PersonService personService0 = (PersonService)ctx.getBean("personService"); // PersonService personService1 = (PersonService)ctx.getBean("personService"); // System.out.println(personService0==personService1); ctx.close(); } }控制台出现:
我是save()方法 我是dao层的add()方法
二、值的注入
1、PersonServiceImpl类中,添加name属性,并生成setter和getter方法:
package cn.chen.service.impl; import cn.chen.dao.PersonDao; import cn.chen.service.PersonService; public class PersonServiceImpl implements PersonService{ private PersonDao personDao; private String name; @Override public void save() { // TODO Auto-generated method stub System.out.println("我是save()方法"); System.out.println("注入的值为:"+name); personDao.add(); } // public void init(){ // System.out.println("我是初始化方法"); // } public PersonDao getPersonDao() { return personDao; } public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } public String getName() { return name; } public void setName(String name) { this.name = name; } }2、配置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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="chen" class="cn.chen.dao.impl.PersonDaoImpl"></bean> <!--class表示类的路径,id表示由spring容器来创建该类的一个对象--> <bean id="personService" class="cn.chen.service.impl.PersonServiceImpl" > <property name="personDao" ref="chen"></property> <property name="name" value="chenning"></property> </bean> </beans>3、测试,在控制台会显示
我是save()方法 注入的值为:chenning 我是dao层的add()方法
三、集合的注入
1、PersonServiceImpl类中添加一下四个属性并生成setter和getter方法
private Set<String> sets=new HashSet<String>(); private List<String> lists=new ArrayList<String>(); private Properties properties=new Properties(); private Map<String,String> maps=new HashMap<String,String>();
2、添加接口方法:
package cn.chen.service; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public interface PersonService { public Set<String> getSets(); public List<String> getLists(); public Properties getProperties(); public Map<String, String> getMaps(); public void save(); }
3、配置beans.xml:
<bean id="personService" class="cn.chen.service.impl.PersonServiceImpl" > <property name="personDao" ref="chen"></property> <property name="name" value="chenning"></property> <!-- 集合类的注入--> <property name="sets"> <set> <value>set-one</value> <value>set-two</value> <value>set-three</value> </set> </property> <property name="lists"> <list> <value>list-one</value> <value>list-two</value> <value>list-three</value> </list> </property> <property name="properties"> <props> <prop key="prop-key1">prop-value1</prop> <prop key="prop-key2">prop-value2</prop> <prop key="prop-key3">prop-value3</prop> </props> </property> <property name="maps"> <map> <entry key="map-key1" value="map-value1"/> <entry key="map-key2" value="map-value2"/> <entry key="map-key3" value="map-value3"/> </map> </property> </bean>
4、测试,SpringTest类:
package cn.chen.test; import org.junit.*; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.chen.service.PersonService; public class SpringTest { @BeforeClass public static void setUpBeforeClass() throws Exception{ } @Test public void instanceSpring(){ //ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");//读取spring配置文件 AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService) ctx.getBean("personService");//根据id值获得该类PersonService的对象,注意是获得,而不是创建,创建是由spring容器来完成的 personService.save(); // PersonService personService0 = (PersonService)ctx.getBean("personService"); // PersonService personService1 = (PersonService)ctx.getBean("personService"); // System.out.println(personService0==personService1); System.out.println("======set======"); for(String value : personService.getSets()){ System.out.println(value); } System.out.println("======list======"); for(String value : personService.getLists()){ System.out.println(value); } System.out.println("======property======"); for(Object key : personService.getProperties().keySet()){ System.out.println(key+"="+personService.getProperties().getProperty((String) key)); } System.out.println("======map======"); for(String key : personService.getMaps().keySet()){ System.out.println(key+"="+personService.getMaps().get(key)); } ctx.close(); } }
控制台显示:
我是save()方法 注入的值为:chenning 我是dao层的add()方法 ======set====== set-one set-two set-three ======list====== list-one list-two list-three ======property====== prop-key3=prop-value3 prop-key2=prop-value2 prop-key1=prop-value1 ======map====== map-key1=map-value1 map-key2=map-value2 map-key3=map-value3
完成。
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
源码下载地址:
点击打开链接
相关文章推荐
- <四>Ioc控制反转——为bean.xml瘦身第一步(附源码)
- <二>Ioc控制反转——bean id什么时候实例化(附源码)
- <五>Ioc控制反转——为bean.xml瘦身第二步(附源码)
- <一>Ioc控制反转——手把手教学:如何配置并使用单元测试一个spring框架(附源码)
- 【JavaWeb-23】spring、IoC控制反转和DI依赖注入入门、基于XML的Bean装配、基于注解的Bean装配
- <七>AOP面向切面——配置bean.xml文件方式声明切面(附源码)
- spring、IoC控制反转和DI依赖注入入门、基于XML的Bean装配、基于注解的Bean装配
- 【Spring】【对<bean>注入各种信息】【简单属性,对象属性,集合属性,构造器】
- Spring入门Blog[二、Ioc控制反转集合注入和Scope]
- SpringMVC学习-->控制反转(IoC)与依赖注入(DI
- 控制反转(IOC)、依赖注入(DI)之通过构造函数注入对象
- 读取xml文件转成List<T>对象的两种方法(附源码)
- 码农小汪-spring框架学习之2-spring IoC and Beans 控制反转 依赖注入 ApplicationContext BeanFactory
- spring容器DI依赖注入对象<三>
- 读取xml文件转成List<T>对象的两种方法(附源码)
- Spring ioc详解,IOC控制反转对象bean
- 码农小汪-spring框架学习之2-spring IoC and Beans 控制反转 依赖注入 ApplicationContext BeanFactory
- Dom4J解析xml文件动态转换为List<Bean>或者Map集合
- Spring源码阅读——简单模拟Spring的控制反转IOC和依赖注入(Bean的加载和获取)
- 控制反转(IOC)、依赖注入(DI)之使用List、Properties注入集合