您现在的位置是:主页 > news > 怎么分析网站设计/培训心得体会范文大全1000
怎么分析网站设计/培训心得体会范文大全1000
admin2025/5/9 16:27:55【news】
简介怎么分析网站设计,培训心得体会范文大全1000,北京网站推广怎么做,北京房山住房和城乡建设委员会网站目录直通车 1、 属性注入 2、 构造方法注入 1、 属性注入 是最常用的注入方式. 即通过 setter 方法注入Bean 的属性值或依赖的对象, 属性注入使用 <property> 元素, 使用 name 属性指定 Bean 的属性名称,value 属性或 <value> 子节点指定属性值 &l…
怎么分析网站设计,培训心得体会范文大全1000,北京网站推广怎么做,北京房山住房和城乡建设委员会网站目录直通车
1、 属性注入
2、 构造方法注入 1、 属性注入
是最常用的注入方式.
即通过 setter 方法注入Bean 的属性值或依赖的对象, 属性注入使用 <property> 元素, 使用 name 属性指定 Bean 的属性名称,value 属性或 <value> 子节点指定属性值
&l…
目录直通车
1、 属性注入
2、 构造方法注入
1、 属性注入
是最常用的注入方式.
即通过 setter 方法注入Bean 的属性值或依赖的对象, 属性注入使用 <property> 元素, 使用 name 属性指定 Bean 的属性名称,value 属性或 <value> 子节点指定属性值
<bean id="helloWorld" class="com.spring.beans.HelloWorld"><property name="name" value="HelloSpring"> </property></bean>
2、 构造方法注入
通过构造方法注入Bean 的属性值或依赖的对象,它保证了 Bean 实例在实例化后就可以使用。构造器注入在 <constructor-arg> 元素里声明属性, <constructor-arg> 中没有 name 属性。
下面通过一个实例学习构造器方法注入:
新建一个Java Class名为TestConstructor:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestConstructor {/*** 一个汽车的例子*/private String brand;private String provence;private double price;private int maxSpeed;public TestConstructor(String brand, String provence, double price) {this.brand = brand;this.provence = provence;this.price = price;}public TestConstructor(String brand, double price, int maxSpeed) {this.brand = brand;this.price = price;this.maxSpeed = maxSpeed;}@Overridepublic String toString() {return "TestConstructor{" +"brand='" + brand + '\'' +", provence='" + provence + '\'' +", price=" + price +", maxSpeed=" + maxSpeed +'}';}public static void main(String[] args) {// 创建IOC容器ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/com/spring/beans/hello.xml");// 从IOC容器中获取Bean的实例TestConstructor tc1 = (TestConstructor)applicationContext.getBean("testConstructor1");System.out.println("---- tc1 -----\n" + tc1);TestConstructor tc2 = (TestConstructor)applicationContext.getBean("testConstructor2");System.out.println("---- tc2 ----\n" + tc2);}
}
在hello.xml 写一下构造器的配置:
index是按照构造器中的参数顺序来赋值的,下面运行之后观察运行结果。
<bean id="testConstructor1" class="com.spring.beans.TestConstructor"><constructor-arg value="Audi" index="0"> </constructor-arg><constructor-arg value="SiChuan" index="1"> </constructor-arg><constructor-arg value="300000" index="2"> </constructor-arg></bean><bean id="testConstructor2" class="com.spring.beans.TestConstructor"><constructor-arg value="BaoMa" index="0"> </constructor-arg><constructor-arg value="Shanghai" index="1"> </constructor-arg><constructor-arg value="200" index="2"> </constructor-arg></bean>
运行结果:
发现赋值的顺序虽然标出来了,但是两个构造器,怎么两个都使用了第一个构造器?
当前使用index的方法来传递参数已经不能满足我此时的需求了,那么怎样修改才能区分调用构造器的方法呢?
将hello.xml的构造器注入修改为以下内容:
<bean id="testConstructor1" class="com.spring.beans.TestConstructor"><constructor-arg name="brand" value="Audi"> </constructor-arg><constructor-arg name="province" value="SiChuan"> </constructor-arg><constructor-arg name="price" value="300000" type="double"> </constructor-arg></bean><bean id="testConstructor2" class="com.spring.beans.TestConstructor"><constructor-arg name="brand" value="BaoMa" type="java.lang.String"> </constructor-arg><constructor-arg name="price" value="300000" type="double"> </constructor-arg><constructor-arg name="maxSpeed" value="200" type="int"> </constructor-arg></bean>
这里可以指定name,也可以指定type来区分构造器。
观察运行结果: