zoukankan      html  css  js  c++  java
  • spring中3中为bean注入值的办法总结

    有三种办法,分别是:

    • Normal way
    • Shortcut
    • “p” schema

    假设我们现在有这么一个bean:

    public class FileNameGenerator 
    {
    	private String name;
    	private String type;
     
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public String getType() {
    		return type;
    	}
    	public void setType(String type) {
    		this.type = type;
    	}
    }
    

      

    1. Normal way

    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
     
    	<bean id="FileNameGenerator" class="com.mkyong.common.FileNameGenerator">
    		<property name="name">
    			<value>mkyong</value>
    		</property>
    		<property name="type">
    			<value>txt</value>
    		</property>
    	</bean>
    </beans>
    

      

    2. Shortcut

    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
     
    	<bean id="FileNameGenerator" class="com.mkyong.common.FileNameGenerator">
    		<property name="name" value="mkyong" />
    		<property name="type" value="txt" />
    	</bean>
     
    </beans>
    

      

    3. “p” schema

    <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="FileNameGenerator" class="com.mkyong.common.FileNameGenerator" 
                 p:name="mkyong" p:type="txt" />
     
    </beans>
    

      第三种办法需要加入: xmlns:p=”http://www.springframework.org/schema/p

    三种办法都很好,具体选哪个,看个人喜好。


    ==============================================================================

    本博客已经废弃,不在维护。新博客地址:http://wenchao.ren


    我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他
    们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其
    实我是一个程序员

    ==============================================================================
  • 相关阅读:
    100行代码实现了多线程,批量写入,文件分块的日志方法
    阿里云客户端开发技巧
    阿里云客户端的实现(支持文件分块,断点续传,进度,速度,倒计时显示)
    类库间无项目引用时,在编译时拷贝DLL
    数据库-锁的实践
    Node.js学习资料
    文档流转,文档操作,文档归档(一)
    滑动验证码研究-后续
    iTextSharp 116秒处理6G的文件
    在职场中混,"讲演稿"的重要性
  • 原文地址:https://www.cnblogs.com/rollenholt/p/2835094.html
Copyright © 2011-2022 走看看