Javassit |
|
|
Javassist是一个开源的分析、编辑和创建Java字节码的类库。是由东京工业大学的数学和计算机科学系的 Shigeru Chiba (千叶 滋)所创建的。它已加入了开放源代码JBoss 应用服务器项目,通过使用Javassist对字节码操作为JBoss实现动态AOP框架。
关于java字节码的处理,目前有很多工具,如bcel,asm。不过这些都需要直接跟虚拟机指令打交道。如果你不想了解虚拟机指令,可以采用javassist。javassist是jboss的一个子项目,其主要的优点,在于简单,而且快速。直接使用java编码的形式,而不需要了解虚拟机指令,就能动态改变类的结构,或者动态生成类。
|
Hibernate mapping xml |
|
|
<?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 default-access="field">
<class name="com.aaxis.model.Person" table="person" dynamic-insert="true" dynamic-update="true"
select-before-update="true">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"/>
</id>
<version name="version" column="version" type="java.lang.Long"/>
<property name="active" column="active" type="java.lang.Boolean"/>
<property name="name" column="name" type="java.lang.String"/>
<property name="description" column="description" type="java.lang.String"/>
</class>
</hibernate-mapping>
|
Hibernate configure xml |
|
|
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/couple?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.use_second_level_cache">false</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="jdbc.batch_size">100</property>
<property name="jdbc.fetch_size">10</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Format all executed SQL to stdout -->
<property name="format_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="hibernate/mappings/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
|
FileCopy |
file, copy |
|
package com.dream.channel;
import junit.framework.TestCase;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* Created by IntelliJ IDEA.
* User: Zhong Gang
* Date: 11-10-5
* Time: 上午10:19
*/
public class ChannelTest extends TestCase {
public void testFileChannelCopy() throws Exception {
long start = System.currentTimeMillis();
FileInputStream fileInputStream = new FileInputStream("G:\\The.Big.Bang.Theory.S05E01.720p.HDTV.x264-IMMERSE.mkv");
FileOutputStream fileOutputStream = new FileOutputStream("G:\\TBBT.mkv");
FileChannel fileInputStreamChannel = fileInputStream.getChannel();
FileChannel fileOutputStreamChannel = fileOutputStream.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (true) {
buffer.clear();
int read = fileInputStreamChannel.read(buffer);
if (read == -1) {
break;
}
buffer.flip();
fileOutputStreamChannel.write(buffer);
}
long end = System.currentTimeMillis();
System.out.println("Time spend is " + (end - start));
}
public void testFileCopy() throws Exception {
long start = System.currentTimeMillis();
FileInputStream fileInputStream = new FileInputStream("G:\\The.Big.Bang.Theory.S05E01.720p.HDTV.x264-IMMERSE.mkv");
FileOutputStream fileOutputStream = new FileOutputStream("G:\\TBBT.mkv");
int read = fileInputStream.read();
while (read != -1) {
fileOutputStream.write(read);
read = fileInputStream.read();
}
long end = System.currentTimeMillis();
System.out.println("Time spend is " + (end - start));
}
public void testFileBufferCopy() throws Exception {
long start = System.currentTimeMillis();
FileInputStream fileInputStream = new FileInputStream("G:\\read.txt");
FileOutputStream fileOutputStream = new FileOutputStream("G:\\write.txt");
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
int read = bufferedInputStream.read();
while (read != -1) {
bufferedOutputStream.write(read);
read = bufferedInputStream.read();
}
bufferedOutputStream.flush();
long end = System.currentTimeMillis();
System.out.println("Time spend is " + (end - start));
}
}
|
JVM各区大小设置 |
jvm size |
|
-Xms512m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=256m
|
commons file upload |
fileupload |
http://search.maven.org/ |
<!-- commons fileupload dependencies -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.0.1</version>
</dependency>
|
Hibernate Junit test |
junit test |
|
import junit.framework.TestCase;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import java.util.Properties;
/**
* Created by IntelliJ IDEA.
* User: Zhong Gang
* Date: 11-8-8
* Time: 下午10:03
*/
public class UserDaoImplTest extends TestCase {
private SessionFactory sessionFactory = null;
@Override
public void setUp() throws Exception {
Properties properties = new Properties();
properties.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
properties.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/demo");
properties.setProperty("hibernate.connection.username", "root");
properties.setProperty("hibernate.connection.password", "root");
Configuration configuration = new Configuration();
configuration.addResource("hibernate_mappings/User.hbm.xml");
configuration.addResource("hibernate_mappings/Authority.hbm.xml");
configuration.addResource("hibernate_mappings/Husband.hbm.xml");
configuration.addResource("hibernate_mappings/Person.hbm.xml");
configuration.addResource("hibernate_mappings/Role.hbm.xml");
configuration.addResource("hibernate_mappings/Wife.hbm.xml");
configuration.setProperties(properties);
sessionFactory = configuration.buildSessionFactory();
}
public void testUsersCount() {
Session session = sessionFactory.openSession();
boolean connected = session.isConnected();
assertTrue(connected);
}
}
|
url rewriter |
url rewriter |
|
一。引入url rewriter
<dependency>
<groupId>org.tuckey</groupId>
<artifactId>urlrewritefilter</artifactId>
<version>3.1.0</version>
</dependency>
二。配置UrlRewriterFilter
<filter>
<filter-name>urlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>urlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
三。配置urlrewriter.xml 该配置文件需要放在WEB-INF根目录下,如果需要配置自定义的urlrewriter.xml文件位置,那么需要在urlRewriterFilter中配置configPath参数
urlrewriter.xml举例
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN"
"http://tuckey.org/res/dtds/urlrewrite3.0.dtd">
<urlrewrite>
<rule>
<from>/index.html$</from>
<to>/index.jsp</to>
</rule>
<outbound-rule>
<from>/index.jsp</from>
<to>/index.html</to>
</outbound-rule>
</urlrewrite>
|
Singleton |
singleton |
|
package com.template.guice;
/**
* Created by IntelliJ IDEA.
* User: Zhong Gang
* Date: 11-8-6
* Time: 下午3:16
*/
public class DBConnection {
private static final String DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/demo";
private static final String URERNAME = "root";
private static final String PASSWORD = "root";
private String driverClassName;
private String url;
private String username;
private String password;
private static DBConnection ourInstance = new DBConnection(DRIVER_CLASS_NAME, URL, URERNAME, PASSWORD);
public static DBConnection getInstance() {
return ourInstance;
}
private DBConnection() {
}
private DBConnection(String driverClassName, String url, String username, String password) {
this.driverClassName = driverClassName;
this.url = url;
this.username = username;
this.password = password;
}
}
|