spring

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring相关的知识,希望对你有一定的参考价值。

技术分享

尽量使用 scope="singleton" ,不要使用prototype,因为对性能的影响较大

 

给集合类型注入值

Java中主要的集合有:map set list 数组

department类

 1 package com.hsp.collection;
 2 
 3 import java.util.List;
 4 import java.util.Map;
 5 import java.util.Set;
 6 
 7 public class Department {
 8     
 9     private String name;
10     
11     
12     private String [] empName;
13     private List<Employee> empList;
14     private Set<Employee> empsets;
15     private Map<String,Employee> empMaps;
16     
17     public Map<String, Employee> getEmpMaps() {
18         return empMaps;
19     }
20     public void setEmpMaps(Map<String, Employee> empMaps) {
21         this.empMaps = empMaps;
22     }
23     public Set<Employee> getEmpsets() {
24         return empsets;
25     }
26     public void setEmpsets(Set<Employee> empsets) {
27         this.empsets = empsets;
28     }
29     public List<Employee> getEmpList() {
30         return empList;
31     }
32     public void setEmpList(List<Employee> empList) {
33         this.empList = empList;
34     }
35     public String getName() {
36         return name;
37     }
38     public void setName(String name) {
39         this.name = name;
40     }
41     
42     public String[] getEmpName() {
43         return empName;
44     }
45     public void setEmpName(String[] empName) {
46         this.empName = empName;
47     }
48 
49 }

employee类

 1 package com.hsp.collection;
 2 
 3 public class Employee {
 4     
 5     private String name;
 6     private int id;
 7 
 8     public int getId() {
 9         return id;
10     }
11 
12     public void setId(int id) {
13         this.id = id;
14     }
15 
16     public String getName() {
17         return name;
18     }
19 
20     public void setName(String name) {
21         this.name = name;
22     }
23 
24 }

测试类

 1 package com.hsp.collection;
 2 
 3 import java.util.Iterator;
 4 import java.util.Map;
 5 import java.util.Map.Entry;
 6 
 7 import javax.swing.text.html.parser.Entity;
 8 
 9 import org.springframework.context.ApplicationContext;
10 import org.springframework.context.support.ClassPathXmlApplicationContext;
11 
12 public class App1 {
13 
14     /**
15      * @param args
16      */
17     public static void main(String[] args) {
18         // TODO Auto-generated method stub
19         ApplicationContext ac = new ClassPathXmlApplicationContext("com/hsp/collection/beans.xml");
20         Department department = (Department) ac.getBean("department");
21         System.out.println(department.getName());
22         for (String emName:department.getEmpName()){
23             System.out.println(emName);
24         }
25         
26         System.out.println("********通过list集合取出数据*********");
27         for(Employee e: department.getEmpList()){
28             System.out.println("name"+e.getName()+" "+e.getId());
29         }
30         
31         System.out.println("********通过set集合取出数据*********");
32         for(Employee e: department.getEmpsets()){
33             System.out.println("name"+e.getName()+" "+e.getId());
34         }
35         
36         
37         System.out.println("********通过map集合取出数据 迭代器方法*********");
38         Map<String, Employee> empmaps=department.getEmpMaps();
39         Iterator it = empmaps.keySet().iterator();
40         while (it.hasNext()) {
41             String key = (String) it.next();
42             Employee emp = empmaps.get(key);
43             System.out.println("key="+key+" "+emp.getName());
44             
45         }
46         
47         System.out.println("********通过map集合取出数据 简洁方法*********");
48         for(Entry <String,Employee> entry1:department.getEmpMaps().entrySet()){
49             System.out.println(entry1.getKey()+" "+entry1.getValue().getName());
50         }
51     }
52 
53 }

beans.xml文件

 1 <bean id="department" class="com.hsp.collection.Department">
 2 
 3 
 4 <property name="name" value="医务室" />
 5 
 6 <!-- 对数组注入值 -->
 7 <property name="empName">
 8     <list>
 9        <value>张三</value>
10        <value>李四</value>
11        <value>王五</value>
12     </list>
13 </property>
14 
15 <!-- 给list注入值 -->
16 <property name="empList">
17      <list>
18          <ref bean="emp1"/>
19          <ref bean="emp2"/>
20      </list>
21 </property>
22 
23 <!-- 给set注入值 -->
24 <property name="empsets">
25      <set>
26          <ref bean="emp1"/>
27          <ref bean="emp2"/>     
28      </set>
29 </property>
30 
31 <!-- 给map注入值 -->
32 <property name="empMaps">
33      <map>
34          <entry key="1" value-ref="emp1" />
35          <entry key="2" value-ref="emp2" />    
36      </map>
37 </property>
38 </bean>
39 
40 
41 <bean id="emp1" class="com.hsp.collection.Employee">
42 <property name="name" value="扬州"></property>
43 <property name="id" value="1"></property>
44 </bean>
45 <bean id="emp2" class="com.hsp.collection.Employee">
46 <property name="name" value="武汉"></property>
47 <property name="id" value="2"></property>
48 </bean>

内部bean

<bean id="foo" class="…Foo">

        <property name="属性">

        <!--第一方法引用-->

        <ref  bean ="bean 对象名"/>

        <!--内部bean-->

        <bean>

                <property></property>

        </bean>

        </property>

</bean>

 

4、继承配置

student类

 1 public class Student {
 2     
 3     protected String name;
 4     protected int age;
 5     
 6     
 7     public String getName() {
 8         return name;
 9     }
10     public void setName(String name) {
11         this.name = name;
12     }
13     public int getAge() {
14         return age;
15     }
16     public void setAge(int age) {
17         this.age = age;
18     }
19     
20 }

Gradate类

 1 public class Gradate extends Student {
 2     
 3     private String degree;
 4 
 5     public String getDegree() {
 6         return degree;
 7     }
 8 
 9     public void setDegree(String degree) {
10         this.degree = degree;
11     }
12 
13 }

测试类

 1 public class App1 {
 2     
 3     public static void main(String[] args){
 4         ApplicationContext ac = new ClassPathXmlApplicationContext("com/hsp/inherit/beans.xml");
 5         
 6         Gradate gradate = (Gradate) ac.getBean("gradate");
 7         System.out.println(gradate.getName()+" "+gradate.getAge()+" "+gradate.getDegree());
 8     }
 9 
10 }

beans.xml文件

<!-- 配置学生对象 -->
<bean id="student" class="com.hsp.inherit.Student">
    <property name="name" value="李世民"/>
    <property name="age" value="21"/>
</bean>

<!-- 配置Gradate -->
<bean id="gradate" parent="student" class="com.hsp.inherit.Gradate">
    <!-- 如果自身配置属性那么,age,则会替换父对象的数据 -->
    <property name="name" value="李渊"/>
    <property name="age" value="63"/>
    <property name="degree" value="小学毕业"/>
</bean>

 

以上是关于spring的主要内容,如果未能解决你的问题,请参考以下文章

Spring boot:thymeleaf 没有正确渲染片段

What's the difference between @Component, @Repository & @Service annotations in Spring?(代码片段

spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段

Spring Rest 文档。片段生成时 UTF-8 中间字节无效 [重复]

解决spring-boot启动中碰到的问题:Cannot determine embedded database driver class for database type NONE(转)(代码片段

一张图帮你记忆,Spring Boot 应用在启动阶段执行代码的几种方式