Spring知识集锦
配置
Spring各种注入方式的区别
注入方式 | 说明 |
@Resource | 来源:JSR250 (Common Annotations for Java) 注入方式:
|
13 years ago
0
Dom4j知识集锦
如何禁止加载XSD和DTD
1 2 |
SAXReader reader = new SAXReader( true ); reader.setFeature( Constants.XERCES_FEATURE_PREFIX + Constants.LOAD_EXTERNAL_DTD_FEATURE, false ); |
DTD验证,无法联网时报错
通过指定本地的DTD文档来解决:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
SAXReader reader = new SAXReader( true ); reader.setEntityResolver( new EntityResolver() { public InputSource resolveEntity( String publicId, String systemId ) { if ( publicId.equals( "-//Hibernate/Hibernate Mapping DTD 3.0//EN" ) ) { InputStream in = getClass().getResourceAsStream( "/hibernate-mapping-3.0.dtd" ); return new InputSource( in ); } return null; } } ); |
使用JAXP来进行XSD验证
Dom4j如何格式化XML输出
如何使用XPath解析带名字空间的XML
Dom4j访问者模式中,删除节点导致NullPointerException
报错:java.lang.NullPointerException at org.dom4j.tree.AbstractElement.accept(AbstractElement.java:208)
解决:在访问者中记录需要删除的节点,访问结束后一… 阅读全文
13 years ago
0
Hibernate基于注解的配置样例
实体类上的常用注解
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@Entity ( name = "ChristmasTree" ) @Table ( name = "T_CRISMS_TREE" ) //给自动生成的表添加注释 @org.hibernate.annotations.Table ( appliesTo = "T_CRISMS_TREE", comment = "圣诞树" ) @Cacheable //JPA注解,表示该类型的实体支持缓存 //启用二级缓存时有意义,指定Hibernate缓存策略、存放区域 @Cache ( usage = CacheConcurrencyStrategy.READ_WRITE, region = "entityCache" ) public class ChristmasTree { //集合属性的缓存策略必须单独配置 @Cache ( usage = CacheConcurrencyStrategy.READ_WRITE, region = "entityCache" ) private List branches; } |
使用注解方式进行对象-数据库映射时,应当以JPA注解为主,仅Hibernate特有的功能则使用其私有的注解类。
简单主键和简单属性映射
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
//简单的主键映射 @Id //该注解指明此实体的标识符属性,对应了关系型数据库的主键 @Column ( name = "ID", columnDefinition = "int(11) comment '主键'" ) private Integer id; //UUID方式的主键生成器配置,其它任何自定义的主键生成器可以依样扩展 @Id @Column ( name = "ID", columnDefinition = "varchar(36) comment '主键,36位UUID'" ) @GeneratedValue ( generator = "uuid" ) @GenericGenerator ( name = "uuid", strategy = "org.hibernate.id.UUIDGenerator" ) private String id; //可以指定普通属性的延迟加载 @Basic ( fetch = FetchType.EAGER, optional = true ) @Column ( name = "BUILD_COST", columnDefinition = "decimal(10,2) comment '建造费用'" ) private BigDecimal buildCost; //时间映射 //只能用于java.util.Date或者java.util.Calendar,用于表示日期、时间或者时间戳 @Temporal ( TemporalType.TIMESTAMP ) @Column ( name = "BUILD_TIME", columnDefinition = "datetime comment '建造时间'" ) private Date buildTime; //大字段映射 @Lob @Column ( name = "REMARK", columnDefinition = "longtext comment '圣诞树备注信息'" ) private String remark; //枚举类型映射 @Enumerated ( EnumType.ORDINAL ) //根据枚举元素的声明顺序,从0开始 @Column ( name = "STATUS", columnDefinition = "tinyint comment '状态'" ) private Status status; //乐观并发控制用列 @Version @Column ( name = "VERSION" ) public Integer version; |
复合主键映射
嵌入式(embeddable)对象映射
关联映射
继承映射
其它注解
阅读全文
14 years ago
0
使用Eclipse进行远程调试
一些基础知识
JPDA(Java Platform Debugger Architecture)是实现Java远程调试的基础,它是一个CS架构,包含三个组件:
- JVMTI(JVM Tool Interface ):服务端,即debuggee端。该接口代替JDK…
14 years ago
0
JProfiler学习笔记
剖析视图
内存视图(Memory Views)
该视图专注于对象的内存分配情况,包含以下选项卡:
All Objects | 显示堆中实时的对象情况:显示某种类型(聚合级别可以是类、包等)对象的数量(Instance Count)、浅尺寸(Shallow size)… |
14 years ago
0
Maven POM文件配置示例
排除传递性依赖
1 2 3 4 5 6 7 8 9 10 11 12 |
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>2.5.0</version> <exclusions> <exclusion> <!-- CXF对Spring-web的依赖被排除 --> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </exclusion> </exclusions> </dependency> |
指定资源文件目录并过滤
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<build> <directory>target</directory> <outputDirectory>target/classes</outputDirectory> <testOutputDirectory>target/test-classes</testOutputDirectory> <sourceDirectory>src/main/java</sourceDirectory> <testSourceDirectory>src/test/java</testSourceDirectory> <resources> <resource> <!-- 在这里可以指定多个资源文件目录 --> <directory>src/main/java</directory> <excludes> <!--注意该配置从本应当是源代码的目录中过滤掉Java文件,其他文件作为资源文件 --> <exclude>**/*.java</exclude> </excludes> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> <testResources> <testResource> <directory>src/test/resources</directory> </testResource> </testResources> </build> |
指定额外的源代码目录
过滤资源文件
配置目录链接(Linked Folder)
打包时生成*–sources.jar
打包时生成*–javadoc.jar
指定编译级别
启用对AspectJ的支持
指定源代码、资源文件的编码方式
打包时拷贝所有依赖包
打包生成可执行的jar文件
打uber-jar另一种方法
maven-shade-plugin也支持将依赖打包进JAR,这类似于maven-assembly-plugin。但是后者存在BUG,会导致Unable to locate Spring NamespaceHandler for XML schema na…
阅读全文
15 years ago
0
Hibernate基于XML的配置文件样例
Hibernate 3.0配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory name="cpdb"> <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="hibernate.connection.password">CASTOR</property> <property name="hibernate.connection.url">jdbc:oracle:thin:@RD-DB-5.sparknet.com.cn:1522:CPDB</property> <property name="hibernate.connection.username">CASTOR</property> <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property> <property name="hibernate.jdbc.batch_size">20</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.generate_statistics">true</property> <property name="hibernate.current_session_context_class">thread</property> </session-factory> </hibernate-configuration> |
XRM配置文件样例
XRM是Hibernate3里面的一个试验性功能,用于在数据库和XML文件之间进行映射
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping package="sparknet.traing.hxm.model"> <class entity-name="Corp" node="corp" table="T_CORP"> <comment> <![CDATA[ Corporation basic information ]]> </comment> <!-- 复合主键的映射 --> <composite-id name="id" node="-"> <key-property name="org" node="org" type="long"> <column name="ORG" sql-type="NUMBER(8)"> <comment>database Code</comment> </column> </key-property> <key-property name="id" node="id" type="long"> <column name="ID" sql-type="NUMBER(8)"> <comment>corporation identifier</comment> </column> </key-property> <key-property name="seqId" node="seq-id" type="long"> <column name="SEQ_ID" sql-type="NUMBER(8)"> <comment>changing sequence</comment> </column> </key-property> <generator class="foreign"> <param name="property">corpOther</param> </generator> </composite-id> <!-- 普通属性的映射 --> <property name="corpName" node="corp-name" type="string" unique="true" unique-key="UK_CORP_NAME"> <column name="CORP_NAME" sql-type="VARCHAR(64)"> <comment>corporation name</comment> </column> </property> <property name="regCapi" node="reg-capi" type="currency"> <column name="REG_CAPI" sql-type="NUMBER(14,2)"> <comment>registered capital</comment> </column> </property> <property name="startDate" node="start-date" type="fmt_date"> <column name="START_DATE" sql-type="DATE"> <comment> corporation start date</comment> </column> </property> <property name="startTime" node="start-time" type="fmt_time"> <column name="START_TIME" sql-type="DATE"> <comment> corporation start time</comment> </column> </property> <!-- 组件映射 --> <component name="address" node="addr" lazy="true"> <property name="street" node="street" type="string"> <column name="STREET" sql-type="VARCHAR2(128)"> <comment>address of street</comment> </column> </property> <property name="building" node="building" type="string"> <column name="BUILDING" sql-type="VARCHAR2(128)"> <comment>building</comment> </column> </property> <property name="zipCode" node="zip-code" type="int"> <column name="ZIP" sql-type="NUMBER(6)"> <comment>zip code</comment> </column> </property> </component> <!-- 共享主键的一对一关联,目前这个版本,会导致当前Class的主键不能映射到XML中? --> <one-to-one name="corpOther" entity-name="AdvancedCorpInfo" node="corp-other-info" constrained="true" cascade="all" /> <!-- 多对一映射 --> <many-to-one name="city" entity-name="City" embed-xml="true" node="city-code" cascade="all"> <column name="CITY_ID" sql-type="NUMBER(8)"> <comment>address of city</comment> </column> </many-to-one> <many-to-one name="gongShangOrg" node="belong-dist-org" entity-name="GongShangOrg" cascade="all"> <column name="GS_ORG" sql-type="NUMBER(8)"></column> <column name="GS_ID" sql-type="NUMBER(8)"></column> <column name="GS_SEQ_ID" sql-type="NUMBER(8)"></column> </many-to-one> <!-- 基于连接表的一对多双向关联,One端--> <list name="stocks" embed-xml="true" node="stocks-of-corp" cascade="all,delete-orphan" batch-size="20" table="T_M_CORP_STOCK"> <key not-null="true" foreign-key="FK_CORP_STOCK"> <column name="CORP_ORG" sql-type="NUMBER(8)" /> <column name="CORP_ID" sql-type="NUMBER(8)" /> <column name="CORP_SEQ_ID" sql-type="NUMBER(8)" /> </key> <list-index> <column name="ORD"> <comment>list index</comment> </column> </list-index> <many-to-many unique="true" entity-name="Stock" node="stock" embed-xml="true"> <column name="STOCK_ORG" sql-type="NUMBER(8)" /> <column name="STOCK_ID" sql-type="NUMBER(8)" /> <column name="STOCK_SEQ_ID" sql-type="NUMBER(8)" /> </many-to-many> </list> <!-- 基于外键的一对多关联,One端 --> <set name="oldStocks"> <key> <column name="CORP_ORG" sql-type="NUMBER(8)" /> <column name="CORP_ID" sql-type="NUMBER(8)" /> <column name="CORP_SEQ_ID" sql-type="NUMBER(8)" /> </key> <one-to-many entity-name="stock"/> </set> <property name="timestamp" node="timestamp" type="fmt_date_time"> <column name="STATE_DATE"> <comment>Version column</comment> </column> </property> </class> <class entity-name="Stock" node="Stock" table="T_STOCK"> <composite-id name="id" node="identifier"> <key-property name="org" node="org" type="long"> <column name="ORG" sql-type="NUMBER(8)"> <comment>database Code</comment> </column> </key-property> <key-property name="id" node="id" type="long"> <column name="ID" sql-type="NUMBER(8)"> <comment>stock identifier</comment> </column> </key-property> <key-property name="seqId" node="seq-id" type="long"> <column name="SEQ_ID" sql-type="NUMBER(8)"> <comment>changing sequence</comment> </column> </key-property> <generator class="assigned" /> </composite-id> <property name="stockName" node="stock-name" not-null="true" type="string"> <column name="NAME" sql-type="VARCHAR2(64)"> <comment>stock's name</comment> </column> </property> <property name="stockType" node="stock-type" type="string"> <column name="STOCK_TYPE" sql-type="VARCHAR2(64)"> <comment>type of a stock</comment> </column> </property> <property name="stockRegCapi" node="stock-registered-capital" type="double"> <column name="STOCK_REG_CAPI" sql-type="NUMBER(12,2)"> <comment>registered capital of a stock</comment> </column> </property> <property name="stockActualCapi" node="stock-actual-capital" type="double"> <column name="STOCK_ACTUAL_CAPI" sql-type="NUMBER(12,2)"> <comment>actual capital of a stock</comment> </column> </property> <property name="capiType" node="captial-type" type="string"> <column name="CAPI_TYPE" sql-type="VARCHAR2(64)"> <comment>capi type of a stock</comment> </column> </property> <!-- 基于连接表的一对多双向关联,Many端 --> <join table="T_M_CORP_STOCK" inverse="true" optional="true"> <key not-null="true" foreign-key="FK_CORP_STOCK"> <column name="STOCK_ORG" sql-type="NUMBER(8)" /> <column name="STOCK_ID" sql-type="NUMBER(8)" /> <column name="STOCK_SEQ_ID" sql-type="NUMBER(8)" /> </key> <!-- 注意:在本Sample中,如果下行embed-xml设置为true则导致循环引用,XML处理出错 --> <many-to-one name="corp" embed-xml="false" entity-name="Corp" node="corp-ref"> <column name="CORP_ORG" sql-type="NUMBER(8)" /> <column name="CORP_ID" sql-type="NUMBER(8)" /> <column name="CORP_SEQ_ID" sql-type="NUMBER(8)" /> </many-to-one> </join> </class> <class entity-name="GongShangOrg" node="org" table="T_DICT_ORG"> <composite-id name="id" node="-"> <key-property name="org" node="org" type="long"> <column name="ORG" sql-type="NUMBER(8)"> <comment>database Code</comment> </column> </key-property> <key-property name="id" node="id" type="long"> <column name="ID" sql-type="NUMBER(8)"> <comment>gong shang org identifier</comment> </column> </key-property> <key-property name="seqId" node="seq-id" type="long"> <column name="SEQ_ID" sql-type="NUMBER(8)"> <comment>changing sequence</comment> </column> </key-property> <generator class="assigned" /> </composite-id> <property name="orgName" node="org-name" type="string"> <column name="NAME" sql-type="VARCHAR2(64)"> <comment>city name</comment> </column> </property> </class> <class entity-name="AdvancedCorpInfo" node="adv-corp-info" table="T_CORP_OTHER"> <composite-id name="id" node="-"> <key-property name="org" node="org" type="long"> <column name="ORG" sql-type="NUMBER(8)"> <comment>database Code</comment> </column> </key-property> <key-property name="id" node="id" type="long"> <column name="ID" sql-type="NUMBER(8)"> <comment>corporation identifier</comment> </column> </key-property> <key-property name="seqId" node="seq-id" type="long"> <column name="SEQ_ID" sql-type="NUMBER(8)"> <comment>changing sequence</comment> </column> </key-property> <generator class="assigned" /> </composite-id> <property name="webSite" node="web-site" type="string"> <column name="WEB_URL" sql-type="VARCHAR2(128)"> <comment>web site url</comment> </column> </property> <property name="citySign" node="city-or-village" type="boolean"> <column name="CITY_SIGN" sql-type="NUMBER(1)"> <comment>city village flag</comment> </column> </property> </class> <class entity-name="City" node="City" table="T_DICT_CITY"> <comment> <![CDATA[ City dictionary ]]> </comment> <!-- 简单主键映射 --> <id name="cityId" node="-" type="long" > <column name="CITY_ID" sql-type="NUMBER(8)"> <comment>city identifier</comment> </column> <generator class="sequence"> <param name="sequence">SEQ_HXM</param> </generator> </id> <property name="cityName" node="-" type="string"> <column name="NAME" sql-type="VARCHAR2(64)"> <comment>city name</comment> </column> </property> <property name="cityCode" node="." type="int"> <column name="CODE" sql-type="NUMBER(4)"> <comment>city code</comment> </column> </property> </class> </hibernate-mapping> |
15 years ago
0
Java NIO浅析
什么是NIO
I/O是计算机与外部世界或者一个程序与计算机的其余部分的之间的接口,是操作系统的重要组成部分,I/O核心功能通常是操作系统内核的一部分。
NIO即“New I/O”(也有人将其解释为Non-blocking I/O),是JDK 1.4引入的新的输…
阅读全文
15 years ago
0
Axis知识集锦
Axis 动态WebService客户端样例
1 2 3 4 5 6 7 8 9 |
org.apache.axis.client.Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress( "http://192.168.1.184:9090/services/flowService" ); call.setOperationName( new QName( "http://flow.gmem.cc/", "request" ) ); call.addParameter( "userName", XMLType.XSD_STRING, ParameterMode.IN ); call.addParameter( "password", XMLType.XSD_STRING, ParameterMode.IN ); call.addParameter( "xml", XMLType.XSD_STRING, ParameterMode.IN ); call.setReturnType( XMLType.XSD_STRING ); Object result = call.invoke( new Object[] { "USER", "111111", xml } ); |
Axis1.4 客户端调用报错:Fault occurred while processing
报错信息:
AxisFault
faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server
faultSubcode:
faultString: Fault occurred w… 阅读全文
15 years ago
0