zoukankan      html  css  js  c++  java
  • JAVA_OA管理系统(三):Spring参数注入

    模块总述:

    分为五个模块:诸如基本值,注入Bean对象,注入集合,注入spring表达式,注入null或者空字符串。
    /**
     *  Java bean
     *   规范
     *   1 这个类必须有包
     *   2 必须继承  Serializable
     *   3 有无参的构造器
     *   4 有get set 方法
     *   
     *   注意:其实现在用注解的比较多,但是xml是最基本的。
     */



    一.注入基本值

    <value>元素可以通过字符串指定属性或者构造器的值,容器会将字符串从
    Java.lang.String类型转化为实际的属性或参数类型然后注入给bean


    <bean id="student" class="com.throne.Student" >
       <property name="name" value=“FontThrone”></property></span>
      <property name="age">
    <span style="white-space:pre">	</span><value>18<value>
    </property>
     </bean>

    private String name;
    	private String age;
    	public Student() {
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public String getAge() {
    		return age;
    	}
    	public void setAge(String age) {
    		this.age = age;
    	}

    String str="applicationContext.xml";
    		ApplicationContext ac=
    			new ClassPathXmlApplicationContext(str);
    		Student s=ac.getBean("student",Student.class);
    		System.out.println(s.getAge()+"
    "+s.getName());

    结果:
    18
    FontThrone

    二.注入Bean对象

    <bean id="student" class="com.lpc.entity.Student" >
       //交给容器管理,注册时,如果是Bean对象,要用ref引用
        <property name="book" ref="book"></property>
      </bean>
     
      <bean id="book" class="com.throne.Book">
         <property name="bookId" value="12"></property>
         <property name="bookName" value="平凡的世界"></property>
      </bean>



    <span style="white-space:pre">	</span>private Book book;
    	public Student() {
    	}
    	...
    	public Book getBook() {
    		return book;
    	}
    	
    	public void setBook( Book book) {
    		this.book = book;
    	}
    	

    //之前的重复部分(创建ApplicationContext等)省略掉
    System.out.println(s.getBook().getBookName());
    
    结果:
    <span style="white-space:pre">	</span>平凡的世界

    三.注入集合

    	<bean id="message" class="com.throne.entity.Message">
    	   <property name="list" ref="ppp">
    	   </property>
    	   <property name="set">
    	       <set>
    	          <value>张三</value>
    	          <value>李四</value>
    	          <value>王五</value>
    	       </set>
    	   </property>
    	   <property name="map">
    	       <map>
    	          <entry key="语文" value="80"></entry>
    	          <entry key="数学" value="100"></entry>
    	          <entry key="英语" value="60"></entry>
    	       </map>
    	   </property>
    
    	 <!--  
    	 1  采用引入的方式注入集合
                 a 先声明集合bean,然后采用引用的方式将这些bean注入给Message
                 使用<util>标签声明集合bean,然后在MessageBean下使用<property>标签引用这些集合bean。
             2  为什么要使用引入的方式
                引入的方式就是为了我们方便复用代码  !important  
                如果别的bean里面也要复用  那么就用它 
           -->
    	<util:list id="ppp">
    	          <value>兰州</value>
    	          <value>北京</value>
    	          <value>长沙</value>
    	</util:list> 



            private List<String> list;
    	private Set<String> set;
    	private Map<String,Object> map;
    	private Properties properties;
    //	注意导入的是until的包,而非sun的:import java.util.Properties;
    	public Message() {
    	}
    	public List<String> getList() {
    		return list;
    	}
    	public void setList(List<String> list) {
    		this.list = list;
    	}
    	public Set<String> getSet() {
    		return set;
    	}
    	public void setSet(Set<String> set) {
    		this.set = set;
    	}
    	public Map<String, Object> getMap() {
    		return map;
    	}
    	public void setMap(Map<String, Object> map) {
    		this.map = map;
    	}
    	public Properties getProperties() {
    		return properties;
    	}
    	public void setProperties(Properties properties) {
    		this.properties = properties;
    	}


                    Message m=ac.getBean("message",Message.class);
    		List<String> list=m.getList();
    		for(String ss:list){
    			System.out.println(ss);
    		}
    		
    		Set set=m.getSet();
    		Iterator<String> it=set.iterator();
    		while(it.hasNext()==true){
                    System.out.println(it.next());
                    System.out.println("11111");
        }


    结果:

    兰州
    北京
    长沙
    张三
    11111
    李四
    11111
    王五
    11111

    四.表达式注入

    		A a=ac.getBean("ddd",A.class);
    		System.out.println(a.getUser());
    		/*
    		 *  注入表达式
    	        Spring引入了一种表达式语言,在语法上与EL的语法很相似,用来读取一个bean中
    	  的属性。
    	  
    	  	xml配置:
    	  	<util:properties id="jdbc1"  location="classpath:db.properties"></util:properties>-->
    		<bean id="ddd" class="com.tanzhou.entity.A">
    		   <property name="user" value="#{jdbc1.user}"></property>
    		</bean>
    		
    		类:
    		public class A implements Serializable {
    		private String user;
    
    		public A() {
    
    		}
    
    		public String getUser() {
    			return user;
    		}
    
    		public void setUser(String user) {
    			this.user = user;
    		}
    		
    		执行:
    		A a=ac.getBean("ddd",A.class);	
    		System.out.println(a.getUser());
    		
    		 * */

    此时读取的是与配置的xml文件同一目录下的db.properties文件,它只有一句话的内容:user=scott

    五.注入null或者空字符串

    注入空值和字符串(了解内容)
         给一个属性注入字符串
        <property  name=“name” value=“”></property>
        注入空值
        <property  name=“name”><null/><property>







  • 相关阅读:
    atitit.nfc 身份证 银行卡 芯片卡 解决方案 attilax总结
    atitit.php 流行框架 前三甲为:Laravel、Phalcon、Symfony2 attilax 总结
    Atitit.执行cmd 命令行 php
    Atitit. 图像处理jpg图片的压缩 清理垃圾图片 java版本
    atitit。企业组织与软件工程的策略 战略 趋势 原则 attilax 大总结
    atitit. 管理哲学 大毁灭 如何防止企业的自我毁灭
    Atitit.java的浏览器插件技术 Applet japplet attilax总结
    Atitit.jquery 版本新特性attilax总结
    Atitit. 软件开发中的管理哲学一个伟大的事业必然是过程导向为主 过程导向 vs 结果导向
    (转)获取手机的IMEI号
  • 原文地址:https://www.cnblogs.com/fonttian/p/7294865.html
Copyright © 2011-2022 走看看