Spring:注解版依赖注入+注入collection值演示

加载的jar有:

                           log4j-1.2.16.jar   

                           org.springfaramework.asm-3.0.5.release.jar

                           org.springfaramework.beans-3.0.5.release.jar

                           org.springfaramework.context-3.0.5.release.jar

                           org.springfaramework.core-3.0.5.release.jar

                           org.springfaramework.expression-3.0.5.release.jar

                           asm-all.3.3.1.jar

                           cglib-2.2.2.jar

                           commons-logging-1.1.1.jar

   

文件结构图如下:

  

代码演示如下:

MyDao.java

package org.baicai.spring.annotation;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.stereotype.Component;

//标注组件

@Component
public class MyDao {
   public MyDao()
   {
    System.out.println("in MyDao");   
   }
   //标注生命同期方法(初始化)
   @PostConstruct
   public void init()
   {
      System.out.println("in MyDao init() ");
   }
   public void say(String name)
   {
       System.out.println(name+":你好!");
   }
   //标注生命周期方法(销毁)
   @PreDestroy
   public void close()
   {
       System.out.println("in MyDao close() ");
   }
}


MyService.java

package org.baicai.spring.annotation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//标注服务组件
@Service
public class MyService {

    //配置依赖注入
    @Autowired(required=true)
    public MyDao dao;
    
    public MyService()
    {
        System.out.println("in MyService()");
    }
    public MyDao getDao()
    {
       return dao;
    }
    public void setDao(MyDao dao)
    {
        this.dao=dao;
    }
    public void say(String name)
    {
        dao.say(name);
    }
}


Demo.java

package org.baicai.spring.annotation;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Demo {
    
    public static void main(String[] args)
    {
        AnnotationConfigApplicationContext context= new AnnotationConfigApplicationContext();
        context.registerShutdownHook();
        
        //自动扫描包里面的class 多个用,分开
       context.scan("org.baicai.spring.annotation");
       System.out.println("==========================================");
       
       //更新context状态
       context.refresh();
       System.out.println("==========================================");
       
       MyService service=context.getBean(MyService.class);
       service.say("Annotation");
       service.say("张三");
    }
}


Bean .java

package org.baicai.spring.collection;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class Bean {
    
    private List<String> list;
    private Properties properties;
    private Map<String, String> map;
    private Date date;
    public List<String> getList() {
        return list;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
    public Properties getProperties() {
        return properties;
    }
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
    public Map<String, String> getMap() {
        return map;
    }
    public void setMap(Map<String, String> map) {
        this.map = map;
    }
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
    public void showInfo() {
        System.out.printf("list:%s%nproperties:%s%nmap:%s%ndate:%s%n", list,
                properties, map, date);
    }
}


BeanOne .java

package org.baicai.spring.collection;

public class BeanOne {

}


BeanTwo .java

package org.baicai.spring.collection;


public class BeanTwo {

     private BeanOne beanOne;
    
     public  void setBeanOne(BeanOne beanOne)
     {
         this.beanOne=beanOne;
     }
     public BeanOne getBeanOne()
     {
         return beanOne;
     }
     public void showBeanOne()
     {
         System.out.println(getBeanOne());
     }
}


MyPropertyEditor.java

package org.baicai.spring.collection;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class MyPropertyEditor  extends PropertyEditorSupport {
    
    private SimpleDateFormat sdf=new SimpleDateFormat();
    private String format;
    
    public String getFormat() {
        return format;
    }
    public void setFormat(String format) {
        this.format = format;
        sdf.applyPattern(format);
    }
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        try {
            setValue(sdf.parse(text));
        } catch (ParseException e) {
            throw new IllegalArgumentException();
        }
    }
}


context.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-3.0.xsd">
           <!-- 注册属性编辑器(类型转换) -->
            <bean id="datePropertyEditor"  class="org.baicai.spring.collection.MyPropertyEditor">
                <property name="format" value="yyyy-MM-dd"></property>
            </bean>
            
            <bean id="customEditorConfigurer"             class="org.springframework.beans.factory.config.CustomEditorConfigurer">
               <property name="customEditors">
                   <map>
                       <!-- 注册java.util.Date类型由dataPropertyEditor负责转换 -->
                       <entry key="java.util.Date" value-ref="datePropertyEditor"></entry>
                   </map>
               </property>
            </bean>
            
            <!-- List集合对象 -->
            <bean id="list" class="java.util.ArrayList">
                <constructor-arg index="0">
                     <list>
                          <value>one</value>
                          <value>two</value>
                          <value>three</value>
                     </list>
                </constructor-arg>
            </bean>
            
            <!-- 集合以及特殊字段的注入(p:属性注入方式是xmlns:p="http://www.springframework.org/schema/p"提供的功能) -->
            <bean id="bean" class="org.baicai.spring.collection.Bean" p:date="1991-01-01">
               <property name="list" ref="list"></property>
               <property name="properties">
                  <props>
                      <prop key="one">一</prop>
                      <prop key="two">二</prop>
                      <prop key="three">三</prop>
                      <prop key="four">四</prop>
                  </props>
               </property>
               <property name="map">
                  <map>
                     <entry key="Michael" value="米迦勒(天使名),迈克尔(人名)"></entry>
                      <entry key="Control" value="控制"></entry>
                  </map>
               </property>
            </bean>
            
            <!--  scope默认为单例(singleton)应用程序每次调用的都是同一个Bean prototype 每调用一次Bean对象都会重新产生  -->
            <bean id="beanOne" class="org.baicai.spring.collection.BeanOne" scope="prototype"/>
            
            <bean id="beanTwo" class="org.baicai.spring.collection.BeanTwo" >
               <!-- 普通版本
                <property name="beanOne" ref="beanOne"/>
                -->
                <!-- 方法注入版本 -->
              <lookup-method bean="beanOne" name="getBeanOne"/>
            </bean>
    </beans>


执行结果如下:


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值