Spring 配置的两种方式

一:Spring的IOC与DI
IOC (Inverse Of Control) 控制反转
控制反转是将组建依赖关系的创建与管理置于程序之外的技术。控制反转IoC意味着将你设计好的类交由系统去控制,而不是由你的类内部控制。将类的创建与获取提取到外部。因为使用IOC技术来创建和管理对象,所以我们通常又将IOC称之为“IOC容器”。
由IOC容器控制程序间的关系,而不是由应用程序直接控制。
由于控制权由应用程序转向了IOC容器,所以称之为反转。

DI(Dependency Injection) 依赖注入
依赖注入是指我们获取一个对象时,不再需要手动new(),而是由IOC容器进行创建与获取。

IOC与DI的关系,IOC是宏观上看控制权的转换,DI则是从微观上看控制权时如何转换的,二者是一码事。
二:IOC/DI实现方式
1.Setter注入:使用setXXXX()方法进行注入
2.使用构造方法进行注入
三:IOC容器的Bean作用域
Bean Scope 属性
singleton 单例模式,在ioc容器中有且只有一个对象
prototype多例模式,每次从容器中获取到的都是新对象。
[color=red]四:使用注解配置[/color]
IOC 组件
@Service – 标记业务bean
@Controller –标记 Action
@Repository – 标记DAO
@Component –不好归类时,使用Component
注入方式
@Resource 按名称装配
@Autowired 按类型装配

例子:
第一步:在applicationContext.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/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
[color=red]<context:component-scan base-package="com.bjsxt.jlls"></context:component-scan>[/color]
//当容器初始化时扫描com.bjsxt.jlls包,把它这个包以及子包中有注解标注的就把它实例化放到ioc容器中。

</beans>
第二步:package com.bjsxt.jlls.dao

@Repository
//@Component
public class CustomerDAO {
public CustomerDAO(){
System.out.println("实例化CustomerDAO");
}

public void save(){
System.out.println("调用了使用Hibernate方法实现的CustomerDAO.save()方法");
}
}
第三步:
@Service
//@Component
public class CustomerService {

//IOC容器中有木有name="customerDAO"的对象
@Resource(name = "customerDAO")
private CustomerDAO cDAO = null;
@Resource(name = "purchaseDAO")
private PurchaseDAO pDAO = null;

public CustomerService(){
System.out.println("实例化CustomerService");
}

public void save(){
System.out.println("调用了CustomerService.save()方法");
cDAO.save();
pDAO.save();
}

public CustomerDAO getcDAO() {
return cDAO;
}

public void setcDAO(CustomerDAO cDAO) {
this.cDAO = cDAO;
}

public PurchaseDAO getpDAO() {
return pDAO;
}

public void setpDAO(PurchaseDAO pDAO) {
this.pDAO = pDAO;
}


}
第四步:
@Component
public class CustomerAction {
@Resource
private CustomerService customerService = null;

public CustomerAction(){
System.out.println("实例化CustomerAction");
}

public String save(){
System.out.println("调用了CustomerAction.save()方法");
customerService.save();
return null;
}
public CustomerService getCustomerService() {
return customerService;
}
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
}
第五步:测试,注解方法[color=red]获取对象默认的对象名称为类名第一个字母小写如:customerDao[/color]
public class CustomerActionTester {
@Test
public void testSave(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//CustomerDAO cdao = (CustomerDAO)ctx.getBean("customerDAO");
//cdao.save();

//CustomerService cs = (CustomerService)ctx.getBean("customerService");
//cs.save();

CustomerAction action = (CustomerAction)ctx.getBean("customerAction");
action.save();
}
}
五:配置文件配置
在applicationContext
<?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/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<!-- . 具体配置... -->
<bean name="customerDAO" class="com.bjsxt.jlls.dao.CustomerDAO"></bean>
<bean name="customerJdbcDAO" class="com.bjsxt.jlls.dao.CustomerJdbcDAO"></bean>
<bean name="purchaseDAO" class="com.bjsxt.jlls.dao.PurchaseDAO"></bean>

<bean name="customerService" class="com.bjsxt.jlls.service.CustomerService">
<property name="customerDAO" ref="customerJdbcDAO"></property>
<property name="purchaseDAO" ref="purchaseDAO"></property>
</bean>
<bean name = "customerAction" class="com.bjsxt.jlls.action.CustomerAction">
<property name="customerService" ref = "customerService"></property>
</bean>
</beans>
具体的类中也是创建对象赋值为null,get set 方法。
使用配置文件配置的好处是:由于软件设计的开闭原则当你软件上线后不是因为软件软件的bug源代码而是因为软件功能无法预期需求是不允许修改的。这个设计原则是在不修改源代码的前提下对软件功能进行扩展的目的。想要达到这个效果就要使用Spring的AOP原则。
例如:程序开始时使用的是Hibernate对dao层进行操作,后来感觉不够完善,改成用jdbc,不修改源代码的实现如下:继承加上重写save方法来实现,
同时<property name="customerDAO" ref="[color=red]customerJdbcDAO[/color]"></property>
public class CustomerJdbcDAO extends CustomerDAO{
public void save(){
System.out.println("调用了JDBC方式实现的CustomerDAO.save()方法");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值