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

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

看下现在的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;
	@Override
	public void save() {
		// TODO Auto-generated method stub
		System.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;
	@Override
	public void save() {
		// TODO Auto-generated method stub
		System.out.println("我是save()方法");
		System.out.println("注入的值为:"+name);
		personDao.add();
	}
       //不用去找name="chen",应该是根据类型由spring容器自动注入的
	@Resource
	public 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 {

	@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();
		ctx.close();
	}
}


完成。

源码下载:

点击打开链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值