编程札记

S

Spring EL hello world实例

Spring EL与OGNL和JSF EL相似,计算评估或在bean创建时执行。此外,所有的Spring表达式都可以通过XML或注解。 在本教程中,我们将学习如何使用Spring表达式语言(SpEL),注入字符串,整数,Bean到属性,无论是在XML和注释。 1. Spring Beans 两个简单Bean,后来利用 SpEL 注入值到属性,在 XML 和 注释。 package com.yiibai.core; public class Customer { private Item item; private String itemName; } package com.yiibai.core; public class Item { private String name; private int qty; } 3. Spring EL以XML形式 使用 SpEL关闭的#{ SpEL expression }括号,请参阅XML 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-3.0.xsd"> <bean id="itemBean" class="com.yiibai.core.Item"> <property name="name" value="itemA" /> <property name="qty" value="10" /> </bean> <bean id="customerBean" class="com.yiibai.core.Customer"> <property name="item" value="#{itemBean}" /> <property name="itemName" value="#{itemBean.name}" /> </bean> </beans> #{itemBean}

阅读全文 ↗
S

Spring @PostConstruct和@PreDestroy实例

在Spring中,既可以实现 InitializingBean和DisposableBean接口或在bean配置文件中指定 init-method 和 destroy-method 在初始化和销毁回调函数。在这篇文章中,我们将介绍如何使用 @PostConstruct 和 @PreDestroy 注解来做同样的事情。 注:@PostConstruct和@PreDestroy 标注不属于 Spring,它是在J2EE库- common-annotations.jar。 @PostConstruct 和 @PreDestroy 一个 CustomerService Bean使用 @PostConstruct 和 @PreDestroy 注释 package com.yiibai.customer.services; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; public class CustomerService { String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } @PostConstruct public void initIt() throws Exception { System.out.println("Init method after properties are set : " + message); } @PreDestroy public void cleanUp() throws Exception { System.out.println("Spring Container is destroy! Customer clean up"); } } 默认情况下,Spring不会意识到@PostConstruct和@PreDestroy注解。要启用它,要么注册“CommonAnnotationBeanPostProcessor”,要么在bean配置文件的<context:annotation-config />‘ 指定, 1. Commo

阅读全文 ↗
S

Spring Bean init-method 和 destroy-method实例

在Spring中,可以使用 init-method 和 destroy-method 在bean 配置文件属性用于在bean初始化和销毁某些动作时。这是用来替代 InitializingBean和DisposableBean接口。 示例 这里有一个例子向您展示如何使用 init-method 和 destroy-method。 package com.yiibai.customer.services; public class CustomerService { String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public void initIt() throws Exception { System.out.println("Init method after properties are set : " + message); } public void cleanUp() throws Exception { System.out.println("Spring Container is destroy! Customer clean up"); } }

阅读全文 ↗
S

Spring Bean InitializingBean和DisposableBean实例

在Spring中,InitializingBean和DisposableBean是两个标记接口,为Spring执行时bean的初始化和销毁某些行为时的有用方法。 对于Bean实现 InitializingBean,它将运行 afterPropertiesSet()在所有的 bean 属性被设置之后。 对于 Bean 实现了DisposableBean,它将运行 destroy()在 Spring 容器释放该 bean 之后。 示例 下面是一个例子,向您展示如何使用 InitializingBean 和 DisposableBean。一个 CustomerService bean来实现 InitializingBean和DisposableBean 接口,并有一个消息(message)属性。 package com.yiibai.customer.services; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; public class CustomerService implements InitializingBean, DisposableBean { String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public void afterPropertiesSet() throws Exception { System.out.println("Init method after properties are set : " + message); } public void destroy() throws Exception { System.out.println("Spring Container is destroy! Customer clean up"); } } 执行它 package com.yiibai.common; import org.springf

阅读全文 ↗
S

Spring自定义@Required-style注解

@Required注解是用来确保特定属性已设置。如果您迁移现有项目到Spring框架或有自己的@Required-style注解不管是什么原因,Spring允许您定义自定义@Required-style注解,相当于@Required注解。 在这个例子中,您将创建一个名为 @Mandatory 定制 @Required-style 注解,相当于@Required注解。 1.创建@Mandatory接口 package com.yiibai.common; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Mandatory { } 2.应用它到属性 package com.yiibai.common; public class Customer { private Person person; private int type; private String action; @Mandatory public void setPerson(Person person) { this.person = person; } //getter and setter methods } 3.注册它 包函新@Mandatory注释到“RequiredAnnotationBeanPostProcessor' 类。 <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/sp

阅读全文 ↗
S

Spring使用@Required注解依赖检查

Spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置。在大多数情况下,你只需要确保特定属性已经设置但不是所有属性.. 对于这种情况,你需要 @Required 注解,请参见下面的例子: @Required示例 Customer对象,适用@Required在 setPerson()方法,以确保 person 属性已设置。 package com.yiibai.common; import org.springframework.beans.factory.annotation.Required; public class Customer { private Person person; private int type; private String action; public Person getPerson() { return person; } @Required public void setPerson(Person person) { this.person = person; } } 简单地套用@Required注解不会强制执行该属性的检查,还需要注册一个RequiredAnnotationBeanPostProcessor以了解在bean配置文件@Required注解。 RequiredAnnotationBeanPostProcessor可以用两种方式来启用。 1. 包函 <context:annotation-config /> 添加 Spring 上下文和 <context:annotation-config />在bean配置文件。 <beans ... xmlns:context="http://www.springframework.org/schema/context" ... http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> ... <context:annotation-config /> ... </beans> 完整的实例, <beans xmlns="http://www.springframewor

阅读全文 ↗
S

Spring依赖检查

在Spring中,可以使用依赖检查功能,以确保所要求的属性可设置或者注入。 依赖检查模式 4个依赖检查支持的模式: none – 没有依赖检查,这是默认的模式。 simple – 如果基本类型(int, long,double…)和集合类型(map, list..)的任何属性都没有设置,UnsatisfiedDependencyException将被抛出。 objects – 如果对象类型的任何属性都没有设置,UnsatisfiedDependencyException将被抛出。 all – 如果任何类型的任何属性都没有被设置,UnsatisfiedDependencyException将被抛出。 注:默认模式是 none 示例 Customer和Person对象的示例。 package com.yiibai.common; public class Customer { private Person person; private int type; private String action; //getter and setter methods } package com.yiibai.common; public class Person { private String name; private String address; private int age; //getter and setter methods } 1. none 依赖检查 Spring bean配置文件使用 “none” 依赖检查模式。 <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.yiibai.common.Customer" > <property name="action

阅读全文 ↗
S

Spring bean配置继承

在 Spring,继承是用为支持bean设置一个 bean 来分享共同的值,属性或配置。 一个子 bean 或继承的bean可以继承其父 bean 的配置,属性和一些属性。另外,子 Bean 允许覆盖继承的值。 请参见下面的完整的例子来告诉你如何配置 bean 继承在 Spring 中工作。 package com.yiibai.common; public class Customer { private int type; private String action; private String Country; //... } 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="BaseCustomerMalaysia" class="com.yiibai.common.Customer"> <property name="country" value="Malaysia" /> </bean> <bean id="CustomerBean" parent="BaseCustomerMalaysia"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> </beans> 以上就是“BaseCustomerMalaysia” Bean中含有的 country 属性的值,而“CustomerBean” Bean 继承其父('BaseCustomerMalaysia')这个值。 执行它 package com.yiibai.common; import org.springframework.context.ApplicationContext; import org.springfra

阅读全文 ↗
S

Spring PropertyPlaceholderConfigurer实例

很多时候,大多数Spring开发人员只是把整个部署的详细信息(数据库的详细信息,日志文件的路径)写在XML 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="customerDAO" class="com.yiibai.customer.dao.impl.JdbcCustomerDAO"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="customerSimpleDAO" class="com.yiibai.customer.dao.impl.SimpleJdbcCustomerDAO"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/yiibaijava" /> <property name="username" value="root" /> <property name="password" value="password" /> </bean> </beans> 但是,在企业环境中,部署的细节通常只可以由系统管理员或数据库管理员来'触碰',他们可能会拒绝直接访问你的bean的配置文件,它们会要求部署配置一个单独的文件,

阅读全文 ↗
S

Spring注入日期到bean属性-CustomDateEditor

这一个Spring例子向您展示如何为bean属性注入一个“日期”。 package com.yiibai.common; import java.util.Date; public class Customer { Date date; public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } @Override public String toString() { return "Customer [date=" + date + "]"; } } 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="customer" class="com.yiibai.common.Customer"> <property name="date" value="2015-12-31" /> </bean> </beans> 执行-运行程序输出 package com.yiibai.common; 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)

阅读全文 ↗
S

Spring MapFactoryBean例子

MapFactoryBean类为开发者提供了一种在Spring的bean配置文件中创建一个具体的Map集合类(HashMap和TreeMap)。 这里有一个MapFactoryBean。例如,在运行时它将实例化一个HashMap,并注入到一个bean属性。 package com.yiibai.common; import java.util.Map; public class Customer { private Map maps; //... } 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.yiibai.common.Customer"> <property name="maps"> <bean class="org.springframework.beans.factory.config.MapFactoryBean"> <property name="targetMapClass"> <value>java.util.HashMap</value> </property> <property name="sourceMap"> <map> <entry key="Key1" value="one" /> <entry key="Key2" value="two" /> <entry key="Key3" value="three" /> </map> </property> </bean> </property> </bean> </beans> 另外,还可以使用 util 的模式和<util:map>来做到同样的事情。 <beans xmlns="http://www.springframework.org/sc

阅读全文 ↗