弹簧是一种机械元件,它可以在受到外力作用时产生弹性变形,并在外力消失后恢复原状。它是一种重要的机械元件,广泛应用于机械设备中。
Spring 框架是 Java 平台上的一个开源的轻量级的 Java 开发框架,它主要用于快速开发企业应用程序。Spring 框架提供了一套基于 POJO 的低耦合、面向切面的容器,使得 Java 开发者能够轻松地使用 IOC、AOP 等高级特性。
public class SpringTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld"); helloWorld.sayHello(); } }
Spring 框架提供了大量的优势,如依赖注入、声明式事务、AOP 等特性,使得 Java 开发者能够快速地开发出高质量的企业应用系统。此外,Spring 框架还能够帮助 Java 开发者快速地整合各种不同的 Java 技术,如 Hibernate、Struts 等。
Spring 框架核心是 IOC 和 AOP 两大部分。IOC 是 Inversion of Control 的简写,即“控制反转”。IOC 采用依赖注入方式来实现对象之间的耦合关系,使得对象之间不再相互依赖,耦合度大大降低。AOP 是 Aspect Oriented Programming 的简写,即“面向切面编程”。AOP 采用切面和连接点来将业务逻辑和补充逻辑进行隔离和封装,使得代码易于理解和修改。
Spring 框架拥有众多优势和特性,因此已成为当前最流行的 Java EE 公共平台之一。它不仅能帮助 Java 工作者快速地开发出优雅、易扩展、易测试的企业应用;而且还能帮助工作者整合不同的 Java 技术以实施真正意义上的 SOA 服务化体系。
我们可以将值或值列表填充到Spring xml配置文件中定义的Java bean。
我们可以将值或值列表填充到Spring xml配置文件中定义的Java bean。...
为了展示如何使用xml配置文件来填充集合属性,我们定义了一个具有四个集合属性的Customer对象。
package com.www..cnmon; import java.util.HashSet; import java.util.Set; public class Customer { private Set<Object> sets = new HashSet<Object>(); public Set<Object> getSets() { return sets; } public void setSets(Set<Object> sets) { this.sets = sets; } public String toString() { return sets.toString(); } }
Person Java Bean
package com.www..cnmon; public class Person { private String name; private int age; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", address=" + address + "]"; } }
以下代码显示如何将数据填充到java.util.Set类型化属性。
代码填充三个值。 第一个是硬编码值1.第二个是一个bean参考。 我们必须在某处定义PersonBean,以便在此处使用它。 第三个是bean定义与属性设置。
... <property name="sets"> <set> <value>1</value> <ref bean="PersonBean" /> <bean class="com.www..cnmon.Person"> <property name="name" value="java2sSet" /> <property name="address" value="address" /> <property name="age" value="28" /> </bean> </set> </property> ...
Full Spring的bean配置文件。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="CustomerBean" class="com.www..cnmon.Customer"> <!-- java.util.Set --> <property name="sets"> <set> <value>1</value> <ref bean="PersonBean" /> <bean class="com.www..cnmon.Person"> <property name="name" value="java2sSet" /> <property name="address" value="address" /> <property name="age" value="28" /> </bean> </set> </property> </bean> <bean id="PersonBean" class="com.www..cnmon.Person"> <property name="name" value="java2s1" /> <property name="address" value="address 1" /> <property name="age" value="28" /> </bean> </beans>
下面是加载和运行配置的代码。
package com.www..cnmon; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml"); Customer cust = (Customer)context.getBean("CustomerBean"); System.out.println(cust); } }
输出
Customer [ sets=[ 1, Person [address=address 1, age=28, name=java2s1], Person [address=address, age=28, name=java2sSet]] ]
输出...
以下代码显示了如何使用SetFactoryBean。
这里是Java bean类。
package com.www..cnmon; //from w w w. j av a 2 s. co m import java.util.HashSet; import java.util.Set; public class Customer { private Set<Object> sets = new HashSet<Object>(); public Set<Object> getSets() { return sets; } public void setSets(Set<Object> sets) { this.sets = sets; } public String toString() { return sets.toString(); } }
这里是Spring的bean配置文件。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="CustomerBean" class="com.www..cnmon.Customer"> <property name="sets"> <bean class="org.springframework.beans.factory.config.SetFactoryBean"> <property name="targetSetClass"> <value>java.util.HashSet</value> </property> <property name="sourceSet"> <list> <value>1</value> <value>2</value> <value>3</value> </list> </property> </bean> </property> </bean> </beans>
我们还可以使用util模式和< util:set> 以将数据填充到java.util.Set。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"> <bean id="CustomerBean" class="com.www..cnmon.Customer"> <property name="sets"> <util:set set-class="java.util.HashSet"> <value>1</value> <value>2</value> <value>3</value> </util:set> </property> </bean> </beans>
使用以下代码运行应用程序。
package com.www..cnmon; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml"); Customer cust = (Customer)context.getBean("CustomerBean"); System.out.println(cust); } }
上面的代码生成以下结果。
x:forEach 标签 x:forEach标签用来循环遍历XML文档的节点。 语法格式x:forEach var=string select=string begin=int end=int ste...
fn:trim()函数 fn:trim()函数将字符串两端的空白符移除。语法 fn:trim()函数的语法如下:${fn.trim(string)...
语法和语义这一章节将 Rust 的知识划分成小块,每一块描述一个概念。如果你想从下至上的学习 Rust,按照顺序阅读本教程是个比较...
因为MicrosoftOffice办公软件被广泛应用,以及该软件版本的不断提升,功能不断完善,所以在Office办公软件平台上开发出的VBA应用...
ASP.NET TextBox 控件 Web 服务器控件定义和用法 TextBox 控件用于创建用户可输入文本的文本框。属性 属性 描述 .NET AutoComple...
ASP.NET Button OnClientClick 属性 Button 控件定义和用法 OnClientClick 属性用于设置当 Button 控件被点击时运行一段客户端脚...
ASP.NET Image ImageUrl 属性 Image 控件定义和用法 ImageUrl 属性用于设置或返回要显示的图像的 URL。 语法asp:Image ImageUrl=...
指令ASP.NET 指令是指定可选设置的说明,如注册一个自定义的控制和页面的语言。这些设置介绍了 NET Framework 如何处理单页表单(...
异常处理在 ASP.NET 中异常处理有三个方面:Tracing - 在页面级或者应用程序级追踪程序执行。Error handling - 在页面级或者应用...