Spring actual method example of setting common attribute values
•
Java
This article describes the method of setting common attribute values in spring. Share with you for your reference, as follows:
One configuration
<?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <bean id="exampleBean" class="org.crazyit.app.service.ExampleBean"> <!-- 指定int型的参数值 --> <property name="integerField" value="1"/> <!-- 指定double型的参数值 --> <property name="doubleField" value="2.3"/> </bean> </beans>
Second bean
package org.crazyit.app.service; public class ExampleBean { // 定义一个int型的成员变量 private int integerField; // 定义一个double型的成员变量 private double doubleField; // integerField的setter和getter方法 public void setIntegerField(int integerField) { this.integerField = integerField; } public int getIntegerField() { return this.integerField; } // doubleField的setter和getter方法 public void setDoubleField(double doubleField) { this.doubleField = doubleField; } public double getDoubleField() { return this.doubleField; } }
Three test categories
package lee; import org.springframework.context.*; import org.springframework.context.support.*; import org.crazyit.app.service.*; public class BeanTest { public static void main(String[] args) { //以类加载路径下的bean.xml文件来创建spring容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); ExampleBean b = ctx.getBean("exampleBean",ExampleBean.class); System.out.println(b.getIntegerField()); System.out.println(b.getDoubleField()); } }
IV. test results
Readers interested in more Java related content can view the topics on this site: introduction and advanced tutorial of spring framework, tutorial of Java data structure and algorithm, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills
I hope this article will be helpful to you in Java programming.
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码