第一章 spring简介

一:对象的创建

    了解spring得须了解一下对象的工厂模式    

public interface Fruit{
	public void eat();
}

public class Apple implements Fruit{
	public void eat(){
		System.out.println("吃苹果!");
	}
}

public class Orange implements Fruit{
	public void eat(){
		System.out.println("吃桔子!");
	}
}

    直接创建对象模式   

Fruit fruit = new Orange();
fruit.eat();

   工厂方式创建对象 

public class FruitFactory(){
	public static Fruit getInstance(int type){
		if(type ==1){
			return new Apple();
		}else id(type ==2){
			return new Orange();
		}
		return null;
	}
}

public class Test{	
	public static void main(String[] args) {		
		Fruit fruit = FruitFactory.getInstance(2);
		fruit.eat();
	}
}

 

   利用Spring完全解耦合(xml配置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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
	<bean id="fruit" class="model.Orange"/>
</beans>

    测试类  

public class Test{	
	public static void main(String[] args) {
		ApplicationContext act = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		Fruit fruit = (Fruit)act.getBean("fruit");
		fruit.eat();
	}
}

 

二:spring技术

   我们需要掌握Spring中的技术:

      AOP:可以给服务层的方法进行拦截

      Core:Ioc,控制反转,依赖注入

      事务的配置

   环境的搭建:

      用到哪些功能就加载哪些包,比如我现在只用到核心功能IOC这一块,就只需要导入下列包即可.(可以通过MyEclipse自动加载Spring支持的方式加入此包.)

      以下为Core功能所需要的几个包:

      spring-core.jar

      spring-context.jar

      spring-beans.jar

      log4j-1.2.15.jar

      commons-logging.jar

      commons-attributes-compiler.jar

      commons-attributes-api.jar

   配置文件的编写:

      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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

</beans>

   bean的配置:
      <bean id="fruit" class="model.Orange" />
      <bean name="/fruit" class="model.Orange" />

      id一般用来配置服务层或Dao层

      name用来配置Action层,因为需要将导航地址配置在name属性中,name属性允许特殊符号的出现,如”/”。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值