• Spring注入方式(2)


    3、引用其他bean

      Bean经常需要相互协作完成应用程序的功能,bean之间必须能够互相访问,就必须在bean配置之间指定对bean的引用,可以通过节点<ref>或者ref来为bean属性指定对bean的引用,也可以在属性或者构造器里包含bean的声明,这样bean称为内部bean。

    bean中引用其他bean,其中Car为对象。

     1 <!-- 通过构造方法配置bean属性 -->
     2     <bean id="car" class="hello.Car">
     3     <constructor-arg value="Audi" index="0"></constructor-arg>
     4     <constructor-arg value="ShangHai" index="1"></constructor-arg>
     5     <constructor-arg value="300000" type="double" ></constructor-arg>
     6         
     7 </bean>
     8 
     9 <bean id="person" class="hello.Person">
    10     <property name="name" value = "Tom"></property>
    11     <property name="age" value = "24"></property>
    12     <property name="car" ref="car"></property>
    13 </bean>
    View Code

    内部bean

     1 <bean id="person" class="hello.Person">
     2         <property name="name" value = "Tom"></property>
     3         <property name="age" value = "24"></property>
     4         <!--  
     5         <property name="car" ref="car"></property>-->
     6         
     7         <!-- 内部bean -->
     8         <property name="car">
     9             <!-- 内部bean不能被外部bean使用 -->
    10             <bean id="car3" class="hello.Car">
    11                 <constructor-arg value="Ford"></constructor-arg>
    12                 <constructor-arg value="Changan"></constructor-arg>
    13                 <constructor-arg value="200000" type="double"></constructor-arg>
    14                 
    15             </bean>
    16         </property>
    17 </bean>
    View Code

    4、集合属性

    在Spring中可以通过<list>、<set>或者<map>来配置集合属性。

    通过list配置集合属性

    Person.java

     1 package com.spring;
     2 
     3 import java.util.List;
     4 
     5 public class Person {
     6     private String name;
     7     private int age;
     8     private List<Car> cars;
     9     public String getName() {
    10         return name;
    11     }
    12     public void setName(String name) {
    13         this.name = name;
    14     }
    15     public int getAge() {
    16         return age;
    17     }
    18     public void setAge(int age) {
    19         this.age = age;
    20     }
    21     public List<Car> getCars() {
    22         return cars;
    23     }
    24     public void setCars(List<Car> cars) {
    25         this.cars = cars;
    26     }
    27     @Override
    28     public String toString() {
    29         return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]";
    30     }
    31     
    32     
    33     
    34 }
    View Code

    Car.java

     1 package com.spring;
     2 
     3 public class Car {
     4     private String brand;
     5     private double price;
     6     private int maxSpeed;
     7     public String getBrand() {
     8         return brand;
     9     }
    10     public void setBrand(String brand) {
    11         this.brand = brand;
    12     }
    13     public double getPrice() {
    14         return price;
    15     }
    16     public void setPrice(double price) {
    17         this.price = price;
    18     }
    19     public int getMaxSpeed() {
    20         return maxSpeed;
    21     }
    22     public void setMaxSpeed(int maxSpeed) {
    23         this.maxSpeed = maxSpeed;
    24     }
    25     @Override
    26     public String toString() {
    27         return "Car [brand=" + brand + ", price=" + price + ", maxSpeed=" + maxSpeed + "]";
    28     }
    29     
    30 }
    View Code

    beans-collection.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:util="http://www.springframework.org/schema/util"
     5     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     6         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
     7     
     8     <bean id="car1" class="com.spring.Car">
     9         <property name="brand" value="audi"></property>
    10         <property name="price" value="400000"></property>
    11         <property name="maxSpeed" value="240"></property>
    12     </bean>
    13     
    14     <bean id="car2" class="com.spring.Car">
    15         <property name="brand" value="baoma"></property>
    16         <property name="price" value="700000"></property>
    17         <property name="maxSpeed" value="270"></property>
    18     </bean>
    19     
    20     <bean id="person" class="com.spring.Person">
    21         <property name="name" value="Jerry"></property>
    22         <property name="age" value="41"></property>
    23         <property name="cars">
    24             <list>
    25                 <ref bean="car1" />
    26                 <ref bean="car2" />
    27             </list>
    28         </property>
    29     </bean>
    30 </beans>
    View Code

    结果:

    通过map配置属性

    Person.java

     1 package com.spring;
     2 
     3 import java.util.Map;
     4 
     5 public class Person {
     6     private String name;
     7     private int age;
     8     private Map<String, Car> carMap;
     9     public String getName() {
    10         return name;
    11     }
    12     public void setName(String name) {
    13         this.name = name;
    14     }
    15     public int getAge() {
    16         return age;
    17     }
    18     public void setAge(int age) {
    19         this.age = age;
    20     }
    21     public Map<String, Car> getCarMap() {
    22         return carMap;
    23     }
    24     public void setCarMap(Map<String, Car> carMap) {
    25         this.carMap = carMap;
    26     }
    27     @Override
    28     public String toString() {
    29         return "Person [name=" + name + ", age=" + age + ", carMap=" + carMap + "]";
    30     }
    31 }
    View Code

    beans-collection

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:util="http://www.springframework.org/schema/util"
     5     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     6         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
     7     
     8     <bean id="car1" class="com.spring.Car">
     9         <property name="brand" value="audi"></property>
    10         <property name="price" value="400000"></property>
    11         <property name="maxSpeed" value="240"></property>
    12     </bean>
    13     
    14     <bean id="car2" class="com.spring.Car">
    15         <property name="brand" value="baoma"></property>
    16         <property name="price" value="700000"></property>
    17         <property name="maxSpeed" value="270"></property>
    18     </bean>
    19     
    20     <bean id="person" class="com.spring.Person">
    21         <property name="name" value="Jerry"></property>
    22         <property name="age" value="41"></property>
    23         <property name="carMap">
    24             <map>
    25                 <entry key="1" value-ref="car1"></entry>
    26                 <entry key="2" value-ref="car2"></entry>
    27             </map>
    28         </property>
    29     </bean>
    30 </beans>
    View Code

    结果:

    配置Set属性,和配置list一样。

    5、p名字空间注入,也需要setter方法。

    Student.java

     1 package com.spring;
     2 
     3 public class Student {
     4     private String name;
     5     private String number;
     6     
     7     public String getName() {
     8         return name;
     9     }
    10     public void setName(String name) {
    11         this.name = name;
    12     }
    13     public String getNumber() {
    14         return number;
    15     }
    16     public void setNumber(String number) {
    17         this.number = number;
    18     }
    19     
    20     @Override
    21     public String toString() {
    22         return "Student [name=" + name + ", number=" + number + "]";
    23     }
    24     
    25     
    26 }
    View Code

    Main.java

     1 package com.spring;
     2 
     3 import org.springframework.context.ApplicationContext;
     4 import org.springframework.context.support.ClassPathXmlApplicationContext;
     5 
     6 public class Main {
     7     public static void main(String[] args) {
     8         ApplicationContext ac = new ClassPathXmlApplicationContext("beans-p.xml");
     9         Student student = (Student) ac.getBean("student");
    10         System.out.println(student);
    11     }
    12 }
    View Code

    beans-p.xml

    1 <?xml version="1.0" encoding="UTF-8"?>
    2 <beans xmlns="http://www.springframework.org/schema/beans"
    3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4     xmlns:p="http://www.springframework.org/schema/p"
    5     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    6     
    7     <bean id="student" class="com.spring.Student" p:name="Merry" p:number="1120141314"></bean>
    8 
    9 </beans>
    View Code

     执行结果

    6、property注入

    Student.java

     1 package com.spring;
     2 
     3 import java.util.Properties;
     4 
     5 public class Student {
     6     private String name;
     7     private String number;
     8     private Properties properties;
     9     
    10     public String getName() {
    11         return name;
    12     }
    13     public void setName(String name) {
    14         this.name = name;
    15     }
    16     public String getNumber() {
    17         return number;
    18     }
    19     public void setNumber(String number) {
    20         this.number = number;
    21     }
    22     public Properties getProperties() {
    23         return properties;
    24     }
    25     public void setProperties(Properties properties) {
    26         this.properties = properties;
    27     }
    28     @Override
    29     public String toString() {
    30         return "Student [name=" + name + ", number=" + number + ", properties=" + properties + "]";
    31     }
    32     
    33     
    34     
    35     
    36     
    37     
    38 }
    View Code

    beans-p.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:p="http://www.springframework.org/schema/p"
     5     xmlns:util="http://www.springframework.org/schema/util"
     6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     7         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
     8     <bean id="student" class="com.spring.Student">
     9         <property name="name" value="Merry"></property>
    10         <property name="number" value="1120141213"></property>
    11         <property name="properties" >
    12             <props>
    13                 <prop key="user">root</prop>
    14                 <prop key="psw">1234</prop>
    15                 <prop key="jdbcUrl">jdbc:mysql:///test</prop>
    16                 <prop key="driverClass">com.mysql.jdbc.Driver</prop>
    17             </props>
    18         </property>
    19         
    20     </bean>
    21 </beans>
    View Code

    执行结果

    7、c命名空间注入

  • 相关阅读:
    推荐个我在用的免费翻译软件,支持多家翻译API整合
    IDEA小技巧:Markdown里的命令行可以直接运行了
    Java 18 新特性:简单Web服务器 jwebserver
    Spring Authorization Server授权服务器入门
    Spring Security实现基于RBAC的权限表达式动态访问控制
    Intellij IDEA 2022 正式发布,这些功能真不错
    【大话云原生】负载均衡篇小饭馆客流量变大了
    【大话云原生】微服务篇五星级酒店的服务方式
    数据建模软件Chiner,颜值与实用性并存
    【大话云原生】煮饺子与docker、kubernetes之间的关系
  • 原文地址:https://www.cnblogs.com/Hxinguan/p/5936039.html
Copyright © 2020-2023  润新知