Spring 简单介绍 Spring 注入

1.Spring 是什么
Spring 是一个开源的控制反转的(IoC) 和面向切面的(AOP) 的容器框架。它主要目的是简化企业开发。

控制反转: 所谓控制反转就是应用本身不负责依赖对象的创建及维护,依赖对象的创建及维护是由外部容器负责的。这样控制权就由应用转移到了外部容器,控制权的转移就是所谓的反转。

依赖注入: 所谓依赖注入就是 在运行期,由外部容器动态地将依赖对象注入到组件中。

2.使用spring 2.5 需要的jar
必须使用的是 spring.jar 和 commons-logging.jar
如果使用了切面编程(aop),还需要
aspectjweaver.jar
aspectjrt.jar
cglib-nodep-2.1_3.jar 
如果使用了JSR-250中的注解,还需要
common-annotations.jar

3.实例化spring容器
方法一: 在类路径下寻找配置文件来实例化容器
ApplicationContext ctx  = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});

方法二:
在文件系统路径下寻找配置文件来实例化容器(不常用)
ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[]{"d://beans.xml"});

4.手动添加schema文件
windows --> perferences --> myeclipse --> files and editors -->xml --> xmlcatalog
点击"and" ,  在Location中选择"文件路径" 将 Key Type 改为Schema location , Key 改为 原有路径加上文件名如 /beans-2.5.xsd

5.三种实例化bean的方式
(1)使用类构造器实例化
<bean id="personService" class="com.hf.service.impl.PersonServiceBean" />
(2)使用静态工厂方法实例化
    <bean id="personService2" class="com.hf.service.impl.PersonServiceBeanFactory"
    factory-method="createPersonServiceBean" />

package com.hf.service.impl;
public class PersonServiceBeanFactory {
   public static PersonServiceBean createPersonServiceBean(){
    return  new PersonServiceBean();
   }
}

(3)使用实例工厂方法实例化
  
<bean id="personServiceFactory" class="com.hf.service.impl.PersonServiceBeanFactory" />
<bean id="personService3"  factory-bean="personServiceFactory" factory-method="createPersonServiceBean2" />

package com.hf.service.impl;
public class PersonServiceBeanFactory {
   public  PersonServiceBean createPersonServiceBean2(){
    return  new PersonServiceBean();
   }
}
 
6.默认情况下
<bean id="personService" class="com.hf.service.impl.PersonServiceBean" />
PersonService personService= (PersonService)cxt.getBean("personService");
每次调用bean 获取的是同一个引用(单实例)

<bean id="personService" class="com.hf.service.impl.PersonServiceBean"  scope="prototype"/>
每次调用bean 获取的是新的引用

疑问:Spring作用域都有哪些 scope

7.初始化bean
默认情况下 bean 在spring容器实例化时就被实例化了
scope="prototype" 时 是在调用getBean方法是实例化bean的
lazy-init="true"  时 是在调用getBean方法是实例化bean的 (延迟初始化)
在beans 节点 输入default-lazy-init="true" 则所有的bean对象都会(延迟初始化)
init-method="init" bean被实例化后就执行的初始化方法  init为要初始化的方法名称

destroy-method="destroy"  bean被销毁后执行的方法  destroy为要销毁bean的方法名称
容器被关闭时及bean被销毁
<bean id="personService" class="com.hf.service.impl.PersonServiceBean"
    init-method="init"  destroy-method="destroy"/>
AbstractApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
cxt.close();


8. 注入集合的方法
ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService1= (PersonService)cxt.getBean("personService");
------Set集合-------------------------------------------
实现类
 private Set<String> sets = new HashSet<String>();
 public Set<String> getSets() {
  return sets;
 }
 public void setSets(Set<String> sets) {
  this.sets = sets;
 }

接口
public Set<String> getSets();
配置xml
<bean id="personService" class="com.hf.service.impl.PersonServiceBean">
    <property name="sets">
     <set>
      <value>第1个</value>
      <value>第2个</value>
      <value>第3个</value>
     </set>
    </property>
</bean>
---------List集合--------------------------------------------
同理
private List <String> lists = new ArrayList<String>();
   <property name="lists">
    <list>
      <value>第w1个</value>
      <value>第w2个</value>
      <value>第w3个</value>
    </list>
   </property>

---------Properties  集合---------------------------------------
实现类
private Properties properties = new Properties();
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}

接口
public Properties getProperties();

xml配置
<bean id="personService" class="com.hf.service.impl.PersonServiceBean">

   <property name="properties">
     <props>
       <prop key="key1">value1</prop>
       <prop key="key2">value2</prop>
       <prop key="key3">value3</prop>
     </props>
   </property>
</bean>
 
输出
 for(Object key : personService1.getProperties().keySet()){
    System.out.println(key+"="+personService1.getProperties().getProperty((String)key));   
}

--------Map集合-----------------------------------
实现类
private Map<String,String> maps =new HashMap<String,String>();
public Map<String, String> getMaps() {
  return maps;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
接口
public Map<String, String> getMaps();

xml配置
<bean id="personService" class="com.hf.service.impl.PersonServiceBean">
    <property name="maps">
      <map>
        <entry key="key-1" value="value-1"></entry>
        <entry key="key-2" value="value-2"></entry>
        <entry key="key-3" value="value-3"></entry>
      </map>
    </property>
</bean>

输出
for(String key : personService1.getMaps().keySet()){
  System.out.println(key+"="+personService1.getMaps().get(key));   
}

9.通过构造器的方式注入值

实现类
private PersonDao persondao;
private String name;
//构造函数
public PersonServiceBean(PersonDao persondao, String name) {
this.persondao = persondao;
this.name = name;}

//打印控制台
public void save(){
System.out.println(name+ persondao.show());
}

xml配置
<bean id="personDao" class="com.hf.dao.impl.PersonDaoBean" />
<bean id="personService" class="com.hf.service.impl.PersonServiceBean">
  <constructor-arg  index="0" type="com.hf.dao.PersonDao" ref="personDao" />
  <constructor-arg index="1" value="马总" />
</bean>


xml 手工注入分 构造器注入和属性(get set)注入
依赖注入还有注解的方式
包括注解方式 及以上方式都属于   手工注入
注解注入方式的好处是:是xml文件简化减小臃肿

 

自动注入方式(不建议使用)
 
<bean id="personDao1" class="com.hf.dao.impl.PersonDaoBean"></bean>
<bean id="personService" class="com.hf.service.impl.PersonServiceBean" autowire="byType">

autowire属性如下:
. byType:按类型装配,可以根据属性的类型, 在容器中找到跟该类型匹配的bean. 如果发现多个则将抛出异常,如果没找到及属性值为null.
. byName: 按名称装配,可以根据属性的名称, 在容器中寻找跟该属性名相同的bean,如果没找到及属性为null。
. constructor与byType 的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有中找到与构造器参数类型一致的bean ,那么将会抛出异常。
. autodetect:通过bean类型的自省机制(introspection)来决定是使用constructor还是使用byType方式进行自动装配。
  如果发现默认的构造器,那么将使用byTypte 方式。
autowire="byName"按属性名称装配 没找到返回null

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值