<四>Ioc控制反转——为bean.xml瘦身第一步(附源码)

来源:互联网 发布:erp系统网络架构图 编辑:程序博客网 时间:2024/06/05 16:45

基于<三>中的第一部分源码(即对象的注入)进行添加,后面会附有完整代码。

看下现在的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>

这意味着我们

①每创建一个类交给spring管理,就需要<bean id="chen" class="cn.chen.dao.impl.PersonDaoImpl"></bean>这样一句语句;

②并且在一个类中调用其他类,交给spring管理的话,就需要一个<property name="personDao" ref="chen"></property>属性,并且需要在类中生成setter和getter方法。

因此,如果项目较大的话,beans.xml文件就会显得非常臃肿难以维护。因此需要进行瘦身。

接下来,先讲下瘦身第一步,将<property name="personDao" ref="chen"></property>属性去掉。话不多说,上代码:

1、对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:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd                      http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-2.5.xsd"           >           <context:annotation-config/>           <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>
(一般来说值的注入就直接写了比较方便) 在 bean.xml文件头加入的四句语句,

xmlns:context="http://www.springframework.org/schema/context"

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 <context:annotation-config/>

是配置作用,作用是让spring容器隐式注册了多个对注释进行解析处理的处理器,比如AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor等。
2、修改PersonServiceImpl类如下:

package cn.chen.service.impl;import java.util.*;import javax.annotation.Resource;import cn.chen.dao.PersonDao;import cn.chen.service.PersonService;public class PersonServiceImpl implements PersonService{//不需用setter和getter方法了,@Resource(name="chen")的name值是bean.xml中的//<bean id="chen" class="cn.chen.dao.impl.PersonDaoImpl"></bean>的id值@Resource(name="chen") private PersonDao personDao;private String name;@Overridepublic void save() {// TODO Auto-generated method stubSystem.out.println("我是save()方法");System.out.println("注入的值为:"+name);personDao.add();}//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;}}

另外一种改法如下,两种都可以:

声明private PersonDao personDao;并生成setter方法,在setter方法前加@Resource即可

package cn.chen.service.impl;import java.util.*;import javax.annotation.Resource;import cn.chen.dao.PersonDao;import cn.chen.service.PersonService;public class PersonServiceImpl implements PersonService{//不需用setter和getter方法了,@Resource(name="chen")的name值是bean.xml中的//<bean id="chen" class="cn.chen.dao.impl.PersonDaoImpl"></bean>的id值//@Resource(name="chen") private PersonDao personDao;private PersonDao personDao;private String name;@Overridepublic void save() {// TODO Auto-generated method stubSystem.out.println("我是save()方法");System.out.println("注入的值为:"+name);personDao.add();}       //不用去找name="chen",应该是根据类型由spring容器自动注入的@Resourcepublic void setPersonDao(PersonDao personDao) {this.personDao = personDao;}public String getName() {return name;}public void setName(String name) {this.name = name;}}


3、测试,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 {@BeforeClasspublic 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();ctx.close();}}


完成。

源码下载:

点击打开链接

0 0