求知过程之Spring注入bean

1、setter方式注入

主文件:SpringBeanTest

package cn.spring.library.controller;

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

public class SpringBeanTest {
    
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config/spring-test.xml");
        SpringBeanContent springBeanContent = (SpringBeanContent) context.
        		getBean("springBeanContent");
        springBeanContent.setAge(50);
        springBeanContent.perform();
    }
}

内容展示页面:SpringBeanContent

package cn.spring.library.controller;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import java.util.Properties;

public class SpringBeanContent {
	private String name;
    private int age;
    private BeanRecite beanRecite;
    private List<String> list;
    private Map<String, String> map;
    private Properties properties;
    
    public void perform() {
        System.out.println("name : " + name);
        System.out.println("age : " + age);
        beanRecite.recite();
        for(String val : list) {
            System.out.println("in list : " + val);
        }
        for(Entry<String, String> entry : map.entrySet()) {
            System.out.println("in map : " + entry.getKey() + ":" + entry.getValue());
        }
        for(Entry<Object, Object> entry : properties.entrySet()) {
            System.out.println("in properties : " + entry.getKey() + ":" + entry.getValue());
        }
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public BeanRecite getBeanRecite() {
        return beanRecite;
    }
    public void setBeanRecite(BeanRecite beanRecite) {
        this.beanRecite = beanRecite;
    }
    public List<String> getList() {
        return list;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
    public Map<String, String> getMap() {
        return map;
    }
    public void setMap(Map<String, String> map) {
        this.map = map;
    }
    public Properties getProperties() {
        return properties;
    }
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}

信息提示页面:BeanRecite

package cn.spring.library.controller;

public class BeanRecite {
	public void recite(){
		System.out.println("这是recite方法:你好");
	}
}

配置文件:spring-test.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-3.2.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<bean id="springBeanContent" class="cn.spring.library.controller.SpringBeanContent">
	    <property name="name" value="jorn" />
	    <property name="age" value="22" />
	    <property name="beanRecite" ref="beanReciteLink" />
	    <property name="list">
	      <list>
	        <value>hello</value>
	        <value>world</value>
	      </list>
	    </property>
	    <property name="map">
	      <map>
	        <entry key="key1" value="value1" />
	        <entry key="key2" value="value2" />
	        <entry key="key3" value="value3" />
	      </map>
	    </property>
	    <property name="properties">
	      <props>
	        <prop key="PKEY01">PKEY01 PKEY01 PKEY01</prop>
	        <prop key="PKEY02">PKEY02 PKEY02 PKEY02</prop>
	        <prop key="PKEY03">PKEY03 PKEY03 PKEY03</prop>
	      </props>
	    </property>
	  </bean>
	  
	  <bean id="beanReciteLink" class="cn.spring.library.controller.BeanRecite">
	  
	  </bean>

</beans>

运行结果:

name : jorn
age : 50
这是recite方法:你好
in list : hello
in list : world
in map : key1:value1
in map : key2:value2
in map : key3:value3
in properties : PKEY03:PKEY03 PKEY03 PKEY03
in properties : PKEY02:PKEY02 PKEY02 PKEY02
in properties : PKEY01:PKEY01 PKEY01 PKEY01

个人总结:在配置文件中统一管理bean即对象,项目中直接通过id即可调用改对象,省去了多处调用多处实例化的尴尬!但仅仅是为了这个目的吗(不需要new实例化了)(语言理解有待精炼。)

2、构造器注入

package cn.spring.library.controller;

/**
 * 展示类
 * 
 * 应用场景:构造器注入
 * 
 */
public class SpringContentType002 {
	private String name;
    private Integer age;
    private BeanRecite beanRecite;
    
    public SpringContentType002(BeanRecite beanRecite, String name, Integer age){
    	this.beanRecite = beanRecite;  
        this.name = name;  
        this.age = age;
    }
    
    public void perform() {
        System.out.println("name : " + name);
        System.out.println("age : " + age);
        beanRecite.recite();
    }
}

配置文件添加:

<!-- 构造器注入 -->
<bean id="springContentType002" class="cn.spring.library.controller.SpringContentType002">
	<constructor-arg index="0" type="cn.spring.library.controller.BeanRecite" ref="beanReciteLink" />
	<constructor-arg index="1" type="java.lang.String" value="jory" />
	<constructor-arg index="2" type="java.lang.Integer" value="23" />
</bean>

3、静态工厂的方法注入

package cn.spring.library.controller;

import cn.spring.library.dao.IFactoryDao;

/**
 * 展示类
 * 
 * 应用场景:静态工厂的方法注入
 * 
 */
public class SpringContentType003 {
	
	/**
	 * 注入对象
	 * 
	 */  
    private IFactoryDao staticFactoryDao;  
      
    public void staticFactoryOk(){  
    	//staticFactoryDao = new StaticFacotryDaoImpl();
        staticFactoryDao.saveFactory();  
    }  
    
    /**
	 * 注入对象的set方法  
	 * 
	 */ 
    public void setStaticFactoryDao(IFactoryDao staticFactoryDao) {  
        this.staticFactoryDao = staticFactoryDao;  
    }  
}
接口类:IFactoryDao

package cn.spring.library.dao;

public interface IFactoryDao {
	
	/**
	 * 注入对象
	 * 
	 */
	public void saveFactory();
}
实现类:StaticFacotryDao001Impl

package cn.spring.library.dao.impl;

import cn.spring.library.dao.IFactoryDao;

public class StaticFacotryDao001Impl implements IFactoryDao {
	
	public StaticFacotryDao001Impl(){
		System.out.println("这是StaticFacotryDao001Impl的构造函数,不带参数");
	}
	
	public StaticFacotryDao001Impl(String name){
		System.out.println("这是StaticFacotryDao001Impl的构造函数,带参数name:"+name);
	}
	
	@Override
	public void saveFactory() {
		System.out.println("hello world");
	}
}
工厂类:

package cn.spring.library.factory;

import cn.spring.library.dao.IFactoryDao;
import cn.spring.library.dao.impl.StaticFacotryDao001Impl;

public class DaoFactory {

    /**
      * 静态工厂  
      * 
      */
    public static final IFactoryDao getStaticFactoryDao001Impl(){  
        return new StaticFacotryDao001Impl();  
    } 
    
}
配置文件添加:

<!-- 静态工厂的方法注入 -->
<bean name="springContentType003" class="cn.spring.library.controller.SpringContentType003" >  
     <!--使用静态工厂的方法注入对象,对应下面的配置文件-->  
     <property name="staticFactoryDao" ref="staticFactoryDaoLink"></property>  
</bean>
    
<!--此处获取对象的方式是从工厂类中获取静态方法-->  
<!-- <bean name="staticFactoryDaoLink" class="cn.spring.library.dao.impl.StaticFacotryDaoImpl"></bean>  -->
<bean name="staticFactoryDaoLink" class="cn.spring.library.factory.DaoFactory" factory-method="getStaticFactoryDao001Impl"></bean>  

个人总结:对于工厂模式调查了一番,有点不明白书面上讲的好处,体现在什么地方!

4、实例工厂的方法注入

工厂类:

package cn.spring.library.factory;

import cn.spring.library.dao.IFactoryDao;
import cn.spring.library.dao.impl.StaticFacotryDao001Impl;

public class DaoFactory {

    /**
      * 实例工厂  
      * 
      */
    public IFactoryDao getStaticFactoryDao001Impl(){  
        return new StaticFacotryDao001Impl();  
    } 
    
}
配置文件添加:
<!-- 静态工厂的方法注入 -->
<bean name="springContentType003" class="cn.spring.library.controller.SpringContentType003" >  
     <!--使用静态工厂的方法注入对象,对应下面的配置文件-->  
     <property name="staticFactoryDao" ref="staticFactoryDaoLink"></property>  
</bean>
	 
<!--此处获取对象的方式是从工厂类中获取实例方法-->  
<bean name="daoFactory" class="cn.spring.library.factory.DaoFactory"></bean>  
<bean name="staticFactoryDaoLink" factory-bean="daoFactory" factory-method="getStaticFactoryDao001Impl"></bean>

总结:bean的四种注入方式,练习了一遍,但是还是有许多不明白的地方!
另外注意:通过Spring创建的对象默认是单例的,如果需要创建多实例对象可以在<bean>标签后面添加一个属性:
<bean name="..." class="..." scope="prototype">  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值