spring注入

代码结构及使用的jar

 

 

Dao.java

package com.orange.test;

public class Dao {
 public void insert() {
  System.out.println("dao insert...");
 }
 
}


 

 

Service.java

package com.orange.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;


public class Service {

 private Dao dao;
 private String str;
 private Integer id;
 private Set<String> sets = new HashSet<String>();
 private List<String> list = new ArrayList<String>();
 private Properties properties = new Properties();
 private Map<String,String> map = new HashMap<String,String>();


 //省略以上7个set方法....


 public void add() {
  dao.insert();
  System.out.println(str);
  System.out.println(id);
  System.out.println(sets);
  System.out.println(list);
  System.out.println(properties.get("username")+ "--" + properties.get("password"));
  System.out.println(map.get("uname") + "--" + map.get("pwd"));
 }

 public Map<String, String> getMap() {
  return map;
 }

 public void setMap(Map<String, String> map) {
  this.map = map;
 }

 
 

}



 

 

Service2.java

package com.orange.test;

public class Service2 {

 private Dao dao;
 private String name;
 
 public Service2(Dao dao,String name){
  this.dao = dao;
  this.name = name;
 }
 
 public void add() {
  dao.insert();
  System.out.println(name);
 }


}



 

 Service3.java

package com.orange.test;
import javax.annotation.Resource;

public class Service3 {

 @Resource private Dao dao;
 public void add() {
  dao.insert();
 }
}


 

Test.java

package com.orange.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.orange.test.Service;

public class Test {

 public static void main(String[] args) {
  
  System.out.println("----set方法注入----");
  ApplicationContext context1 = new ClassPathXmlApplicationContext("beans.xml");
  Service service = (Service)context1.getBean("service");
  service.add();
  System.out.println("----构造器注入----");
  Service2 service2= (Service2)context1.getBean("service2");
  service2.add();

System.out.println("----注解注入----");
  Service3 service3= (Service3)context1.getBean("service3");
  service3.add();
 }

}



 

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">
             
   <!--bean标签内部分属性说明-->
   <!-- scope="prototype"  每次创建都是一个新的对象  getBean("")这个类时,才被实例化 --> 
   <!-- scope="singleton"  单例模式, 默认(不配置此属性) ,spring容器实例化时,类被实例化   --> 
   <!-- lasy-init="true" 实例化spring容器时不会类被实例化(延时加载),执行getBean时才被实例化  --> 
   <!-- 如想配置所有的类延时加载可在 跟标签beans标签内配置 default-lazy-init="true"-->
   <!-- init-method="" 实例化类后执行的初始化方法-->
   <!-- destroy-method="" 类被销毁(spring容器被关闭即调用close())时执行的方法-->
   
   
  <bean id="dao" class="com.orange.test.Dao"></bean>
  <bean id="service" class="com.orange.test.Service">
   <property name="dao" ref="dao"></property>
   <property name="str" value="aaaaaa"></property>
   <property name="id" value="1234"></property>
   <property name="sets">
    <set>
     <value>a</value>
     <value>b</value>
     <value>c</value>
     <value>d</value>
    </set>
   </property>
   <property name="list">
    <list>
     <value>aa</value>
     <value>bb</value>
     <value>cc</value>
     <value>dd</value>
    </list>
   </property>
   
   <property name="properties">
    <props>
     <prop key="username">root</prop>
     <prop key="password">1234</prop>
    </props>
   </property>
   
   <property name="map">
    <map>
     <entry key="uname" value="orcl"/>
     <entry key="pwd" value="abcd"/>
    </map>
   </property>
  </bean>

<!-- 构造器注入 -->
<bean id="service2" class="com.orange.test.Service2">
 <constructor-arg index="0" type="com.orange.test.Dao" ref="dao"></constructor-arg>
 <constructor-arg index="1" value="bigorange"></constructor-arg>
</bean>

  <!-- 启用注解注入 -->
    <context:annotation-config/>
    <bean id="service3" class="com.orange.test.Service3"/>

</beans>


 

<!----------------------------------beans.xml结束---------------------------------------->

执行结果: 



----set方法注入----
2014-10-23 16:08:55 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@19c26f5: display name [org.springframework.context.support.ClassPathXmlApplicationContext@19c26f5]; startup date [Thu Oct 23 16:08:55 CST 2014]; root of context hierarchy
2014-10-23 16:08:55 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2014-10-23 16:08:55 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@19c26f5]:org.springframework.beans.factory.support.DefaultListableBeanFactory@116ab4e
2014-10-23 16:08:55 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@116ab4e: defining beans [dao,service,service2]; root of factory hierarchy



dao insert...
aaaaaa
1234
[a, b, c, d]
[aa, bb, cc, dd]
root--1234
orcl--abcd
----构造器注入----
dao insert...
bigorange

----注解注入----
dao insert...


 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值